@SpringBootTest class PlusApplicationTests { @Test void GenCode(){ AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor("ZhuSea"); gc.setOpen(false); gc.setIdType(IdType.ID_WORKER); // 设置id的自动填充模式 gc.setDateType(DateType.ONLY_DATE); // 设置时间格式 gc.setServiceName("%sService"); // 去Service的I前缀 gc.setSwagger2(true); // 实体属性 Swagger2 注解 mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://39.107.136.20:13306/admin?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("123456"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setModuleName("gen"); pc.setParent("com.zhuhai"); pc.setController("controller"); pc.setMapper("mapper"); pc.setEntity("pojo"); pc.setService("service"); pc.setXml("mapper"); pc.setServiceImpl("service.impl"); mpg.setPackageInfo(pc); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setInclude("blog_tags","course","links","sys_settings","user_record"," user_say"); // 设置要映射的表名 strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); strategy.setSuperEntityColumns("id"); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix(pc.getModuleName() + "_"); strategy.setControllerMappingHyphenStyle(true); strategy.setLogicDeleteFieldName("deleted");// 逻辑删除
// 自动填充时间配置策略 TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT); TableFill gmtModified = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE); ArrayList<TableFill> tableFills = new ArrayList<>(); tableFills.add(gmtCreate); tableFills.add(gmtModified); strategy.setTableFillList(tableFills); strategy.setVersionFieldName("version");// 乐观锁 mpg.setStrategy(strategy); mpg.execute(); } }