栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot集成Mybatis-plus

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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) 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/991962.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号