代码生成器在每个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"); // 去Service的I前缀
gc.setSwagger2(false); // 实体属性 Swagger2 注解
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.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("swx852345");
mpg.setDataSource(dsc);


/*
* 包配置
* 简单来讲 就是写绝对路径
*/
PackageConfig pc = new PackageConfig();
// pc.setModuleName("code");
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();
}
}