Springboot集成Mybatis-plus

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

1、引入maven依赖

<dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.5.2</version>
</dependency>

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依赖
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.5.2</version>
</dependency>
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();
    }
}






扫描下方二维码,关注公众号:程序进阶之路,实时获取更多优质文章推送。


扫码关注

评论