SpringBoot项目使用mybatis-plus逆向自动生成全套代码

目录

  • 1.在你的SpringBoot项目下新建子模块项目
  • 2.在此模块下新建一个包与一个java类 类名: CodeGenerator
  • 3.在 resources 下新建 文件夹,用来存放 mapper文件
  • 4.配置CodeGenerator类
  • 5.启动代码生成类main方法
  • 6.删除文件

1.在你的SpringBoot项目下新建子模块项目 pom.xml添加以下依赖:
1.8mysqlmysql-connector-javacom.baomidoumybatis-plus-generator3.3.2com.baomidoumybatis-plus-extension3.3.2org.projectlomboklomboktrueorg.springframework.bootspring-boot-starter-freemarker2.3.1.RELEASEorg.springframework.bootspring-boot-maven-plugin

ps:名称随意,最好带上generator 来辨别这是代码自动生成模块

SpringBoot项目使用mybatis-plus逆向自动生成全套代码
文章图片


2.在此模块下新建一个包与一个java类 类名: CodeGenerator SpringBoot项目使用mybatis-plus逆向自动生成全套代码
文章图片

完整代码如下:
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.core.toolkit.StringUtils; 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.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.DateType; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * @Description: 代码生成类 */public class CodeGenerator {//数据库连接参数public static String driver = "com.mysql.cj.jdbc.Driver"; public static String url = "jdbc:mysql://localhost:3306/rht_test?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true"; public static String username="root"; public static String password="123456"; //父级别包名称public static String parentPackage = "cn.rht"; //代码生成的目标路径public static String generateTo = "/rht-generator/src/main/java"; //mapper.xml的生成路径public static String mapperXmlPath = "/rht-generator/src/main/resources/mapper"; //控制器的公共基类,用于抽象控制器的公共方法,null值表示没有父类public static String baseControllerClassName ; //业务层的公共基类,用于抽象公共方法public static String baseServiceClassName ; //作者名public static String author = "rht.cn"; //模块名称,用于组成包名public static String modelName = "portal"; //Mapper接口的模板文件,不用写后缀 .ftlpublic static String mapperTempalte = "/ftl/mapper.java"; /*** * 读取控制台内容*
*/public static String scanner(String tip) {Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append("请输入" + tip + ":"); System.out.println(help.toString()); if (scanner.hasNext()) {String ipt = scanner.next(); if (StringUtils.isNotEmpty(ipt)) {return ipt; }}throw new MybatisPlusException("请输入正确的" + tip + "!"); }/*** RUN THIS*/public static void main(String[] args) {// 代码生成器AutoGenerator mpg = new AutoGenerator(); // 全局配置GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + generateTo); gc.setAuthor(author); gc.setOpen(false); //设置时间类型为Dategc.setDateType(DateType.TIME_PACK); //开启swagger//gc.setSwagger2(true); //设置mapper.xml的resultMapgc.setBaseResultMap(true); mpg.setGlobalConfig(gc); // 数据源配置DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl(url); // dsc.setSchemaName("public"); dsc.setDriverName(driver); dsc.setUsername(username); dsc.setPassword(password); mpg.setDataSource(dsc); // 包配置PackageConfig pc = new PackageConfig(); pc.setEntity("model"); //pc.setModuleName(scanner("模块名")); pc.setModuleName(modelName); pc.setParent(parentPackage); mpg.setPackageInfo(pc); // 自定义配置InjectionConfig cfg = new InjectionConfig() {@Overridepublic void initMap() {// to do nothing}}; List focList = new ArrayList<>(); focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {@Overridepublic String outputFile(TableInfo tableInfo) {// 自定义输入文件名称return projectPath + mapperXmlPath+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; }}); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); mpg.setTemplate(new TemplateConfig().setXml(null)); mpg.setTemplate(new TemplateConfig().setMapper(mapperTempalte)); // 策略配置StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); //字段驼峰命名strategy.setColumnNaming(NamingStrategy.underline_to_camel); //设置实体类的lombokstrategy.setEntityLombokModel(true); //设置controller的父类if (baseControllerClassName!=null) strategy.setSuperControllerClass(baseControllerClassName); //设置服务类的父类if (baseServiceClassName !=null ) strategy.setSuperServiceImplClass(baseServiceClassName); // strategy.//设置实体类属性对应表字段的注解strategy.setEntityTableFieldAnnotationEnable(true); //设置表名String tableName = scanner("表名, all全部表"); if(! "all".equalsIgnoreCase(tableName)){strategy.setInclude(tableName); }strategy.setTablePrefix(pc.getModuleName() + "_"); strategy.setRestControllerStyle(true); mpg.setStrategy(strategy); // 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有!mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); }}


3.在 resources 下新建 文件夹,用来存放 mapper文件 新建模板文件: mapper.java.ftl

SpringBoot项目使用mybatis-plus逆向自动生成全套代码
文章图片

SpringBoot项目使用mybatis-plus逆向自动生成全套代码
文章图片


模板完整代码如下:
package ${package.Mapper}; import ${package.Entity}.${entity}; import ${superMapperClassPackage}; import org.springframework.stereotype.Repository; /** * * ${table.comment!} Mapper 接口 *
* * @author ${author} * @since ${date} */<#if kotlin>interface ${table.mapperName} : ${superMapperClass}<${entity}><#else>@Repositorypublic interface ${table.mapperName} extends ${superMapperClass}<${entity}> {}


4.配置CodeGenerator类 ps:请根据自己实际路径配置

SpringBoot项目使用mybatis-plus逆向自动生成全套代码
文章图片


5.启动代码生成类main方法 ps:输入all 将会自动生成配置数据库下的所有配置文件,或者直接输入单表名称生成某一个表的Controller,mapper,service,model层与mapper.xml文件
SpringBoot项目使用mybatis-plus逆向自动生成全套代码
文章图片


下面是我输入表名为:user,自动生成的部分文件信息展示

SpringBoot项目使用mybatis-plus逆向自动生成全套代码
文章图片

User实体类

SpringBoot项目使用mybatis-plus逆向自动生成全套代码
文章图片

UserMapper.xml文件

SpringBoot项目使用mybatis-plus逆向自动生成全套代码
文章图片

如果你有很多表要生成时,但又不想全部生成时,可以在CodeGenerator类代码中134行代码
//设置表名String tableName = scanner("表名, all全部表"); if(! "all".equalsIgnoreCase(tableName)){strategy.setInclude(tableName); }

替换为:
String[] tableNames = {"user","dept"}; //数据库表名的集合for (int i = 0; i
来生成自己想要生成的文件

6.删除文件 最后:也是重要的一点,在您将这些文件复制到了项目模块上的时候,留下CodeGenerator类与文件夹下的mapper.java.ftl配置,其他生成的请及时删除
至于原因是将来业务拓展后,数据库新增表后,只要新创建表的文件,如果不删除以前生成过的文件,到时候找起来比较麻烦,没必要给自己添这层麻烦
SpringBoot项目使用mybatis-plus逆向自动生成全套代码
文章图片


【SpringBoot项目使用mybatis-plus逆向自动生成全套代码】到此这篇关于SpringBoot项目使用mybatis-plus逆向自动生成全套代码的文章就介绍到这了,更多相关mybatis-plus逆向自动生成代码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读