1. 程式人生 > >springboot-mybatis-plus生成器

springboot-mybatis-plus生成器

capital 輸入 oot perm develop tcap asi entity fin

1、需要添加pom依賴

<!--代碼生成器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.0.3</version>
</dependency>

<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>

2、生成器配置

public class MpGenerator {

public static void main(String[] args) {
// assert (false) : "代碼生成屬於危險操作,請確定配置後取消斷言執行代碼生成!";
AutoGenerator mpg = new AutoGenerator();
// 選擇 freemarker 引擎,默認 Velocity
mpg.setTemplateEngine(new FreemarkerTemplateEngine());

// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setAuthor("ahys");
gc.setOutputDir("G:\\springboot-mybatis\\src\\main\\java");
gc.setFileOverride(false);// 是否覆蓋同名文件,默認是false
gc.setActiveRecord(true);// 不需要ActiveRecord特性的請改為false
gc.setEnableCache(false);// XML 二級緩存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
/* 自定義文件命名,註意 %s 會自動填充表實體屬性! */
gc.setMapperName("%sDao");
// gc.setXmlName("%sDao");
//gc.setServiceName("%sService");
//gc.setServiceImplName("%sServiceImpl");
// gc.setControllerName("%sAction");
mpg.setGlobalConfig(gc);

// 數據源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setTypeConvert(new MySqlTypeConvert());
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
dsc.setUrl("jdbc:mysql://localhost:3306/datadirtory?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai");
mpg.setDataSource(dsc);

// 策略配置
StrategyConfig strategy = new StrategyConfig();
// strategy.setCapitalMode(true);// 全局大寫命名 ORACLE 註意
//strategy.setTablePrefix(new String[] { "user_" });// 此處可以修改為您的表前綴
strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
strategy.setInclude(new String[] { "meta_directory" }); // 需要生成的表
// 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);
mpg.setStrategy(strategy);

// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.hys.mybatis.mapper");
// pc.setModuleName("test");
mpg.setPackageInfo(pc);

// 註入自定義配置,可以在 VM 中使用 cfg.abc 【可無】
// InjectionConfig cfg = new InjectionConfig() {
// @Override
// public void initMap() {
// Map<String, Object> map = new HashMap<String, Object>();
// map.put("abc", this.getConfig().getGlobalConfig().getAuthor() +
// "-mp");
// this.setMap(map);
// }
// };
//
// // 自定義 xxList.jsp 生成
// List<FileOutConfig> focList = new ArrayList<>();
// focList.add(new FileOutConfig("/template/list.jsp.vm") {
// @Override
// public String outputFile(TableInfo tableInfo) {
// // 自定義輸入文件名稱
// return "D://my_" + tableInfo.getEntityName() + ".jsp";
// }
// });
// cfg.setFileOutConfigList(focList);
// mpg.setCfg(cfg);
//
// // 調整 xml 生成目錄演示
// 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);

// 自定義模板配置,可以 copy 源碼 mybatis-plus/src/main/resources/templates 下面內容修改,
// 放置自己項目的 src/main/resources/templates 目錄下, 默認名稱一下可以不配置,也可以自定義模板名稱
// TemplateConfig tc = new TemplateConfig();
// tc.setController("...");
// tc.setEntity("...");
// tc.setMapper("...");
// tc.setXml("...");
// tc.setService("...");
// tc.setServiceImpl("...");
// 如上任何一個模塊如果設置 空 OR Null 將不生成該模塊。
// mpg.setTemplate(tc);

// 執行生成
mpg.execute();

// 打印註入設置【可無】
// System.err.println(mpg.getCfg().getMap().get("abc"));
}
}
3、將生成的代碼拷貝到項目中

springboot-mybatis-plus生成器