|
@@ -15,9 +15,14 @@
|
|
|
*/
|
|
|
package com.baomidou.mybatisplus.test.generator;
|
|
|
|
|
|
+<<<<<<< HEAD
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+=======
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashMap;
|
|
|
+>>>>>>> dev
|
|
|
import java.util.Map;
|
|
|
|
|
|
import com.baomidou.mybatisplus.generator.AutoGenerator;
|
|
@@ -50,126 +55,114 @@ public class MysqlGenerator {
|
|
|
* </p>
|
|
|
*/
|
|
|
public static void main(String[] args) {
|
|
|
- AutoGenerator mpg = new AutoGenerator();
|
|
|
-
|
|
|
- // 全局配置
|
|
|
- GlobalConfig gc = new GlobalConfig();
|
|
|
- gc.setOutputDir("/develop/code/");
|
|
|
- gc.setFileOverride(true);
|
|
|
- gc.setActiveRecord(true);// 开启 activeRecord 模式
|
|
|
- gc.setEnableCache(false);// XML 二级缓存
|
|
|
- gc.setBaseResultMap(true);// XML ResultMap
|
|
|
- gc.setBaseColumnList(true);// XML columList
|
|
|
- gc.setAuthor("Yanghu");
|
|
|
-
|
|
|
- // 自定义文件命名,注意 %s 会自动填充表实体属性!
|
|
|
- // gc.setMapperName("%sDao");
|
|
|
- // gc.setXmlName("%sDao");
|
|
|
- // gc.setServiceName("MP%sService");
|
|
|
- // gc.setServiceImplName("%sServiceDiy");
|
|
|
- // gc.setControllerName("%sAction");
|
|
|
- mpg.setGlobalConfig(gc);
|
|
|
-
|
|
|
- // 数据源配置
|
|
|
- DataSourceConfig dsc = new DataSourceConfig();
|
|
|
- dsc.setDbType(DbType.MYSQL);
|
|
|
- dsc.setTypeConvert(new MySqlTypeConvert() {
|
|
|
- // 自定义数据库表字段类型转换【可选】
|
|
|
- @Override
|
|
|
- public DbColumnType processTypeConvert(String fieldType) {
|
|
|
- System.out.println("转换类型:" + fieldType);
|
|
|
- // if ( fieldType.toLowerCase().contains( "tinyint" ) ) {
|
|
|
- // return DbColumnType.BOOLEAN;
|
|
|
- // }
|
|
|
- return super.processTypeConvert(fieldType);
|
|
|
- }
|
|
|
- });
|
|
|
- dsc.setDriverName("com.mysql.jdbc.Driver");
|
|
|
- dsc.setUsername("root");
|
|
|
- dsc.setPassword("521");
|
|
|
- dsc.setUrl("jdbc:mysql://127.0.0.1:3306/mybatis-plus?characterEncoding=utf8");
|
|
|
- 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[] { "user" }); // 需要生成的表
|
|
|
- // 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.baomidou.demo.TestController");
|
|
|
- // 【实体】是否生成字段常量(默认 false)
|
|
|
- // public static final String ID = "test_id";
|
|
|
- // strategy.setEntityColumnConstant(true);
|
|
|
- // 【实体】是否为构建者模型(默认 false)
|
|
|
- // public User setName(String name) {this.name = name; return this;}
|
|
|
- // strategy.setEntityBuilderModel(true);
|
|
|
- // 【实体】是否为lombok模型(默认 false)<a href="https://projectlombok.org/">document</a>
|
|
|
- // strategy.setEntityLombokModel(true);
|
|
|
- // Boolean类型字段是否移除is前缀处理
|
|
|
- // strategy.setEntityBooleanColumnRemoveIsPrefix(true);
|
|
|
- // strategy.setRestControllerStyle(true);
|
|
|
- // strategy.setControllerMappingHyphenStyle(true);
|
|
|
- mpg.setStrategy(strategy);
|
|
|
-
|
|
|
- // 包配置
|
|
|
- PackageConfig pc = new PackageConfig();
|
|
|
- pc.setModuleName("test");
|
|
|
- pc.setParent("com.baomidou");// 自定义包路径
|
|
|
- pc.setController("controller");// 这里是控制器包名,默认 web
|
|
|
- mpg.setPackageInfo(pc);
|
|
|
-
|
|
|
- // 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
|
|
|
- InjectionConfig cfg = new InjectionConfig() {
|
|
|
- @Override
|
|
|
- public void initMap() {
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
- map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
|
|
|
- this.setMap(map);
|
|
|
- }
|
|
|
- };
|
|
|
- List<FileOutConfig> focList = new ArrayList<>();
|
|
|
- focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
|
|
|
- @Override
|
|
|
- public String outputFile(TableInfo tableInfo) {
|
|
|
- return "/develop/code/xml/" + tableInfo.getEntityName() + ".xml";
|
|
|
- }
|
|
|
- });
|
|
|
- cfg.setFileOutConfigList(focList);
|
|
|
- mpg.setCfg(cfg);
|
|
|
-
|
|
|
- // 关闭默认 xml 生成,调整生成 至 根目录
|
|
|
- TemplateConfig tc = new TemplateConfig();
|
|
|
- tc.setXml(null);
|
|
|
- mpg.setTemplate(tc);
|
|
|
-
|
|
|
- // 自定义模板配置,模板可以参考源码 /mybatis-plus/src/main/resources/template 使用 copy
|
|
|
- // 至您项目 src/main/resources/template 目录下,模板名称也可自定义如下配置:
|
|
|
- // TemplateConfig tc = new TemplateConfig();
|
|
|
- // tc.setController("...");
|
|
|
- // tc.setEntity("...");
|
|
|
- // tc.setMapper("...");
|
|
|
- // tc.setXml("...");
|
|
|
- // tc.setService("...");
|
|
|
- // tc.setServiceImpl("...");
|
|
|
- // mpg.setTemplate(tc);
|
|
|
+ // 代码生成器
|
|
|
+ AutoGenerator mpg = new AutoGenerator().setGlobalConfig(
|
|
|
+ // 全局配置
|
|
|
+ new GlobalConfig()
|
|
|
+ .setOutputDir("/develop/code/")//输出目录
|
|
|
+ .setFileOverride(true)// 是否覆盖文件
|
|
|
+ .setActiveRecord(true)// 开启 activeRecord 模式
|
|
|
+ .setEnableCache(false)// XML 二级缓存
|
|
|
+ .setBaseResultMap(true)// XML ResultMap
|
|
|
+ .setBaseColumnList(true)// XML columList
|
|
|
+ .setAuthor("Yanghu")
|
|
|
+ // 自定义文件命名,注意 %s 会自动填充表实体属性!
|
|
|
+ // .setMapperName("%sDao")
|
|
|
+ // .setXmlName("%sDao")
|
|
|
+ // .setServiceName("MP%sService")
|
|
|
+ // .setServiceImplName("%sServiceDiy")
|
|
|
+ // .setControllerName("%sAction")
|
|
|
+ ).setDataSource(
|
|
|
+ // 数据源配置
|
|
|
+ new DataSourceConfig()
|
|
|
+ .setDbType(DbType.MYSQL)// 数据库类型
|
|
|
+ .setTypeConvert(new MySqlTypeConvert() {
|
|
|
+ // 自定义数据库表字段类型转换【可选】
|
|
|
+ @Override
|
|
|
+ public DbColumnType processTypeConvert(String fieldType) {
|
|
|
+ System.out.println("转换类型:" + fieldType);
|
|
|
+ // if ( fieldType.toLowerCase().contains( "tinyint" ) ) {
|
|
|
+ // return DbColumnType.BOOLEAN;
|
|
|
+ // }
|
|
|
+ return super.processTypeConvert(fieldType);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .setDriverName("com.mysql.jdbc.Driver")
|
|
|
+ .setUsername("root")
|
|
|
+ .setPassword("521")
|
|
|
+ .setUrl("jdbc:mysql://127.0.0.1:3306/mybatis-plus?characterEncoding=utf8")
|
|
|
+ ).setStrategy(
|
|
|
+ // 策略配置
|
|
|
+ new StrategyConfig()
|
|
|
+ // .setCapitalMode(true)// 全局大写命名
|
|
|
+ // .setDbColumnUnderline(true)//全局下划线命名
|
|
|
+ .setTablePrefix(new String[]{"bmd_", "mp_"})// 此处可以修改为您的表前缀
|
|
|
+ .setNaming(NamingStrategy.underline_to_camel)// 表名生成策略
|
|
|
+ // .setInclude(new String[] { "user" }) // 需要生成的表
|
|
|
+ // .setExclude(new String[]{"test"}) // 排除生成的表
|
|
|
+ // 自定义实体父类
|
|
|
+ // .setSuperEntityClass("com.baomidou.demo.TestEntity")
|
|
|
+ // 自定义实体,公共字段
|
|
|
+ .setSuperEntityColumns(new String[]{"test_id", "age"})
|
|
|
+ // 自定义 mapper 父类
|
|
|
+ // .setSuperMapperClass("com.baomidou.demo.TestMapper")
|
|
|
+ // 自定义 service 父类
|
|
|
+ // .setSuperServiceClass("com.baomidou.demo.TestService")
|
|
|
+ // 自定义 service 实现类父类
|
|
|
+ // .setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl")
|
|
|
+ // 自定义 controller 父类
|
|
|
+ // .setSuperControllerClass("com.baomidou.demo.TestController")
|
|
|
+ // 【实体】是否生成字段常量(默认 false)
|
|
|
+ // public static final String ID = "test_id";
|
|
|
+ // .setEntityColumnConstant(true)
|
|
|
+ // 【实体】是否为构建者模型(默认 false)
|
|
|
+ // public User setName(String name) {this.name = name; return this;}
|
|
|
+ // .setEntityBuilderModel(true)
|
|
|
+ // 【实体】是否为lombok模型(默认 false)<a href="https://projectlombok.org/">document</a>
|
|
|
+ // .setEntityLombokModel(true)
|
|
|
+ // Boolean类型字段是否移除is前缀处理
|
|
|
+ // .setEntityBooleanColumnRemoveIsPrefix(true)
|
|
|
+ // .setRestControllerStyle(true)
|
|
|
+ // .setControllerMappingHyphenStyle(true)
|
|
|
+ ).setPackageInfo(
|
|
|
+ // 包配置
|
|
|
+ new PackageConfig()
|
|
|
+ .setModuleName("test")
|
|
|
+ .setParent("com.baomidou")// 自定义包路径
|
|
|
+ .setController("controller")// 这里是控制器包名,默认 web
|
|
|
+ ).setCfg(
|
|
|
+ // 注入自定义配置,可以在 VM 中使用 cfg.abc 设置的值
|
|
|
+ new InjectionConfig() {
|
|
|
+ @Override
|
|
|
+ public void initMap() {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
|
|
|
+ this.setMap(map);
|
|
|
+ }
|
|
|
+ }.setFileOutConfigList(Arrays.<FileOutConfig>asList(new FileOutConfig("/templates/mapper.xml.vm") {
|
|
|
+ // 自定义输出文件目录
|
|
|
+ @Override
|
|
|
+ public String outputFile(TableInfo tableInfo) {
|
|
|
+ return "/develop/code/xml/" + tableInfo.getEntityName() + ".xml";
|
|
|
+ }
|
|
|
+ }))
|
|
|
+ ).setTemplate(
|
|
|
+ // 关闭默认 xml 生成,调整生成 至 根目录
|
|
|
+ new TemplateConfig().setXml(null)
|
|
|
+ // 自定义模板配置,模板可以参考源码 /mybatis-plus/src/main/resources/template 使用 copy
|
|
|
+ // 至您项目 src/main/resources/template 目录下,模板名称也可自定义如下配置:
|
|
|
+ // .setController("...");
|
|
|
+ // .setEntity("...");
|
|
|
+ // .setMapper("...");
|
|
|
+ // .setXml("...");
|
|
|
+ // .setService("...");
|
|
|
+ // .setServiceImpl("...");
|
|
|
+ );
|
|
|
|
|
|
// 执行生成
|
|
|
mpg.execute();
|
|
|
|
|
|
- // 打印注入设置
|
|
|
+ // 打印注入设置,这里演示模板里面怎么获取注入内容【可无】
|
|
|
System.err.println(mpg.getCfg().getMap().get("abc"));
|
|
|
}
|
|
|
|