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.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
/**
* mysql 代码生成器
*/
public class Generator {
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
//文件输出目录的模块或者项目的地址
String projectPath = "D:/test";
//生成文件的输出目录
gc.setOutputDir(projectPath + "/yeb-generator/src/main/java");
//作者
gc.setAuthor("author");
//是否覆蓋已有文件 默认值:false
gc.setFileOverride(true);
//是否打开输出目录 默认值:true
gc.setOpen(false);
//开启 baseColumnList 默认false
gc.setBaseColumnList(true);
//开启 BaseResultMap 默认false
gc.setBaseResultMap(true);
//实体命名方式 默认值:null 例如:%sEntity 生成 UserEntity
//gc.setEntityName("%sEntity");
//mapper 命名方式 默认值:null 例如:%sDao 生成 UserDao
gc.setMapperName("%sMapper");
//Mapper xml 命名方式 默认值:null 例如:%sDao 生成 UserDao.xml
gc.setXmlName("%sMapper");
//service 命名方式 默认值:null 例如:%sBusiness 生成 UserBusiness
gc.setServiceName("%sService");
//service impl 命名方式 默认值:null 例如:%sBusinessImpl 生成 UserBusinessImpl
gc.setServiceImplName("%sServiceImpl");
//controller 命名方式 默认值:null 例如:%sAction 生成 UserAction
gc.setControllerName("%sController");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true");
// dsc.setSchemaName("public");
//dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("username");
dsc.setPassword("password");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
String name = "com.test";
//自定义包配置
pc.setParent(name);
pc.setModuleName(null);
pc.setMapper("dao.test");
pc.setEntity("entity.test");
pc.setService("service.test");
pc.setServiceImpl("serviceimpl.test");
pc.setController("controller.test");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输入文件名称
return projectPath + "/yeb-generator/src/main/resources/mapper/" + "/test/" +
tableInfo.getEntityName() + "Mapper" +
StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
mpg.setTemplate(new TemplateConfig().setXml(null));
// 策略配置 数据库表配置,通过该配置,可指定需要生成哪些表或者排除哪些表
StrategyConfig strategy = new StrategyConfig();
//表名生成策略
strategy.setNaming(NamingStrategy.underline_to_camel);
//数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// 全局大写命名 ORACLE 注意
//strategy.setCapitalMode(true);
//表前缀
//strategy.setTablePrefix("prefix");
//自定义继承的Entity类全称,带包名
//strategy.setSuperEntityClass("com.test.domain");
//自定义实体,公共字段
//strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
//【实体】是否为lombok模型(默认 false
strategy.setEntityLombokModel(true);
//生成 @RestController 控制器
strategy.setRestControllerStyle(true);
//自定义继承的Controller类全称,带包名
//strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
//需要包含的表名,允许正则表达式(与exclude二选一配置)
String tables = "test";
String[] num = tables.split(",");
// 需要生成的表可以多张表
strategy.setInclude(num);
// 排除生成的表
//strategy.setExclude(new String[]{"test"});
//如果数据库有前缀,生成文件时是否要前缀acl_
//strategy.setTablePrefix("sys_");
//驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
//是否生成实体时,生成字段注解
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
mysql代码生成
最新推荐文章于 2023-11-21 10:14:35 发布