import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
/**
* 代码生成工具
*
* @author: hzq
* @date: 2022/4/26
*/
public class CodeGeneratorTest extends ApplicationTest {
@Test
public void generator() {
// 作者名称
String author = "hzq";
// model名称 不分model则为空字符串
// 例如 model名称为[order] 路径则为 [/order] 包名则为 [.order]
// controller 生成在 com.controller.order 包下
// service 生成在 com.service.order 包下
// serviceImpl 生成在 com.service.order.impl 包下
// entity 生成在 com.domain.entity.order 包下
// mapper 生成在 com.mapper.order 包下
// xml 生成在 resource/mapper/order 文件下
String modelPath = "/mediate";
String modelName = ".mediate";
// 表列表
String[] includes = {"mediate_sms_template"};
generator(author, modelPath, modelName, includes);
}
/**
* 目前只支持生成在 zw-backend 模块下
* 如需生成在其他模块请修改生成路径
*
* @param author 作者
* @param modelPath 模块路径
* @param modelName 模块包
* @param includes 要生成表
*/
public void generator(String author, String modelPath, String modelName, String[] includes) {
/*包路径*/
String userDirPath = System.getProperty("user.dir");
String basePackagePath = "/src/main/java/com";
String entityPackagePath = userDirPath + basePackagePath + "/domain/entity" + modelPath;
String controllerPackagePath = userDirPath + basePackagePath + "/controller" + modelPath;
String servicePackagePath = userDirPath + basePackagePath + "/service" + modelPath;
String serviceImplPackagePath = userDirPath + basePackagePath + "/service" + modelPath + "/impl";
String mapperPackagePath = userDirPath + basePackagePath + "/mapper" + modelPath;
// mapper xml 生成在resource下面
String mapperXmlPackagePath = userDirPath + "/src/main/resources/mapper" + modelPath;
/*包名称*/
String basePackage = "com";
String entityPackage = basePackage + ".domain.entity" + modelName;
String controllerPackage = basePackage + ".controller" + modelName;
String servicePackage = basePackage + ".service" + modelName;
String serviceImplPackage = basePackage + ".service" + modelName + ".impl";
String mapperPackage = basePackage + ".mapper" + modelName;
// xml 生成在resource下面 所以不配置包名
// String mapperXmlPackage = basePackage + ".mapper" + modelName + ".xml";
//1、全局配置
GlobalConfig config = new GlobalConfig();
config
.setAuthor(author)
.setOpen(false)
.setFileOverride(true)
.setBaseResultMap(true)
.setSwagger2(true)
.setServiceName("%sService")
;
//2、数据源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDbType(DbType.MYSQL)
.setDriverName("com.mysql.cj.jdbc.Driver")
.setUrl("jdbc:mysql://127.0.0.1:3306/db_name?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true")
.setUsername("username")
.setPassword("password")
;
//3、策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setCapitalMode(true)
.setNaming(NamingStrategy.underline_to_camel)
.setInclude(includes)
.setEntityLombokModel(true)
.setChainModel(true)
.setRestControllerStyle(true)
.setVersionFieldName("version")
.setLogicDeleteFieldName("del_flag")
.setSuperEntityClass(BaseEntity.class)
.setSuperControllerClass(BaseController.class)
.setSuperEntityColumns("create_by", "create_time", "update_by", "update_time")
// .setEntityTableFieldAnnotationEnable(true)
;
//4、包名策略配置 不需要生成的模块 只需注释掉路径
PackageConfig packageConfig = new PackageConfig();
Map<String, String> packageInfo = CollectionUtils.newHashMapWithExpectedSize(7);
packageInfo.put(ConstVal.ENTITY_PATH, entityPackagePath);
packageInfo.put(ConstVal.CONTROLLER_PATH, controllerPackagePath);
packageInfo.put(ConstVal.SERVICE_PATH, servicePackagePath);
packageInfo.put(ConstVal.SERVICE_IMPL_PATH, serviceImplPackagePath);
packageInfo.put(ConstVal.MAPPER_PATH, mapperPackagePath);
packageInfo.put(ConstVal.XML_PATH, mapperXmlPackagePath);
packageConfig
.setPathInfo(packageInfo)
.setParent("")
.setEntity(entityPackage)
.setController(controllerPackage)
.setService(servicePackage)
.setServiceImpl(serviceImplPackage)
.setMapper(mapperPackage)
// .setXml(mapperXmlPackage)
;
//5、配置自定义属性注入
InjectionConfig injectionConfig = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<>();
//是否使用动态数据源
map.put("useDS", true);
//动态数据源名称
map.put("DSName", "master");
this.setMap(map);
}
};
//6、整合配置
AutoGenerator autoGenerator = new AutoGenerator();
autoGenerator.setGlobalConfig(config)
.setDataSource(dataSourceConfig)
.setStrategy(strategyConfig)
// .setCfg(injectionConfig)
.setPackageInfo(packageConfig)
;
//7、执行
autoGenerator.execute();
}
}
<!--代码生成相关依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>provided</scope>
</dependency>