美文网首页
mybatis plus在spring boot中的使用

mybatis plus在spring boot中的使用

作者: Willa王 | 来源:发表于2020-07-14 10:30 被阅读0次
    pom配置:
    <?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 http://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.0.RELEASE</version>
        </parent>
    
        <groupId>xx.flower</groupId>
        <artifactId>my_boot</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-generator</artifactId>
                <version>3.1.2</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.1.2</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.2</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.8.0-beta4</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </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>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.15</version>
            </dependency>
        </dependencies>
    </project>
    

    说明:由于使用的是SpringBootTest进行测试,而SpringBootTest需要使用junit5以上,spring boot2.2.X以上版本才支持junit5,所以该spring-boot-starter-parent版本使用2.2.0.RELEASE,并且需要依赖:

    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    
    spring boot的application.yml配置如下:
    server:
      port: 8088
    
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/test_xx?useUnicode=true&characterEncoding=UTF-8
        username: root
        password: root
    
    mybatis-plus:
      type-aliases-package: xx.flower.test.*.mapper
      mapper-locations: classpath:mapper/test/*.xml
      global-config:
        banner: false
      configuration:
        jdbc-type-for-null: null
    
    代码编写:

    启动springboot的代码:

    package xx.flower.test;
    
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    
    @SpringBootApplication(scanBasePackages="xx.flower")
    @MapperScan({"xx.flower.*.mapper"})
    public class MyApplication {
    
        public static void main(String[] args){
           new SpringApplicationBuilder(MyApplication.class).run(args);
        }
    }
    

    SpringBootTest的测试代码:

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import xx.flower.test.MyApplication;
    import xx.flower.test.entity.Info;
    import xx.flower.test.mapper.InfoMapper;
    
    import java.util.List;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = MyApplication.class)
    public class MybatisTest {
    
        @Autowired
        private InfoMapper infoMapper;
    
        @Test
        public void testSelect(){
            System.out.println("-------------");
            List<Info> list = infoMapper.selectList(null);
            list.forEach(System.out::println);
        }
    }
    

    说明:@SpringBootTest(classes = *)中的classes需要指定到:spring boot的启动类上,如果不指定则提示找不到自动注入类

    结果展示:
    运行结果展示

    相关文章

      网友评论

          本文标题:mybatis plus在spring boot中的使用

          本文链接:https://www.haomeiwen.com/subject/yuwgtktx.html