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、rvice.java、rviceimpl.java,甚至controller
1.pom.xml添加相关依赖,请注意版本号:
<?xml version="1.0" encoding="utf-8"?><project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="/d/file/titlepic/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>relea</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
rver: port: 9999spring: application: name: springboot-mybatisplus # databa 部分注释 datasource: type: com.alibaba.druid.pool.druiddatasource url: jdbc:mysql://localhost:3306/rocketmq?uunicode=true&characterencoding=utf-8&ussl=fal&autoreconect=true&rvertimezone=gmt%2b8 urname: 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: lect 1 from dual testwhileidle: true testonborrow: fal testonreturn: fal 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-ca: 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("ur.dir");// gc.toutputdir(projectpath + "/src/main/java"); string projectpath = "e://java-workspace//rocketmq//rocketmq_space//rocketmq//rocketmq-producer"; gc.toutputdir(projectpath + "\\src\\main\\java\\com\\asiainfo\\crm\\rocketmq\\cell"); // todo 设置用户名 gc.tauthor("zhangpb"); gc.topen(true); // rvice 命名方式 gc.trvicename("怎样设置电脑自动关机%srvice"); // rvice impl 命名方式 gc.trviceimplname("%srviceimpl"); // 自定义文件命名,注意 %s 会自动填充表实体属性! gc.tmappername("%smapper"); gc.txmlname("%smapper"); gc.tfileoverride(true); gc.tactiverecord(true); // xml 二级缓存 gc.tenablecache(fal); // xml resultmap gc.tbaresultmap(true); // xml columlist gc.tbacolumnlist(fal); mpg.tglobalconfig(gc); // todo 数据源配置 datasourceconfig dsc = new datasourceconfig(); dsc.turl("jdbc:mysql://127.0.0.1:3306/rocketmq?uunicode=true&ussl=fal&characterencoding=utf8&rvertimezone=utc"); dsc.tdrivername("com.my美国人的饮食习惯sql.jdbc.driver"); dsc.turname("root"); dsc.tpassword("root"); mpg.tdatasource(dsc); // todo 包配置 packageconfig pc = new packageconfig(); //pc.tmodulename(scanner("模块名"));// pc.tparent("com.zhangpb.demodruid"); pc.tentity("entity"); pc.trvice("rvice"); pc.trviceimpl("rvice.impl"); mpg.tpackageinfo(pc); // 自定义需要填充的字段 list<tablefill> tablefilllist = new arraylist<>(); //如 每张表都有一个创建时间、修改时间 //而且这基本上就是通用的了,新增时,创建时间和修改时间同时修改 //修改时,修改时间会修改, //虽然像mysql数据库有自动更新几只,但像oracle的数据库就没有了, //使用公共字段填充功能,就可以实现,自动按场景更新了。 //如下是配置 //tablefill createfield = new tablefill("gmt_create", fieldfill.inrt); //tablefill modifiedfield = new tablefill("gmt_modified", fieldfill.inrt_update); //tablefilllist.add(createfield); //tablefilllist.add(modi午安问候语fiedfield); // 自定义配置 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.tfileoutconfiglist(foclist); mpg.tcfg(cfg); mpg.ttemplate(new templateconfig().txml(null)); // 策略配置 strategyconfig strategy = new strategyconfig(); strategy.tnaming(namingstrategy.underline_to_camel); strategy.tcolumnnaming(namingstrategy.underline_to_camel); strategy.tentitylombokmodel(true); // 设置逻辑删除键 strategy.tlogicdeletefieldname("deleted"); // todo 指定生成的bean的数据库表名 strategy.tinclude("trade_coupon"); //strategy.tsuperentitycolumns("id"); // 驼峰转连字符 strategy.tcontrollermappinghyphenstyle(true); mpg.tstrategy(strategy); // 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有! mpg.ttemplateengine(new freemarkertemplateengine()); mpg.execute(); }}
到此这篇关于springboot集成mybatis-plus并实现自动生成相关文件的文章就介绍到这了,更多相关springboot集成mybatis-plus自动生成相关文件内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 03:17:11,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/634b40030686467daa78614e568f65e8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:SpringBoot集成Mybatis.doc
本文 PDF 下载地址:SpringBoot集成Mybatis.pdf
留言与评论(共有 0 条评论) |