mybatisplus代码生成器3.5.2
Mybatis-plus最新代码生成器(3.5.1+)的使用mybatis-plus 代码生成器(3.5.2)@Autowiredprivate CodeGeneratorMapper codeGeneratorMapper;//ip地址private String DB_URL = "";//用户名private String DB_USERNAME = "";//密码private Strin
·
Mybatis-plus最新代码生成器(3.5.1+)的使用
@Autowired
private CodeGeneratorMapper codeGeneratorMapper;
//ip地址
private String DB_URL = "";
//用户名
private String DB_USERNAME = "";
//密码
private String DB_PASSWD = "";
//作者
private String DB_AUTHOR = "";
//表集合
private String DB_TABLE_LIST = "";
//数据连接信息
private DataSourceConfig dataSourceConfig() {
return new DataSourceConfig()
.setDbType(DbType.MYSQL)
.setUrl(DB_URL)
.setUsername(DB_USERNAME)
.setPassword(DB_PASSWD)
.setDriverName(GeneratorConfig.DRIVER);
}
// 配置
private GlobalConfig globalConfig() {
return new GlobalConfig()
.setAuthor(DB_AUTHOR)
.setOutputDir(GeneratorConfig.outputDir)
.setFileOverride(true) // 是否覆盖已有文件
.setOpen(false) // 是否打开输出目录
.setDateType(DateType.ONLY_DATE) // 时间采用java 8,(操作工具类:JavaLib => DateTimeUtils)
.setEnableCache(false)// XML 二级缓存
.setBaseResultMap(false)// XML ResultMap
.setBaseColumnList(false)// XML columList
.setKotlin(false) //是否生成 kotlin 代码
.setMapperName(GeneratorConfig.FILE_NAME_DAO) //自定义文件命名,注意 %s 会自动填充表实体属性!
.setXmlName(GeneratorConfig.FILE_NAME_XML)
.setServiceName(GeneratorConfig.FILE_NAME_SERVICE)
.setServiceImplName(GeneratorConfig.FILE_NAME_SERVICE_IMPL)
.setControllerName(GeneratorConfig.FILE_NAME_CONTROLLER)
.setSwagger2(GeneratorConfig.SWAGGER_SUPPORT);
}
//实体类配置
private StrategyConfig strategyConfig() {
return new StrategyConfig()
.setChainModel(true) // 【实体】是否为构建者模型(默认 false)
.setNaming(NamingStrategy.underline_to_camel)
.setColumnNaming(NamingStrategy.underline_to_camel)
// .setSuperEntityClass(BaseEntity.class)
.setEntityLombokModel(true)
.setRestControllerStyle(true)
.setInclude(DB_TABLE_LIST.split(","))
.setControllerMappingHyphenStyle(false)
.setEntityTableFieldAnnotationEnable(true) //是否生成实体时,生成字段注解,包括@TableName("")
;
}
// 包信息配置
private PackageConfig packageConfig() {
return new PackageConfig()
// .setModuleName(scanner("ss"))
.setParent(GeneratorConfig.PACKAGE_PARENT)
// 如果是单体项目请注释掉 .setModuleName(scanner("模块名"))即可,把下面注释取消
.setController(GeneratorConfig.PACKAGE_NAME_CONTROLLER)
.setEntity(GeneratorConfig.PACKAGE_NAME_MODEL)
.setMapper(GeneratorConfig.PACKAGE_NAME_DAO)
.setService(GeneratorConfig.PACKAGE_NAME_SERVICE)
.setServiceImpl(GeneratorConfig.PACKAGE_NAME_SERVICE_IMPL);
}
private InjectionConfig injectionConfig() {
InjectionConfig injectionConfig = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 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 GeneratorConfig.projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
injectionConfig.setFileOutConfigList(focList);
return injectionConfig;
}
private TemplateConfig templateConfig() {
TemplateConfig templateConfig = new TemplateConfig();
//配置自定义输出模板,指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
templateConfig.setController("templates/controllerGenerator.java");
templateConfig.setServiceImpl("templates/serviceImplGenerator.java");
templateConfig.setMapper("templates/mapperGenerator.java");
templateConfig.setXml(null);
return templateConfig;
}
// 获取模板引擎
private AbstractTemplateEngine getTemplateEngine() {
String templateEngine = GeneratorConfig.TEMPLATE_ENGINE;
switch (templateEngine) {
case "velocity":
return new VelocityTemplateEngine();
case "freemarker":
return new FreemarkerTemplateEngine();
case "beetl":
return new BeetlTemplateEngine();
default:
return new VelocityTemplateEngine();
}
}
// 执行器
public Boolean execute(CodeGeneratorResult codeGeneratorResult) {
//执行生成代码之前,先初始化数据库信息
initMsg(codeGeneratorResult);
GlobalConfig globalConfig = globalConfig();
DataSourceConfig dataSourceConfig = dataSourceConfig();
PackageConfig packageConfig = packageConfig();
StrategyConfig strategyConfig = strategyConfig();
InjectionConfig injectionConfig = injectionConfig();
AbstractTemplateEngine templateEngine = getTemplateEngine();
new AutoGenerator()
.setGlobalConfig(globalConfig)
.setDataSource(dataSourceConfig)
.setStrategy(strategyConfig)
.setPackageInfo(packageConfig)
.setTemplateEngine(templateEngine)
.setCfg(injectionConfig)
.setTemplate(templateConfig())
.execute();
//更新数据库连接
LambdaUpdateWrapper<CodeGenerator> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
lambdaUpdateWrapper.eq(CodeGenerator::getId, 1)
.set(CodeGenerator::getDbName, codeGeneratorResult.getDbName())
.set(CodeGenerator::getDbUrl, IpUtils.obtainIp(codeGeneratorResult.getDbUrl()))
.set(CodeGenerator::getTableName, codeGeneratorResult.getTableName())
.set(CodeGenerator::getUserName, codeGeneratorResult.getUserName())
.set(CodeGenerator::getPassword, codeGeneratorResult.getPassword())
.set(CodeGenerator::getAuthor, codeGeneratorResult.getAuthor());
codeGeneratorMapper.update(null, lambdaUpdateWrapper);
return true;
}
//执行生成代码之前,先初始化数据库信息
public void initMsg(CodeGeneratorResult codeGeneratorResult) {
this.DB_URL = codeGeneratorResult.getDbUrl();
this.DB_USERNAME = codeGeneratorResult.getUserName();
this.DB_PASSWD = codeGeneratorResult.getPassword();
this.DB_AUTHOR = codeGeneratorResult.getAuthor();
this.DB_TABLE_LIST = codeGeneratorResult.getTableName();
}
更多推荐
已为社区贡献41条内容
所有评论(0)