• Spring boot整合MyBatis


      步骤一:导入依赖

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.1.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>demo</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--引入springboot-mybatis的依赖 -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.1.1</version>
            </dependency>
            <!--MySQL的依赖-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.32</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </dependency>
            <!-- 引入springboot-mybatis的依赖 -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.1.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.32</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

      步骤二:application.properties文件

    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/student?useUnicode=true&amp;characterEncoding=utf-8
    spring.datasource.password=123
    spring.datasource.username=root

      步骤三:数据库和实体类

      

    package com.example.demo.entity;
    
    public class student {
        private Integer stuid;
        private String stuname;
        private Integer age;
    
        public Integer getStuid() {
            return stuid;
        }
    
        public void setStuid(Integer stuid) {
            this.stuid = stuid;
        }
    
        public String getStuname() {
            return stuname;
        }
    
        public void setStuname(String stuname) {
            this.stuname = stuname;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    }

      步骤四:dao

    @Repository("istudao")
    public interface Istudao {
    
        @Select("select * from student")
        public List<student> allstu();
    }

      步骤五:service与serviceimpl

    public interface studentservice {
        public List<student> allstu();
    }
    @Service
    public class studentserviceimpl implements studentservice {
    
        @Resource
        private Istudao istudao;
    
    
        @Override
        public List<student> allstu() {
            return istudao.allstu();
        }
    }

      步骤六:Controller

    @Controller
    public class myconterller {
    
        @Resource
        private studentservice studentservice;
    
        @RequestMapping("/stu")
        @ResponseBody
        public Object getAll(){
            return studentservice.allstu();
        }
    }

      步骤七:启动

    @SpringBootApplication()
    @MapperScan("com.example.demo.dao")
    public class Springboot {
        public static void main(String[] args) {
            SpringApplication.run(Springboot.class, args);
        }
    }

      步骤八:完成

  • 相关阅读:
    用GDB调试程序(一)
    Linux编程基础——GDB(设置断点)
    滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(10月12日~10月18日)
    北京Uber优步司机奖励政策(10月19日~10月25日)
    借贷宝人脸识别无需绑卡,需合作者共同冲刺
    王璐首次详解借贷宝诞生历程 直面创业从0到1
    创造信用收入 借贷宝颠覆创新普惠金融
    php中,如何将编译后的代码,反编译回去。
    chrome调试ajax
    提示text还能输入多少字节
  • 原文地址:https://www.cnblogs.com/whtt/p/11984637.html
Copyright © 2020-2023  润新知