SpringBoot集成Mybatis-plus并实现自动生成相关文件的示例代码怎么写
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章给大家介绍SpringBoot集成Mybatis-plus并实现自动生成相关文件的示例代码怎么写,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。Mybatis-Plus是一
千家信息网最后更新 2026年11月03日SpringBoot集成Mybatis-plus并实现自动生成相关文件的示例代码怎么写
这篇文章给大家介绍SpringBoot集成Mybatis-plus并实现自动生成相关文件的示例代码怎么写,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
Mybatis-Plus是一个优秀的Mybatis增强工具,目前更新到3.1.1。Mybatis-Plus原生提供了很多单表操作的方法,极大简化了繁琐的curd的操作,同时又支持xml配置、自定义sql的编写。这篇文章介绍SpringBoot2集成Mybatis-Plus 3.1.0,同时介绍mybatis提供MysqlGenerator.java,你可以通过指定的数据库表生成对应的bean、mapper.xml、mapper.java、service.java、serviceImpl.java,甚至controller
1.pom.xml添加相关依赖,请注意版本号:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.1 com.asiainfo rocketmq-producer 0.0.1-SNAPSHOT rocketmq-producer Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-freemarker com.baomidou mybatis-plus-boot-starter 2.3 com.alibaba druid 1.1.5 org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.testng testng RELEASE compile org.springframework.boot spring-boot-maven-plugin org.apache.maven.plugins maven-resources-plugin 2.7 org.apache.maven.shared maven-filtering 1.3 src/main/resources **/*.properties **/*.xml **/*.yml true
2.application.yml
server: port: 9999spring: application: name: springboot-mybatisPlus # database 部分注释 datasource: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/rocketmq?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconect=true&serverTimezone=GMT%2b8 username: root password: root driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 50 initialSize: 0 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 1 from dual testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 removeAbandoned: true removeAbandonedTimeout: 180mybatis-plus: global-config: # 逻辑删除配置 db-config: # 删除前 logic-not-delete-value: 1 # 删除后 logic-delete-value: 0 configuration: map-underscore-to-camel-case: true auto-mapping-behavior: full log-impl: org.apache.ibatis.logging.stdout.StdOutImpl mapper-locations: classpath:mybatisplus/mapper/*.xml
3.MyBatisPlusConfig.java
package com.asiainfo.crm.rocketmq.config;import com.baomidou.mybatisplus.mapper.ISqlInjector;import com.baomidou.mybatisplus.mapper.LogicSqlInjector;import com.baomidou.mybatisplus.plugins.PaginationInterceptor;import com.baomidou.mybatisplus.plugins.PerformanceInterceptor;import lombok.extern.slf4j.Slf4j;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Profile;/** * @author zhangpb * @date 2021/12/15 14:13 * @Description: */@Configuration@Slf4jpublic class MyBatisPlusConfig { /** * @description: 配置分页插件 * * @author: zhangpb * @date: 2019/1/15 10:17 * @param: [] * @return: com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor */ @Bean public PaginationInterceptor paginationInterceptor() { log.debug("注册分页插件"); return new PaginationInterceptor(); } /** * @description: SQL执行效率插件 * * @author: zhangpb * @date: 19-1-24 下午4:59 * @param: [] * @return: com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor */ @Bean @Profile({"test"})// 设置 dev test 环境开启 public PerformanceInterceptor performanceInterceptor() { return new PerformanceInterceptor(); } /** * 逻辑删除用,3.1.1之后的版本可不需要配置该bean,但项目这里用的是3.1.0的 * * @author zhangpb * * @return com.baomidou.mybatisplus.core.injector.ISqlInjector */ @Bean public ISqlInjector sqlInjector() { return new LogicSqlInjector(); }}4.MysqlGenerator.java
package com.asiainfo.crm.rocketmq.config;import com.baomidou.mybatisplus.generator.AutoGenerator;import com.baomidou.mybatisplus.generator.InjectionConfig;import com.baomidou.mybatisplus.generator.config.*;import com.baomidou.mybatisplus.generator.config.po.TableFill;import com.baomidou.mybatisplus.generator.config.po.TableInfo;import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;import java.util.ArrayList;import java.util.List;/** * @author zhangpb * @date 2021/12/15 14:14 * @Description: */public class MysqlGenerator { /** * 每次只生成一张表的 * @param args */ public static void main(String[] args) { // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig();// String projectPath = System.getProperty("user.dir");// gc.setOutputDir(projectPath + "/src/main/java"); String projectPath = "E://java-workspace//rocketmq//rocketmq_space//rocketmq//rocketmq-producer"; gc.setOutputDir(projectPath + "\\src\\main\\java\\com\\asiainfo\\crm\\rocketmq\\cell"); // TODO 设置用户名 gc.setAuthor("zhangpb"); gc.setOpen(true); // service 命名方式 gc.setServiceName("%sService"); // service impl 命名方式 gc.setServiceImplName("%sServiceImpl"); // 自定义文件命名,注意 %s 会自动填充表实体属性! gc.setMapperName("%sMapper"); gc.setXmlName("%sMapper"); gc.setFileOverride(true); gc.setActiveRecord(true); // XML 二级缓存 gc.setEnableCache(false); // XML ResultMap gc.setBaseResultMap(true); // XML columList gc.setBaseColumnList(false); mpg.setGlobalConfig(gc); // TODO 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://127.0.0.1:3306/rocketmq?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("root"); mpg.setDataSource(dsc); // TODO 包配置 PackageConfig pc = new PackageConfig(); //pc.setModuleName(scanner("模块名"));// pc.setParent("com.zhangpb.demodruid"); pc.setEntity("entity"); pc.setService("service"); pc.setServiceImpl("service.impl"); mpg.setPackageInfo(pc); // 自定义需要填充的字段 List tableFillList = new ArrayList<>(); //如 每张表都有一个创建时间、修改时间 //而且这基本上就是通用的了,新增时,创建时间和修改时间同时修改 //修改时,修改时间会修改, //虽然像Mysql数据库有自动更新几只,但像ORACLE的数据库就没有了, //使用公共字段填充功能,就可以实现,自动按场景更新了。 //如下是配置 //TableFill createField = new TableFill("gmt_create", FieldFill.INSERT); //TableFill modifiedField = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE); //tableFillList.add(createField); //tableFillList.add(modifiedField); // 自定义配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; List focList = new ArrayList<>(); focList.add(new FileOutConfig("/templates/mapper.xml.ftl") { @Override public String outputFile(TableInfo tableInfo) { // 自定义输入文件名称 return projectPath + "/src/main/resources/mapper/" + "/" + tableInfo.getEntityName() + "Mapper" + ".xml"; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); mpg.setTemplate(new TemplateConfig().setXml(null)); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); // 设置逻辑删除键 strategy.setLogicDeleteFieldName("deleted"); // TODO 指定生成的bean的数据库表名 strategy.setInclude("trade_coupon"); //strategy.setSuperEntityColumns("id"); // 驼峰转连字符 strategy.setControllerMappingHyphenStyle(true); mpg.setStrategy(strategy); // 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有! mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); }} 关于SpringBoot集成Mybatis-plus并实现自动生成相关文件的示例代码怎么写就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
配置
生成
数据
时间
文件
数据库
代码
同时
插件
逻辑
更新
示例
自动生成
内容
字段
方式
更多
版本
篇文章
帮助
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
上海卓越科技互联网
新华互联网科技学院
科技企业和互联网企业
互联网科技排名
亿策互联网科技
互联网金融科技平台
互联网科技小额贷款
郑州华宇互联网科技
上海卓越科技 互联网
互联网科技汛
金融科技和互联网金融
互联网之后的科技是什么
欧洲十大科技互联网公司
湖南诚度互联网科技有限公司
宜昌元轩互联网科技有限公司
亳州好赞互联网科技有限公司
互联网保险科技创业
恒生互联网科技业指数调整周期
恒生科技指数 中概互联网
科技互联网专业工作会议发言稿
互联网的科技
互联网与新科技
广州宏创互联网科技
深圳天众互联网科技公司
上海亿帐通互联网科技
美国互联网前沿科技
科技赋能互联网健康
福建互联网展示科技馆设计
万物互联网智能科技
广州智享互联网科技