地址: https://github.com/RevoSith/tool/
入口类:GeneratorCore -> GeneratorV2
优点:
- 无他,定制化方便一点.
使用步骤 (GeneratorCore)
- 配置数据库连接
- 配置生成的文件相对路径
- 选择需要使用的插件(可以全都不要)
- 配置表信息
- 执行
插件
- Lombok的data标签
- tkMapper的定制化
- 自定义DTO
- 可以自己定制各种service
插件是选择配置的
public class GeneratorCore {
public static void main(String[] args) throws Exception {
GeneratorBaseConf conf = new GeneratorBaseConf();
//连接配置
databaseConf(conf);
//文件路径配置
classConf(conf);
//插件配置
pluginConf(conf);
//表配置 & 执行
GeneratorV2.execute(conf, getTableConfList());
}
private static List<TableConf> getTableConfList() {
List<TableConf> tableConfList = new ArrayList<>();
List<String> tables = Arrays.asList(
"xxxxxxx");
for (String str : tables) {
TableConf tableConf = new TableConf();
tableConf.setTableName(str);
tableConf.setPk("id");
//selectKey的读取. pre or post
tableConf.setType("pre");
tableConfList.add(tableConf);
}
return tableConfList;
}
/**
* 数据库连接配置
*
* @param conf
*/
private static void databaseConf(GeneratorBaseConf conf) {
conf.setConnectionURL("jdbc:mysql://xxxxxxxxxxxxxxxx:3306/xxxxxxxx?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&useServerPreparedStmts=true");
conf.setUserId("xxxxx");
conf.setPassWord("xxxxxx");
}
/**
* 文件路径配置
*
* @param conf
*/
private static void classConf(GeneratorBaseConf conf) {
conf.setDomainPackage("com.revosith.test.domain");
conf.setMapperPackage("com.revosith.test.mapper");
conf.setModule("test-core");
}
/**
* 插件配置
*
* @param conf
*/
private static void pluginConf(GeneratorBaseConf conf) {
//序列化插件
conf.getPlugins().add(new SerializablePlugin());
//xml 覆写插件
conf.getPlugins().add(new OverwriteXmlPlugin());
//lombok 插件
conf.getPlugins().add(new LombokPlugin());
//tk Mapper 插件
tkPlugin(conf);
//dto 文件插件
simpleDTOFilePlugin(conf);
}
private static void tkPlugin(GeneratorBaseConf conf){
//tk Mapper 插件
MapperPlugin mapperPlugin = new MapperPlugin();
Properties properties = mapperPlugin.getProperties();
properties.setProperty("mappers", "tk.mybatis.mapper.common.Mapper");
conf.getPlugins().add(mapperPlugin);
}
private static void simpleDTOFilePlugin(GeneratorBaseConf conf){
SimpleDTOFilePlugin simpleDTOFilePlugin = new SimpleDTOFilePlugin();
Properties simpleDTOFilePluginProperties = simpleDTOFilePlugin.getProperties();
simpleDTOFilePluginProperties.setProperty(SimpleDTOFilePlugin.PACKAGE,"com.revosith.test.dto");
simpleDTOFilePluginProperties.setProperty(SimpleDTOFilePlugin.MODULE,"test-dal");;
//是否开启 swagger配置
// simpleDTOFilePluginProperties.setProperty(SimpleDTOFilePlugin.ADD_SWAGGER,"00");;
conf.getPlugins().add(simpleDTOFilePlugin);
}
}
public class GeneratorV2 {
private static Context CONTEXT = new Context(ModelType.CONDITIONAL);
/**
* 执行生成
*
* @throws Exception
*/
public static void execute(GeneratorBaseConf dataBaseConf, List<TableConf> tableConfList) throws Exception {
Configuration config = new Configuration();
config.addContext(CONTEXT);
//基础参数配置
propertyConf(dataBaseConf);
//数据库连接配置
jdbcConnectionConfiguration(dataBaseConf);
//基础文件配置 dao entity xmlMapper
baseClassGenerator(dataBaseConf);
//表配置载入
loadTableConf(tableConfList);
//插件配置
addPlugin(dataBaseConf);
//创建 MBG
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, new DefaultShellCallback(true), null);
//执行生成代码
myBatisGenerator.generate(null);
}
/**
* 基础参数配置
* @param dataBaseConf 数据库配置
*/
private static void propertyConf(GeneratorBaseConf dataBaseConf) {
//context id 一个库一个id
CONTEXT.setId("default");
CONTEXT.setTargetRuntime("MyBatis3Simple");
//分割符 ORACLE是双引号,MYSQL默认是`反引号
CONTEXT.addProperty("beginningDelimiter", "`");
CONTEXT.addProperty("endingDelimiter", "`");
//格式化代码
CONTEXT.addProperty("javaFormatter", "org.mybatis.generator.api.dom.DefaultJavaFormatter");
CONTEXT.addProperty("xmlFormatter", "org.mybatis.generator.api.dom.DefaultXmlFormatter");
CONTEXT.addProperty("javaFileEncoding", "utf-8");
CONTEXT.addProperty("autoDelimitKeywords", "true");
}
/**
* dao,entity, xmlMapper
* @param dataBaseConf 数据库配置
*/
private static void baseClassGenerator(GeneratorBaseConf dataBaseConf) {
//dao 配置
CONTEXT.setJavaClientGeneratorConfiguration(ClassConf.clientGenerator(dataBaseConf));
//entity 配置
CONTEXT.setJavaModelGeneratorConfiguration(ClassConf.modelGenerator(dataBaseConf));
//entity字段的类型特殊转换配置 比如tinyint 我映射成了 Integer
CONTEXT.setJavaTypeResolverConfiguration(ClassConf.javaTypeResolverConfiguration());
//xml 配置
CONTEXT.setSqlMapGeneratorConfiguration(ClassConf.sqlMapGeneratorConfiguration(dataBaseConf));
}
/**
* 数据库配置
*/
private static void jdbcConnectionConfiguration(GeneratorBaseConf dataBaseConf) {
JDBCConnectionConfiguration config = new JDBCConnectionConfiguration();
config.setConnectionURL(dataBaseConf.getConnectionURL());
config.setDriverClass(dataBaseConf.getDriver());
config.setUserId(dataBaseConf.getUserId());
config.setPassword(dataBaseConf.getPassWord());
CONTEXT.setJdbcConnectionConfiguration(config);
}
/**
* 添加插件
*/
private static void addPlugin(GeneratorBaseConf dataBaseConf) {
if (CollectionUtils.isEmpty(dataBaseConf.getPlugins())) {
return;
}
for (PluginAdapter plugin : dataBaseConf.getPlugins()) {
PluginConfiguration configSerialize = new PluginConfiguration();
configSerialize.setConfigurationType(plugin.getClass().getName());
for (String name : plugin.getProperties().stringPropertyNames()) {
configSerialize.addProperty(name, plugin.getProperties().getProperty(name));
}
CONTEXT.addPluginConfiguration(configSerialize);
}
}
/**
* 载入表配置
*/
private static void loadTableConf(List<TableConf> tableConfList) {
for (TableConf conf : tableConfList) {
TableConfiguration tempConfig = new TableConfiguration(CONTEXT);
//设置表名
tempConfig.setTableName(conf.getTableName());
//对象名
tempConfig.setDomainObjectName(StringUtils.getStrWithRank(conf.getDomainName(), FieldTool.changeToJavaFiled(conf.getTableName())));
//mapper
tempConfig.setMapperName(StringUtils.getStrWithRank(conf.getMapperName(), FieldTool.changeToJavaFiled(conf.getTableName()) + "Mapper"));
//主键
GeneratedKey generatedKey = new GeneratedKey(conf.getPk(), "Mysql", false, conf.getType());
//主键
tempConfig.setGeneratedKey(generatedKey);
tempConfig.addProperty("useActualColumnNames", "false");
CONTEXT.addTableConfiguration(tempConfig);
}
}
}