public class MysqlGenerator {
public static void main(String[] args) {
/* 获取 JDBC 配置文件 */
Properties props = getProperties();
AutoGenerator mpg = new AutoGenerator();
String outputDir = "E:/code";
final String viewOutputDir = outputDir + "/view/";
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(outputDir);
gc.setFileOverride(true);
gc.setActiveRecord(true);// 开启 activeRecord 模式
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(true);// XML columList
gc.setAuthor("cdy");
// 自定义文件命名,注意 %s 会自动填充表实体属性!
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
gc.setServiceName("I%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setControllerName("%sController");
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.SQL_SERVER);
dsc.setTypeConvert(new MySqlTypeConvert());
dsc.setDriverName(props.getProperty("spring.datasource.driver-class-name"));
// dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername(props.getProperty("spring.datasource.username"));
dsc.setPassword(props.getProperty("spring.datasource.password"));
dsc.setUrl(props.getProperty("spring.datasource.url"));
mpg.setDataSource(dsc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
// strategy.setCapitalMode(true);// 全局大写命名
// strategy.setDbColumnUnderline(true);//全局下划线命名
// strategy.setTablePrefix(new String[] { "bmd_", "mp_" });// 此处可以修改为您的表前缀
strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
strategy.setInclude(new String[] {"station"}); // 需要生成的表
// strategy.setExclude(new String[]{"test"}); // 排除生成的表
// 自定义实体父类
// strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");
// 自定义实体,公共字段
// strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
// 自定义 mapper 父类
// strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
// 自定义 service 父类
// strategy.setSuperServiceClass("com.baomidou.demo.TestService");
// 自定义 service 实现类父类
// strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl");
// 自定义 controller 父类
strategy.setSuperControllerClass("com.dream.team.base.BController");
// 【实体】是否生成字段常量(默认 false)
// public static final String ID = "test_id";
// strategy.setEntityColumnConstant(true);
// 【实体】是否为构建者模型(默认 false)
// public User setName(String name) {this.name = name; return this;}
// strategy.setEntityBuliderModel(true);
mpg.setStrategy(strategy);
// 包配
PackageConfig pc = new PackageConfig();
pc.setModuleName(null); //所属模块
pc.setParent("com.dream.team"); // 自定义包路径
pc.setController("controller"); // 这里是控制器包名,默认 web
pc.setEntity("model");
pc.setXml("sqlMapperXml");
mpg.setPackageInfo(pc);
// 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {}
};
// 生成的模版路径,不存在时需要先新建
File viewDir = new File(viewOutputDir);
if (!viewDir.exists()) {
viewDir.mkdirs();
}
List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
/*focList.add(new FileOutConfig("/templates/add.jsp.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getGeneratorViewPath(viewOutputDir, tableInfo, "Add.jsp");
}
});*/
/*focList.add(new FileOutConfig("/templates/list.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getGeneratorViewPath(viewOutputDir, tableInfo, "List.js");
}
});
focList.add(new FileOutConfig("/templates/list.jsp.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getGeneratorViewPath(viewOutputDir, tableInfo, "List.jsp");
}
});*/
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 执行生成
mpg.execute();
}
/**
* 获取配置文件
*
* @return 配置Props
*/
private static Properties getProperties() {
// 读取配置文件
Resource resource = new ClassPathResource("application-dev.properties");
Properties props = new Properties();
try {
props = PropertiesLoaderUtils.loadProperties(resource);
} catch (IOException e) {
e.printStackTrace();
}
return props;
}
/**
* 页面生成的文件名
*/
private static String getGeneratorViewPath(String viewOutputDir, TableInfo tableInfo, String suffixPath) {
String name = StringUtils.firstToLowerCase(tableInfo.getEntityName());
String path = viewOutputDir + "/" + name + "/" + name + suffixPath;
File viewDir = new File(path).getParentFile();
if (!viewDir.exists()) {
viewDir.mkdirs();
}
return path;
}
}
mybatis-plus 代码生成方法
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 在这里小小推荐下我的个人博客csdn:雷园的csdn博客个人博客:雷园的个人博客简书:雷园的简书 今天我们来说一下...
- 在掘金看过 @SnailClimb 《回顾一下MyBatis逆向工程——自动生成代码》,也尝试了一下,确实能生成,...
- 本文章仅供小编学习使用,如有侵犯他人版权,请联系小编撤回或删除 pom.xml 代码生成类 测试 生成的目录如下:
- Mybatis Plus Gradle Generator 介绍 Web开发中使用mybatis比较多,MyBat...
- 1. 简介 (玉米地传送门) Mybatis-Plus(简称MP)是baomidou 组织小伙伴 Mybatis...