代码生成器在每个service子模块的test中都可以创建一个,记得更改包和数据库配置。
使用Mybatis-Plus的代码生成器需要添加如下依赖,放在所有service
的父模块
pom.xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.4.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.0</version> <scope>test</scope> </dependency>
|
工具类
注意包生成的路径和包名即可
public class CodeGenerate { public static void OnMac() { AutoGenerator mpg = new AutoGenerator();
GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setAuthor("sw-code"); gc.setOpen(false); gc.setFileOverride(true); gc.setServiceName("%sService"); gc.setSwagger2(false); gc.setIdType(IdType.AUTO); gc.setDateType(DateType.ONLY_DATE); mpg.setGlobalConfig(gc);
DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql:///leadnews_schedule?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8&nullCatalogMeansCurrent=true"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("swx852345"); mpg.setDataSource(dsc);
PackageConfig pc = new PackageConfig(); pc.setParent("com.swx"); Map<String, String> pathInfo = new HashMap<>(); String packageName = "schedule"; String currentModulePath = "/leadnews-service/leadnews-schedule"; String parentPath = "/src/main/java/com/swx"; String otherPath = currentModulePath + "/src/main/java/com/swx/" + packageName;
String entityPackageName = "schedule"; pc.setEntity("model." + entityPackageName + ".pojo"); pc.setMapper(packageName + ".mapper"); pc.setService(packageName + ".service"); pc.setServiceImpl(packageName + ".service.impl"); pc.setController(packageName + ".controller.v1");
String entityPath = projectPath.concat("/leadnews-model").concat(parentPath).concat("/model/" + entityPackageName + "/pojo"); String mapper_path = projectPath.concat(otherPath).concat("/mapper"); String mapper_xml_path = projectPath.concat(currentModulePath).concat("/src/main/resources/mapper"); String service_path = projectPath.concat(otherPath).concat("/service"); String service_impl_path = projectPath.concat(otherPath).concat("/service/impl"); String controller_path = projectPath.concat(otherPath).concat("/controller/v1");
pathInfo.put("entity_path",entityPath); pathInfo.put("mapper_path",mapper_path); pathInfo.put("xml_path",mapper_xml_path); pathInfo.put("service_path",service_path); pathInfo.put("service_impl_path",service_impl_path); pathInfo.put("controller_path",controller_path); pc.setPathInfo(pathInfo); mpg.setPackageInfo(pc);
StrategyConfig strategy = new StrategyConfig(); strategy.setInclude("taskinfo", "taskinfo_logs"); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setEntityTableFieldAnnotationEnable(true); strategy.setRestControllerStyle(true); strategy.setControllerMappingHyphenStyle(true); mpg.setStrategy(strategy);
mpg.execute(); }
public static void main(String[] args) { OnMac(); } }
|