经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
Springboot Mybatis Plus自动生成工具类详解代码
来源:jb51  时间:2021/11/24 17:10:03  对本文有异议

前言

代码生成器,也叫逆向工程,是根据数据库里的表结构,自动生成对应的实体类、映射文件和接口。

看到很多小伙伴在为数据库生成实体类发愁,现分享给大家,提高开发效率。

一、pom依赖

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>mybatis-plus-boot-starter</artifactId>
  4. <version>3.4.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.baomidou</groupId>
  8. <artifactId>mybatis-plus-generator</artifactId>
  9. <version>3.4.1</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.freemarker</groupId>
  13. <artifactId>freemarker</artifactId>
  14. <version>2.3.30</version>
  15. </dependency>

二、工具类

  1. package com.his.utils;
  2.  
  3. import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
  4. import com.baomidou.mybatisplus.core.toolkit.StringPool;
  5. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  6. import com.baomidou.mybatisplus.generator.AutoGenerator;
  7. import com.baomidou.mybatisplus.generator.InjectionConfig;
  8. import com.baomidou.mybatisplus.generator.config.*;
  9. import com.baomidou.mybatisplus.generator.config.po.TableInfo;
  10. import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
  11. import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.Scanner;
  15.  
  16. /**
  17. * Mybatis plus代码自动生成
  18. */
  19. public class MybatisPlusUtil {
  20. /** 作者 */
  21. public static final String AUTHOR = "dd";
  22.  
  23. /** 类命名 */
  24. /**
  25. * Entity命名
  26. */
  27. public static final String FILE_NAME_ENTITY = "%sEntity";
  28. /**
  29. * MAPPER命名
  30. */
  31. public static final String FILE_NAME_MAPPER = "%sMapper";
  32. /**
  33. * xml命名
  34. */
  35. public static final String FILE_NAME_XML = "%sMapper";
  36. /**
  37. * Service命名
  38. */
  39. public static final String FILE_NAME_SERVICE = "%sService";
  40. /**
  41. * ServiceImpl命名
  42. */
  43. public static final String FILE_NAME_SERVICE_IMPL = "%sDO";
  44. /**
  45. * Controller命名
  46. */
  47. public static final String FILE_NAME_CONTROLLER = "%sController";
  48.  
  49. /**
  50. 包命名,可以根据自己的项目情况自定义生成后的存放路径
  51. entity默认路径为父目录.entity
  52. mapper默认路径为父目录.mapper
  53. service默认路径为父目录.service
  54. serviceImpl默认路径为父目录.service.impl
  55. controller默认路径为父目录.controller
  56. */
  57. /**
  58. * PARENT命名
  59. */
  60. public static final String PACKAGE_NAME_PARENT = "com.his";
  61. /**
  62. * Entity命名
  63. */
  64. public static final String PACKAGE_NAME_ENTITY = "repository.entity.control";
  65. /**
  66. * MAPPER命名
  67. */
  68. public static final String PACKAGE_NAME_MAPPER = "repository.mapper.control";
  69. /**
  70. * xml命名
  71. */
  72. public static final String PACKAGE_NAME_XML = "sys";
  73. /**
  74. * Service命名
  75. */
  76. public static final String PACKAGE_NAME_SERVICE = "domain.control";
  77. /**
  78. * ServiceImpl命名
  79. */
  80. public static final String PACKAGE_NAME_SERVICE_IMPL = "domain.control";
  81. /**
  82. * Controller命名
  83. */
  84. public static final String PACKAGE_NAME_CONTROLLER = "facade.controller.control";
  85.  
  86. /**
  87. * 读取控制台内容
  88. */
  89. private static String scanner(String tip) {
  90. Scanner scanner = new Scanner(System.in);
  91. StringBuilder help = new StringBuilder();
  92. help.append("请输入" + tip + ":");
  93. System.out.println(help.toString());
  94. if (scanner.hasNext()) {
  95. String ipt = scanner.next();
  96. if (StringUtils.isNotBlank(ipt)) {
  97. return ipt;
  98. }
  99. }
  100. throw new MybatisPlusException("请输入正确的" + tip + "!");
  101. }
  102.  
  103. /**
  104. * 运行这个main方法进行代码生成
  105. */
  106. public static void main(String[] args) {
  107. // 代码生成器
  108. AutoGenerator mpg = new AutoGenerator();
  109.  
  110. // 全局配置
  111. GlobalConfig gc = new GlobalConfig();
  112. String projectPath = System.getProperty("user.dir");
  113. gc.setOutputDir(projectPath + "/src/main/java");
  114. gc.setFileOverride(true);
  115. gc.setAuthor(AUTHOR);
  116. gc.setOpen(false);
  117. gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
  118. gc.setEnableCache(false);// XML 二级缓存
  119. gc.setSwagger2(true); // 实体属性 Swagger2 注解
  120. gc.setBaseResultMap(true);
  121. gc.setBaseColumnList(true);
  122.  
  123. gc.setEntityName(FILE_NAME_ENTITY);
  124. gc.setMapperName(FILE_NAME_MAPPER);
  125. gc.setXmlName(FILE_NAME_XML);
  126. gc.setServiceName(FILE_NAME_SERVICE);
  127. gc.setServiceImplName(FILE_NAME_SERVICE_IMPL);
  128. gc.setControllerName(FILE_NAME_CONTROLLER);
  129. mpg.setGlobalConfig(gc);
  130.  
  131. // 数据源配置
  132. DataSourceConfig dsc = new DataSourceConfig();
  133. dsc.setUrl("jdbc:oracle:thin:@ip:port/test");
  134. dsc.setDriverName("oracle.jdbc.OracleDriver");
  135. dsc.setUsername("user");
  136. dsc.setPassword("pass");
  137. mpg.setDataSource(dsc);
  138.  
  139. //包配置
  140. PackageConfig pc = new PackageConfig();
  141. pc.setModuleName(null);
  142. pc.setParent(PACKAGE_NAME_PARENT);
  143. pc.setController(PACKAGE_NAME_CONTROLLER);
  144. pc.setService(PACKAGE_NAME_SERVICE);
  145. pc.setServiceImpl(PACKAGE_NAME_SERVICE_IMPL);
  146. pc.setMapper(PACKAGE_NAME_MAPPER);
  147. pc.setEntity(PACKAGE_NAME_ENTITY);
  148. pc.setXml(PACKAGE_NAME_XML);
  149. mpg.setPackageInfo(pc);
  150.  
  151. // 策略配置
  152. StrategyConfig strategy = new StrategyConfig();
  153. strategy.setNaming(NamingStrategy.underline_to_camel);
  154. strategy.setColumnNaming(NamingStrategy.underline_to_camel);
  155. //strategy.setEntityLombokModel(true);
  156. strategy.setRestControllerStyle(true);
  157. strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
  158. strategy.setControllerMappingHyphenStyle(true);
  159. // 设置表前缀
  160. strategy.setTablePrefix("IEMR_");
  161. mpg.setStrategy(strategy);
  162. mpg.setTemplateEngine(new FreemarkerTemplateEngine());
  163.  
  164. // 自定义配置
  165. InjectionConfig cfg = new InjectionConfig() {
  166. @Override
  167. public void initMap() {
  168. // to do nothing
  169. }
  170. };
  171.  
  172. // 如果模板引擎是 freemarker
  173. String templatePath = "/templates/mapper.xml.ftl";
  174. // 如果模板引擎是 velocity
  175. // String templatePath = "/templates/mapper.xml.vm";
  176.  
  177. // 自定义输出配置
  178. List<FileOutConfig> focList = new ArrayList<>();
  179. // 自定义配置会被优先输出
  180. focList.add(new FileOutConfig(templatePath) {
  181. @Override
  182. public String outputFile(TableInfo tableInfo) {
  183. // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
  184. return projectPath + "/src/main/resources/mapper/"
  185. + "/" + tableInfo.getMapperName() + StringPool.DOT_XML;
  186. }
  187. });
  188. cfg.setFileOutConfigList(focList);
  189. mpg.setCfg(cfg);
  190.  
  191. // 配置模板
  192. TemplateConfig templateConfig = new TemplateConfig();
  193. templateConfig.setXml(null);
  194. mpg.setTemplate(templateConfig);
  195.  
  196. mpg.execute();
  197. }
  198. }
  199.  

结尾

感谢大家的耐心阅读,如有建议请私信或评论留言。如有收获,劳烦支持,关注、点赞、评论、收藏均可,博主会经常更新,与大家共同进步

到此这篇关于Springboot Mybatis Plus自动生成工具类详解代码的文章就介绍到这了,更多相关Springboot Mybatis Plus 生成工具类内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号