Springboot集成Mybatis-plus

       本文主要介绍如何在SpirngBoot最新版本2.71中集成mybatis-plus、配置自动代码生成器(自动生成Entity、Mapper、xml文件、Server、Controller)、以及集成分页插件pagehelper。

1、引入maven依赖


   com.baomidou
   mybatis-plus-boot-starter
   3.5.2

2、配置项设置

mybatis-plus:
  mapper-locations: classpath:mybatis/mapper/**/*.xml  #注意:一定要对应mapper映射xml文件的所在路径
  type-aliases-package: com.knight.payment.entity  # 注意:对应实体类的路径
  # spring boot集成mybatis的方式打印sql
  configuration:
    log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl #不打印SQL
    map-underscore-to-camel-case: true #开启自动下划线格式转驼峰格式

3、配置 MapperScan 注解

@SpringBootApplication
@MapperScan("com.xxx.mapper")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

4、使用代码生成器

4.1 引入maven依赖


    com.baomidou
    mybatis-plus-generator
    3.5.2

4.2 编写生成器类

public class CodeGenerator {
    static final String URL = "jdbc:mysql://127.0.0.1:3308/mysql?characterEncoding=utf-8&useSSL=false";
    public static void main(String[] args) {
        String projectPath=System.getProperty("user.dir");
        System.out.println(projectPath);

        FastAutoGenerator.create(URL, "root", "xxxxxx")
            .globalConfig(builder -> {
                builder.disableOpenDir()
                        .author("knight")
                        .dateType(DateType.ONLY_DATE)
                        .outputDir(projectPath + "/src/main/java");
            })
            .packageConfig(builder -> {
                builder.parent("com.xxx.xxx") // 设置父包名
                    .entity("entity")
                    .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath + "/src/main/resources/mybatis/mapper")); // 设置mapperXml生成路径
            })
            .strategyConfig(builder -> {
                builder.addInclude("sys_user") // 设置需要生成的表名
                       .addTablePrefix("t_", "c_"); // 设置过滤表前缀
                builder.entityBuilder()
                        .disableSerialVersionUID()
                        .enableChainModel()
                        .enableLombok();
                builder.mapperBuilder()
                        .superClass(BaseMapper.class)
                        .enableBaseColumnList()
                        .enableBaseResultMap();
                builder.controllerBuilder()
                        .superClass(BaseController.class)
                        .enableRestStyle();
                builder.serviceBuilder()
                        .superServiceClass(BaseService.class)
                        .formatServiceFileName("%sService")
                        .formatServiceImplFileName("%sServiceImp");
            })
            .templateEngine(new FreemarkerTemplateEngine())
            .execute();
    }
}

5、集成分页插件

5.1 引入maven依赖


   com.github.pagehelper
   pagehelper-spring-boot-starter
   1.4.3

5.2 配置项设置

#分页插件配置
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

备注:springboot版本一定要和pagehelper版本匹配。目前使用的SpirngBoot是2.7.1版本,pagehelper版本是1.4.3


新时代农民工 (QQ:277718357) 

你可能感兴趣的:(java,mybatis,spring,boot,java)