Mybatis Plus 代碼生成器的實(shí)現(xiàn)
代碼生成器
MyBatis Plus是MyBatis的擴(kuò)展框架,而代碼生成器是MP的核心功能之一,另外還有 “條件構(gòu)造器”和“通用CRUD”等功能。
步驟演示
mp的代碼生成器有兩種方式自動(dòng)生成代碼,一種是通過main方法來執(zhí)行程序,另一種是通過maven插件build產(chǎn)生。第二種方法需要在pom.xml中添加大量的配置信息,因此本人偏向于使用第一種方式。步驟如下:
一、添加mybatis plus依賴:
如果還沒有創(chuàng)建項(xiàng)目,當(dāng)然需要先創(chuàng)建一個(gè)工程項(xiàng)目,然后將jar包依賴添加到項(xiàng)目的classpath下,如果是含有pom.xml的maven項(xiàng)目,比如我最近在使用的springboot項(xiàng)目,那么可以直接添加pom依賴:
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>2.3</version> </dependency>
二、創(chuàng)建生成器主類
官方給出的實(shí)例連接:代碼生成器, 此處貼出我整理過后的代碼:
package com.mht.springbootmybatis.generate; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.InjectionConfig; import com.baomidou.mybatisplus.generator.config.DataSourceConfig; import com.baomidou.mybatisplus.generator.config.FileOutConfig; 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.TemplateConfig; import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert; import com.baomidou.mybatisplus.generator.config.po.TableInfo; 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> * 代碼生成器演示 * </p> */ public class MpGenerator { /** * <p> * MySQL 生成演示 * </p> */ public static void main(String[] args) { AutoGenerator mpg = new AutoGenerator(); // 選擇 freemarker 引擎,默認(rèn) Veloctiy mpg.setTemplateEngine(new FreemarkerTemplateEngine()); // 全局配置 GlobalConfig gc = new GlobalConfig(); gc.setAuthor("Mht"); gc.setOutputDir("D://workspace/spring-boot-mybatis/src/main/java"); gc.setFileOverride(false);// 是否覆蓋同名文件,默認(rèn)是false gc.setActiveRecord(true);// 不需要ActiveRecord特性的請(qǐng)改為false gc.setEnableCache(false);// XML 二級(jí)緩存 gc.setBaseResultMap(true);// XML ResultMap gc.setBaseColumnList(false);// XML columList /* 自定義文件命名,注意 %s 會(huì)自動(dòng)填充表實(shí)體屬性! */ // gc.setMapperName("%sDao"); // gc.setXmlName("%sDao"); // gc.setServiceName("MP%sService"); // gc.setServiceImplName("%sServiceDiy"); // gc.setControllerName("%sAction"); mpg.setGlobalConfig(gc); // 數(shù)據(jù)源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL); dsc.setTypeConvert(new MySqlTypeConvert() { // 自定義數(shù)據(jù)庫(kù)表字段類型轉(zhuǎn)換【可選】 @Override public DbColumnType processTypeConvert(String fieldType) { System.out.println("轉(zhuǎn)換類型:" + fieldType); // 注意!!processTypeConvert 存在默認(rèn)類型轉(zhuǎn)換,如果不是你要的效果請(qǐng)自定義返回、非如下直接返回。 return super.processTypeConvert(fieldType); } }); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("root"); dsc.setUrl("jdbc:mysql://localhost:3306/ease-run?useUnicode=true&characterEncoding=utf8"); mpg.setDataSource(dsc); // 策略配置 StrategyConfig strategy = new StrategyConfig(); // strategy.setCapitalMode(true);// 全局大寫命名 ORACLE 注意 strategy.setTablePrefix(new String[] { "user_" });// 此處可以修改為您的表前綴 strategy.setNaming(NamingStrategy.nochange);// 表名生成策略 strategy.setInclude(new String[] { "user" }); // 需要生成的表 // strategy.setExclude(new String[]{"test"}); // 排除生成的表 // 自定義實(shí)體父類 // strategy.setSuperEntityClass("com.baomidou.demo.TestEntity"); // 自定義實(shí)體,公共字段 // strategy.setSuperEntityColumns(new String[] { "test_id", "age" }); // 自定義 mapper 父類 // strategy.setSuperMapperClass("com.baomidou.demo.TestMapper"); // 自定義 service 父類 // strategy.setSuperServiceClass("com.baomidou.demo.TestService"); // 自定義 service 實(shí)現(xiàn)類父類 // strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl"); // 自定義 controller 父類 // strategy.setSuperControllerClass("com.baomidou.demo.TestController"); // 【實(shí)體】是否生成字段常量(默認(rèn) false) // public static final String ID = "test_id"; // strategy.setEntityColumnConstant(true); // 【實(shí)體】是否為構(gòu)建者模型(默認(rèn) false) // public User setName(String name) {this.name = name; return this;} // strategy.setEntityBuilderModel(true); mpg.setStrategy(strategy); // 包配置 PackageConfig pc = new PackageConfig(); pc.setParent("com.mht.springbootmybatis"); // 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); // // // 調(diào)整 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)閉默認(rèn) xml 生成,調(diào)整生成 至 根目錄 // TemplateConfig tc = new TemplateConfig(); // tc.setXml(null); // mpg.setTemplate(tc); // 自定義模板配置,可以 copy 源碼 mybatis-plus/src/main/resources/templates 下面內(nèi)容修改, // 放置自己項(xiàng)目的 src/main/resources/templates 目錄下, 默認(rèn)名稱一下可以不配置,也可以自定義模板名稱 // TemplateConfig tc = new TemplateConfig(); // tc.setController("..."); // tc.setEntity("..."); // tc.setMapper("..."); // tc.setXml("..."); // tc.setService("..."); // tc.setServiceImpl("..."); // 如上任何一個(gè)模塊如果設(shè)置 空 OR Null 將不生成該模塊。 // mpg.setTemplate(tc); // 執(zhí)行生成 mpg.execute(); // 打印注入設(shè)置【可無】 // System.err.println(mpg.getCfg().getMap().get("abc")); } }
文件基本可以拿過來直接用,但需要注意這兩句代碼:
gc.setOutputDir("D://workspace/spring-boot-mybatis/src/main/java");// 設(shè)置文件輸出路徑
pc.setParent("com.mht.springbootmybatis");// 父包名
下圖是我項(xiàng)目的路徑(執(zhí)行代碼生成器之后的項(xiàng)目結(jié)構(gòu)):
執(zhí)行main方法前請(qǐng)注意,在生成文件的時(shí)候需要有一個(gè)模板引擎的選擇,MyBatis Plus的默認(rèn)模板引擎是velocity。我們可以使用freemarker。
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> </dependency>
最后執(zhí)行main方法就可以了。
執(zhí)行控制臺(tái)如下:
注意,代碼生成前請(qǐng)確保數(shù)據(jù)庫(kù)中表已經(jīng)存在。
參考文章:
《mybatis-plus思維導(dǎo)圖,讓mybatis-plus不再難懂》
到此這篇關(guān)于Mybatis Plus 代碼生成器的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Mybatis Plus 代碼生成器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot中非容器類如何獲取配置文件數(shù)據(jù)
這篇文章主要介紹了springboot中非容器類如何獲取配置文件數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01深入學(xué)習(xí)java8?中的CompletableFuture
本文主要介紹了java8中的CompletableFuture,CompletableFuture實(shí)現(xiàn)了CompletionStage接口和Future接口,前者是對(duì)后者的一個(gè)擴(kuò)展,增加了異步回調(diào)、流式處理、多個(gè)Future組合處理的能力,使Java在處理多任務(wù)的協(xié)同工作時(shí)更加順暢便利,下文需要的朋友可以參考一下2022-05-05簡(jiǎn)單易懂的java8新特性之lambda表達(dá)式知識(shí)總結(jié)
一直想針對(duì)lambda表達(dá)式作一個(gè)總結(jié),借助于這次公司安排的考試作一個(gè)入門式的總結(jié),對(duì)正在學(xué)習(xí)java的小伙伴們非常有幫助,需要的朋友可以參考下2021-05-05java實(shí)現(xiàn)數(shù)字轉(zhuǎn)換人民幣中文大寫工具
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)數(shù)字轉(zhuǎn)換人民幣中文大寫工具,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Mybatis中如何進(jìn)行批量更新(updateBatch)
這篇文章主要介紹了Mybatis中如何進(jìn)行批量更新(updateBatch),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05Java實(shí)現(xiàn)圖片翻轉(zhuǎn)以及任意角度旋轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圖片翻轉(zhuǎn)以及任意角度旋轉(zhuǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01