package com.riskeys.pic.dbconfig.config;
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.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author: zncl
* @Description: mybatis plus 反向生成
* @date 2020/3/11
* @Version:1.0
*/
public class CodeGenerator {
public static void main(String[] args) {
// 数据源配置
String jdbcUrl = "jdbc:mysql://120.26.197.254:3306/db_name?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai";
String userName = "userName ";
String password = "password";
//项目名称,例:pic-product:product
String projectName = "policyservice";
// 生成代码的 @author 值
String author = "Ken";
// 要生成代码的表名配置
String[] tables = {"t_a_wechat_attention", "t_a_wechat_attention_user"};
generatorFile(jdbcUrl, userName, password, tables, projectName, author);
}
/**
* mybatis-plus 反向文件生成
*
* @param url 数据库连接地址
* @param userName 用户名
* @param password 密码
* @param tables 表名
* @param projectName 项目名
*/
private static void generatorFile(String url, String userName, String password, String[] tables, String projectName, String author) {
//项目路径
String projectPath = "/pic-" + projectName + "/src/main";
AutoGenerator mpg = new AutoGenerator();
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(url);
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername(userName);
dsc.setPassword(password);
mpg.setDataSource(dsc);
// 全局配置
GlobalConfig gc = new GlobalConfig();
String dirPath = System.getProperty("user.dir");
gc.setOutputDir(dirPath + projectPath + "/java");
gc.setAuthor(author);
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
gc.setFileOverride(true);
// 生成完毕后是否打开输出目录
gc.setOpen(false);
// 为true时生成entity将继承Model类,单类即可完成基于单表的业务逻辑操作,按需开启
gc.setActiveRecord(true);
// 自定义文件命名,注意 %s 会自动填充表实体属性
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setEntityName("%sBean");
mpg.setGlobalConfig(gc);
// 包配置
PackageConfig pc = new PackageConfig();
// 父级包名配置
pc.setParent("com.riskeys.pic." + projectName);
// 设置模块名, 会在parent包下生成一个指定的模块包
pc.setMapper("dao.primary");
pc.setService("service.baseservice");
pc.setServiceImpl("service.baseservice.impl");
pc.setEntity("model.bean");
mpg.setPackageInfo(pc);
// 注入自定义配置
InjectionConfig injectionConfig = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<>();
map.put("injectionConfig", this.getConfig().getGlobalConfig().getAuthor());
this.setMap(map);
}
};
//自定义文件输出位置
List<FileOutConfig> fileOutList = new ArrayList<>();
//mapper.xml
String templatePath = "/templates/mapper.xml.ftl";
fileOutList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
String entityName = tableInfo.getEntityName().contains("Bean") ? tableInfo.getEntityName().replace("Bean", "") : tableInfo.getEntityName();
return dirPath + projectPath + "/resources/mybatis/primary/" + entityName + "Mapper.xml";
}
});
injectionConfig.setFileOutConfigList(fileOutList);
mpg.setCfg(injectionConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setRestControllerStyle(false);
strategy.setInclude(tables);
strategy.setSuperEntityColumns("id");
// Controller驼峰连字符,如开启,则requestMapping由 helloWorld 变为 hello-world 默认false
strategy.setControllerMappingHyphenStyle(false);
strategy.setTablePrefix(pc.getModuleName() + "_");
// 开启后将使用lombok注解代替set-get方法,false则生成set-get方法
strategy.setEntityLombokModel(true);
// 在实体类中移除is前缀
strategy.setEntityBooleanColumnRemoveIsPrefix(true);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
/**
* 模板配置
*/
mpg.setTemplate(
// 关闭默认 xml 生成,调整生成 至 根目录
new TemplateConfig().setController(null)
.setXml(null)
);
mpg.execute();
}
}
Mybaits 反向生成pojo工具类
最新推荐文章于 2024-03-14 11:48:22 发布