- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- mybatis-plus代碼生成器配置的示例分析
這篇文章主要介紹了mybatis-plus代碼生成器配置的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著(zhù)大家一起了解一下。
最近在玩項目,發(fā)現自己寫(xiě)嚴重浪費時(shí)間~于是想到了代碼生成器,之前用過(guò)一次的mybatis-plus,再重新實(shí)現了一下
確保修改好對應的配置即可
依賴(lài):
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>2.2.0</version> </dependency> <!-- 驅動(dòng) --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.8</version> </dependency>
代碼:
package cn.zytao.taosir.generator; import com.baomidou.mybatisplus.generator.AutoGenerator;import com.baomidou.mybatisplus.generator.config.DataSourceConfig;import com.baomidou.mybatisplus.generator.config.GlobalConfig;import com.baomidou.mybatisplus.generator.config.PackageConfig;import com.baomidou.mybatisplus.generator.config.StrategyConfig;import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;import com.baomidou.mybatisplus.generator.config.rules.DbType;import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; /** * <p> * mybaits-plus代碼生成器 * </p> */public class Generator { /** * <p> * MySQL 生成演示 * </p> */ public static void main(String[] args) { AutoGenerator mpg = new AutoGenerator(); // 選擇 freemarker 引擎,默認 Veloctiy mpg.setTemplateEngine(new FreemarkerTemplateEngine()); // 全局配置 GlobalConfig gc = new GlobalConfig(); gc.setAuthor("taosir"); gc.setOutputDir("F://"); gc.setFileOverride(false);// 是否覆蓋同名文件,默認是false gc.setActiveRecord(true);// 不需要ActiveRecord特性的請改為false gc.setEnableCache(false);// XML 二級緩存 gc.setBaseResultMap(false);// XML ResultMap gc.setBaseColumnList(false);// XML columList /* 自定義文件命名,注意 %s 會(huì )自動(dòng)填充表實(shí)體屬性! */ gc.setMapperName("%sMapper"); // gc.setXmlName("%sDao"); gc.setServiceName("%sService"); gc.setServiceImplName("%sServiceImpl"); //gc.setControllerName("%sController"); mpg.setGlobalConfig(gc); // 數據源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL); dsc.setTypeConvert(new MySqlTypeConvert() { // 自定義數據庫表字段類(lèi)型轉換【可選】 @Override public DbColumnType processTypeConvert(String fieldType) { System.out.println("轉換類(lèi)型:" + fieldType); // 注意??!processTypeConvert 存在默認類(lèi)型轉換,如果不是你要的效果請自定義返回、非如下直接返回。 return super.processTypeConvert(fieldType); } }); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("root"); dsc.setUrl("jdbc:mysql://localhost:3306/taosir?useUnicode=true&characterEncoding=utf8"); mpg.setDataSource(dsc); // 策略配置 StrategyConfig strategy = new StrategyConfig(); //strategy.setCapitalMode(true);// 全局大寫(xiě)命名 ORACLE 注意 //strategy.setTablePrefix(new String[] { "user_" });// 表前綴 strategy.setNaming(NamingStrategy.nochange);// 表名生成策略 strategy.setInclude(new String[] { "user"}); // 需要生成的表 // strategy.setExclude(new String[]{"test"}); // 排除生成的表 // 自定義實(shí)體父類(lèi) // strategy.setSuperEntityClass("com.baomidou.demo.TestEntity"); // 自定義實(shí)體,公共字段 // strategy.setSuperEntityColumns(new String[] { "id", "age" }); // 自定義 mapper 父類(lèi) // strategy.setSuperMapperClass("com.baomidou.demo.TestMapper"); // 自定義 service 父類(lèi) // strategy.setSuperServiceClass("com.baomidou.demo.TestService"); // 自定義 service 實(shí)現類(lèi)父類(lèi) // strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl"); // 自定義 controller 父類(lèi) // strategy.setSuperControllerClass("com.baomidou.demo.TestController"); // 【實(shí)體】是否生成字段常量(默認 false) // public static final String ID = "id"; // strategy.setEntityColumnConstant(true); // 【實(shí)體】是否為構建者模型(默認 false) // public User setName(String name) {this.name = name; return this;} // strategy.setEntityBuilderModel(true); mpg.setStrategy(strategy); // 包配置 PackageConfig pc = new PackageConfig(); pc.setParent("cn.zytao.taosir");// pc.setModuleName("test"); mpg.setPackageInfo(pc); // 注入自定義配置,可以在 VM 中使用 cfg.abc 【可無(wú)】// 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) {// // 自定義輸入文件名稱(chēng)// 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);//// // 關(guān)閉默認 xml 生成,調整生成 至 根目錄// TemplateConfig tc = new TemplateConfig();// tc.setXml(null);// mpg.setTemplate(tc); // 自定義模板配置,可以 copy 源碼 mybatis-plus/src/main/resources/templates 下面內容修改, // 放置自己項目的 src/main/resources/templates 目錄下, 默認名稱(chēng)一下可以不配置,也可以自定義模板名稱(chēng) // TemplateConfig tc = new TemplateConfig(); // tc.setController("..."); // tc.setEntity("..."); // tc.setMapper("..."); // tc.setXml("..."); // tc.setService("..."); // tc.setServiceImpl("..."); // 如上任何一個(gè)模塊如果設置 空 OR Null 將不生成該模塊。 // mpg.setTemplate(tc); // 執行生成 mpg.execute(); // 打印注入設置【可無(wú)】// System.err.println(mpg.getCfg().getMap().get("abc")); }}
建個(gè)工程丟下去,修改相關(guān)配置,run完事~~~~
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站