封装代码生成器
Posted 伍妖捌
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了封装代码生成器相关的知识,希望对你有一定的参考价值。
前言
在项目开发过程中,实体类是必不可少的,并且是大量重复性工作,为了节约时间,通常通过代码生成器来自动生成代码,开发人员只需要开发业务逻辑即可。下面通过使用mybatis-plus代码生成器、以及连接mysql数据库来实现自动代码生成功能。
配置类
这里我将需要修改的属性抽取出来
@Data
public class AutoCodeConfigure {
// 模块名
private String moduleName = "demo";
// 包名
private String packageConfig = "com.dulang";
// 作者名
private String author = "dulang";
// 数据库ip地址
private String dataBaseIP = "127.0.0.1";
// 数据库名
private String dataBaseName = "demo";
// 数据库用户名
private String username = "root";
// 数据库密码
private String password = "123456";
}
生成器
@Data
public class AutoCodeGenerator {
private AutoCodeConfigure autoCodeConfigure;
private GlobalConfig globalConfig;
private DataSourceConfig dataSourceConfig;
private PackageConfig packageConfig;
private StrategyConfig strategy;
public void execute() {
// Step1: 创建代码生成器
AutoGenerator mpg = new AutoGenerator();
// Step2:全局配置
mpg.setGlobalConfig(globalConfig);
// Step3:数据源配置
mpg.setDataSource(dataSourceConfig);
// Step:4:包配置
mpg.setPackageInfo(packageConfig);
// Step5:策略配置(数据库表配置)
mpg.setStrategy(strategy);
// Step6:执行代码生成操作
mpg.execute();
}
public AutoCodeGenerator() {
this.autoCodeConfigure = new AutoCodeConfigure();
this.globalConfig = initGlobalConfig();
this.dataSourceConfig = initDataSourceConfig();
this.packageConfig = initPackageConfig();
this.strategy = initStrategyConfig();
}
public AutoCodeGenerator(AutoCodeConfigure autoCodeConfigure) {
this.autoCodeConfigure = autoCodeConfigure;
this.globalConfig = initGlobalConfig();
this.dataSourceConfig = initDataSourceConfig();
this.packageConfig = initPackageConfig();
this.strategy = initStrategyConfig();
}
private GlobalConfig initGlobalConfig() {
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir(System.getProperty("user.dir") + "\\\\src\\\\main\\\\java");
globalConfig.setAuthor(autoCodeConfigure.getAuthor());
globalConfig.setOpen(false);
globalConfig.setSwagger2(true);
globalConfig.setFileOverride(false);
globalConfig.setDateType(DateType.ONLY_DATE);
globalConfig.setActiveRecord(false);
globalConfig.setBaseResultMap(true);
globalConfig.setBaseColumnList(true);
globalConfig.setServiceName("%sService");
return globalConfig;
}
private DataSourceConfig initDataSourceConfig() {
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setUrl("jdbc:mysql://" + autoCodeConfigure.getDataBaseIP() + ":3306/" + autoCodeConfigure
.getDataBaseName() + "?useUnicode=true&characterEncoding=UTF-8" +
"&serverTimezone=GMT%2B8");
dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
dataSourceConfig.setUsername(autoCodeConfigure.getUsername());
dataSourceConfig.setPassword(autoCodeConfigure.getPassword());
return dataSourceConfig;
}
private PackageConfig initPackageConfig() {
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent(autoCodeConfigure.getPackageConfig());
packageConfig.setModuleName(autoCodeConfigure.getModuleName());
packageConfig.setEntity("model");
packageConfig.setMapper("mapper");
packageConfig.setService("service");
packageConfig.setServiceImpl("service.impl");
packageConfig.setController("controller");
packageConfig.setPathInfo(new HashMap<String, String>() {
String path = (globalConfig.getOutputDir() + "." + packageConfig.getParent()).replace(".", "\\\\");
{
put(ConstVal.XML_PATH, System.getProperty("user.dir") + "\\\\src\\\\main\\\\resources\\\\mapper");
put(ConstVal.ENTITY_PATH, path + "\\\\model");
put(ConstVal.MAPPER_PATH, path + "\\\\mapper");
put(ConstVal.SERVICE_PATH, path + "\\\\service");
put(ConstVal.SERVICE_IMPL_PATH, path + "\\\\service\\\\impl");
put(ConstVal.CONTROLLER_PATH, path + "\\\\controller");
}
});
return packageConfig;
}
private StrategyConfig initStrategyConfig() {
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude(scanner());
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
// strategy.setVersionFieldName("version");
strategy.setLogicDeleteFieldName("is_deleted");
strategy.setEntityBooleanColumnRemoveIsPrefix(true);
// strategy.setTableFillList(Lists.newArrayList(new TableFill("create_time", FieldFill.INSERT),
// new TableFill("update_time", FieldFill.INSERT_UPDATE)));
strategy.setRestControllerStyle(true);
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(packageConfig.getModuleName() + "_");
// 使用过程中有bug,目前注释
// strategy.setSuperEntityClass(autoCodeConfigure.getPackageConfig() + ".common.model.BaseModel");
// strategy.setSuperEntityColumns("id", "create_time", "update_time");
return strategy;
}
/**
* 读取控制台内容
*/
private static String scanner() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入表名:");
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的表名!");
}
}
依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
</dependency>
使用
在任意类中创建main方法添加以下代码,运行即可
public static void main(String[] args) {
AutoCodeConfigure autoCodeConfigure = new AutoCodeConfigure();
autoCodeConfigure.setModuleName("test");
autoCodeConfigure.setPackageConfig("com.auto");
AutoCodeGenerator generator = new AutoCodeGenerator(autoCodeConfigure);
generator.execute();
}
以上是关于封装代码生成器的主要内容,如果未能解决你的问题,请参考以下文章