说明
整个项目,我打算使用MyBatis-Plus作为持久层框架
代码
创建一个chan-code-generator项目
- 核心代码:CodeGenerator
package com.zhangln.chan.gen;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import java.util.ArrayList;
import java.util.List;
/**
* @author sherry
* @description
* @date Create in 2020/8/21
* @modified By:
*/
public class CodeGenerator {
public static void main(String[] args) {
String dbDriverClass = "com.mysql.cj.jdbc.Driver";
String dbUrl = "jdbc:mysql://localhost:3306/chan_log?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=UTC";
String dbUsername = "root";
String dbPassword = "root";
String projectPath = "./";
String author = "sherry";
//模块名
String modulName = "log";
//父包
String parentPackage = "com.zhangln.chan";
//数据库schema
String scheName = "chan_log";
//需要生成的表,多张表用逗号分隔
String tables = "t00_common_log";
generator(dbDriverClass, dbUrl, dbUsername
, dbPassword, projectPath, author, modulName
, parentPackage
, scheName
, tables
);
}
private static void generator(String dbDriverClass, String dbUrl, String dbUsername
, String dbPassword, String projectPath, String author, String modulName
, String parentPackage
, String scheName
, String tableList
) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor(author);
gc.setOpen(false);
gc.setEntityName("%sEntity");
gc.setServiceName("%sDao");
gc.setServiceImplName("%sDaoImpl");
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
gc.setIdType(IdType.ASSIGN_ID);
gc.setEnableCache(false);
mpg.setGlobalConfig(gc);
// 填写表名,用逗号分隔
String[] tables = tableList.split(",");
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(dbUrl);
dsc.setSchemaName(scheName);
dsc.setDriverName(dbDriverClass);
dsc.setUsername(dbUsername);
dsc.setPassword(dbPassword);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
// 模块名
pc.setModuleName(modulName);
pc.setParent(parentPackage);
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 velocity
String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 表名
strategy.setInclude(tables);
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(pc.getModuleName() + "_");
//【实体】是否生成字段常量(默认 false)
strategy.setEntityColumnConstant(true);
strategy.setEntityBuilderModel(true);
//.setLogicDeleteFieldName("is_delete") // 逻辑删除属性名称
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new VelocityTemplateEngine());
mpg.execute();
}
}
源码
https://gitee.com/zhanglning/chan/tree/day2