GeneUtils.java 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package com.sf.util;
  2. import com.baomidou.mybatisplus.generator.FastAutoGenerator;
  3. import com.baomidou.mybatisplus.generator.config.OutputFile;
  4. import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
  5. import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
  6. import java.sql.Types;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.List;
  10. public class GeneUtils {
  11. public static void main(String[] args) {
  12. FastAutoGenerator
  13. .create("jdbc:mysql://localhost:3306/novel-cloud?useUnicode=true&characterEncoding=utf-8&allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Shanghai",
  14. "root", "root123456")
  15. .globalConfig(builder -> {
  16. builder.author("Qing") // 设置作者
  17. // .fileOverride() // 覆盖已生成文件
  18. .outputDir("src/main/java/"); // 指定输出目录 java文件的整体地址
  19. // .outputDir("src\\main\\java\\"); // windows中修改为\\
  20. })
  21. .dataSourceConfig(builder ->
  22. builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
  23. int typeCode = metaInfo.getJdbcType().TYPE_CODE;
  24. if (typeCode == Types.SMALLINT) {
  25. // 自定义类型转换
  26. // tinyInt smallInt < int < bigInt
  27. // byte short int long
  28. return DbColumnType.INTEGER;
  29. }
  30. return typeRegistry.getColumnType(metaInfo);
  31. }))
  32. .packageConfig(builder -> {
  33. builder.parent("com.sf") // 设置父包名 自定义的源代码地址 对应Application主程序入口的包名
  34. .moduleName("") // 设置父包模块名
  35. .pathInfo(Collections.singletonMap(
  36. OutputFile.xml, "src/main/resources/mapper")) // 设置mapperXml生成路径
  37. .entity("po") // 实体类对应的包名 entity->po
  38. .service("service")
  39. .serviceImpl("service.impl") // 服务层实现类对应的位置
  40. .mapper("mapper") // dao层对应的包名 dao->mapper
  41. .xml("mapper.xml")
  42. .controller("controller")
  43. // .other("other")
  44. // .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://"))
  45. .build();
  46. })
  47. .strategyConfig(builder -> {
  48. // sys_user t_user User UserMapper UserService
  49. // List<String> tableList = new ArrayList<>();
  50. // tableList.add("chapter_content");
  51. // tableList.add("fiction_shelf");
  52. // builder.addInclude(tableList); // 设置需要生成的表名
  53. // builder.addInclude("chapter_content","fiction_shelf"); // 设置需要生成的表名
  54. builder.addInclude("home_book"); // 设置需要生成的表名
  55. // .addTablePrefix("t_", "c_"); // 设置过滤表前缀
  56. })
  57. .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
  58. .execute();
  59. }
  60. }