导入依赖

    1. <dependencies>
    2. <!--mybatisplus自动代码生成时,需要基于velocity模板进行生成-->
    3. <dependency>
    4. <groupId>org.apache.velocity</groupId>
    5. <artifactId>velocity</artifactId>
    6. <version>1.7</version>
    7. </dependency>
    8. <!-- mp生成实体类时使用swagger2的注解,需要引入对应依赖 -->
    9. <dependency>
    10. <groupId>io.springfox</groupId>
    11. <artifactId>springfox-swagger2</artifactId>
    12. <version>2.9.2</version>
    13. </dependency>
    14. <dependency>
    15. <groupId>io.springfox</groupId>
    16. <artifactId>springfox-swagger-ui</artifactId>
    17. <version>2.9.2</version>
    18. </dependency>
    19. <dependency>
    20. <groupId>com.baomidou</groupId>
    21. <artifactId>mybatis-plus</artifactId>
    22. <version>3.0.5</version>
    23. </dependency>
    24. <dependency>
    25. <groupId>com.alibaba</groupId>
    26. <artifactId>druid</artifactId>
    27. <version>1.1.22</version>
    28. </dependency>
    29. <dependency>
    30. <groupId>mysql</groupId>
    31. <artifactId>mysql-connector-java</artifactId>
    32. <version>8.0.11</version>
    33. </dependency>
    34. <dependency>
    35. <groupId>org.springframework</groupId>
    36. <artifactId>spring-context</artifactId>
    37. <version>5.2.8.RELEASE</version>
    38. </dependency>
    39. <dependency>
    40. <groupId>org.springframework</groupId>
    41. <artifactId>spring-jdbc</artifactId>
    42. <version>5.2.8.RELEASE</version>
    43. </dependency>
    44. <dependency>
    45. <groupId>org.projectlombok</groupId>
    46. <artifactId>lombok</artifactId>
    47. <version>1.18.18</version>
    48. </dependency>
    49. <dependency>
    50. <groupId>junit</groupId>
    51. <artifactId>junit</artifactId>
    52. <version>4.12</version>
    53. </dependency>
    54. </dependencies>

    编写自动代码生成的工具类,进行相应的配置

    1. /**
    2. * 代码自动生成器
    3. */
    4. public class MybatisPlusAutoCode {
    5. public static void main(String[] args) {
    6. //构建代码自动生成器对象
    7. AutoGenerator autoGenerator = new AutoGenerator();
    8. //配置自动生成策略
    9. // 1、全局配置:
    10. //https://baomidou.com/config/generator-config.html#%E5%85%A8%E5%B1%80%E7%AD%96%E7%95%A5-globalconfig-%E9%85%8D%E7%BD%AE
    11. GlobalConfig gc = new GlobalConfig();
    12. String projectPath = System.getProperty("user.dir"); //获取当前项目所在目录,里面的user.dir是固定写法
    13. gc.setOutputDir(projectPath+"/src/main/java"); //自定义代码生成后的存放目录
    14. gc.setAuthor("caixukun"); //设置项目作者
    15. gc.setOpen(false); //代码生成后是否打开文件夹
    16. gc.setFileOverride(false); //是否覆盖
    17. gc.setServiceName("%sService"); //去Service的I前缀
    18. gc.setIdType(IdType.ID_WORKER); //自定义主键生成策略
    19. gc.setDateType(DateType.ONLY_DATE); //自定义日期类型
    20. gc.setSwagger2(true); //实体使用swagger2注解
    21. autoGenerator.setGlobalConfig(gc); //添加全局配置
    22. //2、设置数据源:
    23. // https://baomidou.com/config/generator-config.html#%E6%95%B0%E6%8D%AE%E6%BA%90-datasourceconfig-%E9%85%8D%E7%BD%AE
    24. DataSourceConfig dsc = new DataSourceConfig();
    25. dsc.setUrl("jdbc:mysql://localhost:3306/mall?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false");
    26. dsc.setDriverName("com.mysql.jdbc.Driver");
    27. dsc.setUsername("root");
    28. dsc.setPassword("root");
    29. dsc.setDbType(DbType.MYSQL); //指定数据库类型
    30. autoGenerator.setDataSource(dsc); //添加数据源配置
    31. //3、包名配置:
    32. // https://baomidou.com/config/generator-config.html#%E5%8C%85%E5%90%8D%E9%85%8D%E7%BD%AE
    33. PackageConfig pc = new PackageConfig();
    34. //pc.setModuleName("rbac"); //指定生成的模块名称
    35. pc.setParent("com.woniuxy"); //设置模块中的父目录名
    36. pc.setEntity("model"); //设置实体类目录名
    37. pc.setMapper("mapper"); //设置mapper目录名
    38. pc.setService("service"); //设置service目录名
    39. pc.setController("controller"); //设置controller目录名
    40. autoGenerator.setPackageInfo(pc);
    41. //4、数据库表配置:
    42. // https://baomidou.com/config/generator-config.html#%E6%95%B0%E6%8D%AE%E5%BA%93%E8%A1%A8%E9%85%8D%E7%BD%AE
    43. StrategyConfig strategy = new StrategyConfig();
    44. // 设置要生成的实体类对应映射的表名
    45. strategy.setInclude("t_goods","t_cart","t_user","t_order","t_order_item");
    46. strategy.setTablePrefix("t_"); //去除表名前缀
    47. //设置表名生成策略,下划线转驼峰
    48. strategy.setNaming(NamingStrategy.underline_to_camel);
    49. //设置列名生成策略,下划线转驼峰
    50. strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    51. strategy.setEntityLombokModel(true); //自动lombok;
    52. //strategy.setLogicDeleteFieldName("deleted"); //设置使用逻辑删除策略的属性名
    53. // 自动填充配置 TableFill
    54. //TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
    55. //TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
    56. //ArrayList<TableFill> tableFills = new ArrayList<>();
    57. //tableFills.add(gmtCreate);
    58. //tableFills.add(gmtModified);
    59. //strategy.setTableFillList(tableFills);
    60. //strategy.setVersionFieldName("version"); // 乐观锁
    61. strategy.setRestControllerStyle(true); //生成 @RestController 控制器
    62. strategy.setControllerMappingHyphenStyle(true); //驼峰转连字符--->localhost:8080/hello_id_2
    63. autoGenerator.setStrategy(strategy);
    64. //执行自动生成
    65. autoGenerator.execute();
    66. }
    67. }