CodeGenerator.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package com.hssx.cloudmodel.util;
  2. import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
  3. import com.baomidou.mybatisplus.core.toolkit.StringPool;
  4. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  5. import com.baomidou.mybatisplus.generator.AutoGenerator;
  6. import com.baomidou.mybatisplus.generator.InjectionConfig;
  7. import com.baomidou.mybatisplus.generator.config.*;
  8. import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
  9. import com.baomidou.mybatisplus.generator.config.po.TableInfo;
  10. import com.baomidou.mybatisplus.generator.config.rules.FileType;
  11. import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
  12. import java.io.File;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.Scanner;
  16. /**
  17. * mybatis-plus代码生成器
  18. * 使用该类需要添加以下依赖,在此之前请移除所有与mybatis有关的其他依赖,防止冲突
  19. * <dependency>
  20. * <groupId>com.baomidou</groupId>
  21. * <artifactId>mybatis-plus-generator</artifactId>
  22. * <version>3.1.2</version>
  23. * </dependency>
  24. *
  25. * <dependency>
  26. * <groupId>com.baomidou</groupId>
  27. * <artifactId>mybatis-plus-boot-starter</artifactId>
  28. * <version>3.1.2</version>
  29. * </dependency>
  30. *
  31. */
  32. // 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
  33. public class CodeGenerator {
  34. /**
  35. * <p>
  36. * 读取控制台内容
  37. * </p>
  38. */
  39. public static String scanner(String tip) {
  40. Scanner scanner = new Scanner(System.in);
  41. StringBuilder help = new StringBuilder();
  42. help.append("请输入" + tip + ":");
  43. System.out.println(help.toString());
  44. if (scanner.hasNext()) {
  45. String ipt = scanner.next();
  46. if (StringUtils.isNotEmpty(ipt)) {
  47. return ipt;
  48. }
  49. }
  50. throw new MybatisPlusException("请输入正确的" + tip + "!");
  51. }
  52. public static void main(String[] args) {
  53. // 代码生成器
  54. AutoGenerator mpg = new AutoGenerator();
  55. // 全局配置
  56. GlobalConfig gc = new GlobalConfig();
  57. // 全局配置
  58. // 自定义文件命名,注意 %s 会自动填充表实体属性!
  59. // gc.setMapperName("%sDao");
  60. // gc.setXmlName("%sMapper");
  61. // gc.setServiceName("%sService");
  62. // gc.setServiceImplName("%sServiceImap");
  63. // gc.setControllerName("%sController");
  64. //生成的代码存放到某个路径下,这里是E盘,
  65. // gc.setOutputDir("E://");
  66. //生成的代码位置为当前项目
  67. String projectPath = System.getProperty("user.dir");
  68. gc.setOutputDir(projectPath + "/src/main/java");
  69. gc.setAuthor("吴涛涛");
  70. gc.setOpen(false);
  71. gc.setFileOverride(true);
  72. gc.setActiveRecord(true);
  73. //%s是实体类类名占位符,不配置这行的话,对于User会生成IUserService,配置后即可生成UserService;
  74. gc.setServiceName("%sService");
  75. // XML 二级缓存
  76. // gc.setEnableCache(true);
  77. // XML ResultMap
  78. gc.setBaseResultMap(true);
  79. // XML columList
  80. gc.setBaseColumnList(true);
  81. //
  82. // gc.setSwagger2(true); 实体属性 Swagger2 注解
  83. mpg.setGlobalConfig(gc);
  84. // 数据源配置
  85. DataSourceConfig dsc = new DataSourceConfig();
  86. dsc.setUrl("jdbc:mysql://118.190.47.230:3306/cloud_model_test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8");
  87. // dsc.setSchemaName("public");
  88. dsc.setDriverName("com.mysql.cj.jdbc.Driver");
  89. dsc.setUsername("root");
  90. dsc.setPassword("p011430seya1026");
  91. mpg.setDataSource(dsc);
  92. // 包配置
  93. PackageConfig pc = new PackageConfig();
  94. //若果需要在Parent(此处即com.example.plus)下新建模块时打开下面注释,后续在控制台提示输入模块时,输入想要新建的模块名就可以
  95. // pc.setModuleName(scanner("模块名"));
  96. pc.setParent("com.hssx.cloudmodel");
  97. mpg.setPackageInfo(pc);
  98. // 自定义配置
  99. InjectionConfig cfg = new InjectionConfig() {
  100. @Override
  101. public void initMap() {
  102. // to do nothing
  103. }
  104. };
  105. //以下为两种模板来生成*mapper.xml文件,任选一种即可,不同的模板对应不同的依赖
  106. // 如果模板引擎是 freemarker,请添加以下依赖。
  107. /**
  108. * <dependency>
  109. * <groupId>org.freemarker</groupId>
  110. * <artifactId>freemarker</artifactId>
  111. * <version>2.3.23</version>
  112. * </dependency>
  113. */
  114. // String templatePath = "/templates/mapper.xml.ftl";
  115. // 如果模板引擎是 velocity 请添加以下依赖。
  116. /**
  117. * <dependency>
  118. * <groupId>org.apache.velocity</groupId>
  119. * <artifactId>velocity-engine-core</artifactId>
  120. * <version>2.0</version>
  121. * </dependency>
  122. */
  123. String templatePath = "/templates/mapper.xml.vm";
  124. // 自定义输出配置
  125. List<FileOutConfig> focList = new ArrayList<>();
  126. // 自定义配置会被优先输出
  127. focList.add(new FileOutConfig(templatePath) {
  128. @Override
  129. public String outputFile(TableInfo tableInfo) {
  130. if(pc.getModuleName() == null){
  131. return projectPath + "/src/main/resources/mapper/"
  132. + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
  133. }else{
  134. // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
  135. return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
  136. + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
  137. }
  138. }
  139. });
  140. cfg.setFileCreate(new IFileCreate() {
  141. @Override
  142. public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
  143. // 判断自定义文件夹是否需要创建,这里调用默认的方法
  144. checkDir(filePath);
  145. //对于已存在的文件,只需重复生成 entity 和 mapper.xml
  146. File file = new File(filePath);
  147. boolean exist = file.exists();
  148. if(exist){
  149. if (filePath.endsWith("Mapper.xml")||FileType.ENTITY==fileType){
  150. return true;
  151. }else {
  152. return false;
  153. }
  154. }
  155. //不存在的文件都需要创建
  156. return true;
  157. }
  158. });
  159. cfg.setFileOutConfigList(focList);
  160. mpg.setCfg(cfg);
  161. mpg.setTemplate(new TemplateConfig().setXml(null));
  162. // 配置模板
  163. // TemplateConfig templateConfig = new TemplateConfig();
  164. //
  165. // // 配置自定义输出模板
  166. // //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
  167. // // templateConfig.setEntity("templates/entity2.java");
  168. // // templateConfig.setService();
  169. // // templateConfig.setController();
  170. //
  171. // templateConfig.setXml(null);
  172. // mpg.setTemplate(templateConfig);
  173. // 策略配置
  174. StrategyConfig strategy = new StrategyConfig();
  175. strategy.setNaming(NamingStrategy.underline_to_camel);
  176. strategy.setColumnNaming(NamingStrategy.underline_to_camel);
  177. //若想要生成的实体类继承某个类,则可打开下面注释。写上需要继承的类的位置即可
  178. // strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
  179. //【实体】是否为lombok模型(默认 false)
  180. // strategy.setEntityLombokModel(true);
  181. //对控制器生成 @RestController 注解
  182. strategy.setRestControllerStyle(true);
  183. //是否生成实体时,生成字段注解
  184. strategy.setEntityTableFieldAnnotationEnable(true);
  185. // strategy.setEntitySerialVersionUID(false)//加此行不生成生成实体类序列化编号,不加默认生成
  186. //若想要生成的实体类继承某个Controller,则可打开下面注释。写上需要继承的Controller的位置即可
  187. // strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
  188. //此处user是表名,多个英文逗号分割
  189. strategy.setInclude("packet_loss_record");
  190. // strategy.setExclude();//数据库表全生成
  191. // strategy.setInclude(scanner("user").split(","));//表名,多个英文逗号分割
  192. strategy.setControllerMappingHyphenStyle(true);
  193. //数据库表前缀,不配置这行的话,生成的类会带有T如:TUser,配置后即可将前缀去掉
  194. // strategy.setTablePrefix("tb_");
  195. mpg.setStrategy(strategy);
  196. // mpg.setTemplateEngine(new FreemarkerTemplateEngine());
  197. mpg.execute();
  198. }
  199. }