mybatis plus在spring boot中的使用

pom配置:


    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.0.RELEASE
    

    xx.flower
    my_boot
    0.0.1-SNAPSHOT

    
        
            com.baomidou
            mybatis-plus-generator
            3.1.2
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.1.2
        
        
            org.projectlombok
            lombok
            1.18.2
        
        
            org.slf4j
            slf4j-api
            1.8.0-beta4
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            mysql
            mysql-connector-java
            8.0.15
        
    

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


   org.springframework.boot
   spring-boot-starter-test

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 list = infoMapper.selectList(null);
        list.forEach(System.out::println);
    }
}

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

结果展示:
运行结果展示

你可能感兴趣的:(mybatis plus在spring boot中的使用)