SpringBoot集成Mybatis-plus并实现自动生成相关文件的示例代码
程序员文章站
2022-03-07 21:45:02
mybatis-plus是一个优秀的mybatis增强工具,目前更新到3.1.1。mybatis-plus原生提供了很多单表操作的方法,极大简化了繁琐的curd的操作,同时又支持xml配置、自定义sq...
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添加相关依赖,请注意版本号:
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.6.1</version> <relativepath/> <!-- lookup parent from repository --> </parent> <groupid>com.asiainfo</groupid> <artifactid>rocketmq-producer</artifactid> <version>0.0.1-snapshot</version> <name>rocketmq-producer</name> <description>demo project for spring boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </dependency> <!--springboot集成mybatis-plus开始--> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <scope>runtime</scope> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-freemarker</artifactid> </dependency> <dependency> <groupid>com.baomidou</groupid> <artifactid>mybatis-plus-boot-starter</artifactid> <version>2.3</version> </dependency> <dependency> <groupid>com.alibaba</groupid> <artifactid>druid</artifactid> <version>1.1.5</version> </dependency> <dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> <optional>true</optional> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> <exclusions> <exclusion> <groupid>org.junit.vintage</groupid> <artifactid>junit-vintage-engine</artifactid> </exclusion> </exclusions> </dependency> <dependency> <groupid>org.testng</groupid> <artifactid>testng</artifactid> <version>release</version> <scope>compile</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> <!-- 添加代码 --> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-resources-plugin</artifactid> <version>2.7</version> <dependencies> <dependency> <groupid>org.apache.maven.shared</groupid> <artifactid>maven-filtering</artifactid> <version>1.3</version> </dependency> </dependencies> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.yml</include> </includes> <filtering>true</filtering> </resource> </resources> </build> </project>
2.application.yml
server: port: 9999 spring: 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: 180 mybatis-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 @slf4j public 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<tablefill> 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<fileoutconfig> 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并实现自动生成相关文件的文章就介绍到这了,更多相关springboot集成mybatis-plus自动生成相关文件内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 详解python日期时间处理2
下一篇: 详解python日期时间处理