關(guān)于mybatis-plus-generator的簡(jiǎn)單使用示例詳解
在springboot項(xiàng)目中集成mybatis-plus是很方便開(kāi)發(fā)的,最近看了一下plus的文檔,簡(jiǎn)單用一下它的代碼生成器,首先在一個(gè)簡(jiǎn)單的springboot項(xiàng)目中加入如下依賴(lài)
<!-- 引入mybatis-plus依賴(lài) --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.2</version> </dependency> <!-- 引入mybatis-plus-generator依賴(lài) --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.3.2</version> </dependency> <!-- 引入freemarker依賴(lài) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!-- 引入mysql依賴(lài) --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> <scope>runtime</scope> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
我這邊呢習(xí)慣把mapper文件放在java源碼路徑下,而不是放在默認(rèn)的resources目錄下,項(xiàng)目啟動(dòng)有導(dǎo)致mapper注入不了,所以還得在pom的<build></build>標(biāo)簽之中加入如下配置,并在啟動(dòng)類(lèi)上加上mapper掃描路徑
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.ftl</include> </includes> <filtering>false</filtering> </resource> </resources>
@SpringBootApplication @MapperScan(basePackages = {"com.fengyun.mallmanage"}) public class MallManageSystemApplication { public static void main(String[] args) { SpringApplication.run(MallManageSystemApplication.class, args); } }
基本依賴(lài)和配置結(jié)束了,我參考了plus的官方文檔,根據(jù)自己的需求稍微修改了一下它的生成器代碼,官網(wǎng)地址
/** * mybatis-plus代碼生成器,生成實(shí)體,mapper,mapper.xml,service,serviceImpl,controller * 演示例子,執(zhí)行 main 方法控制臺(tái)輸入表名回車(chē)自動(dòng)生成對(duì)應(yīng)項(xiàng)目目錄中(目錄要需要自行修改) */ public class CodeGenerator { /** * <p> * 讀取控制臺(tái)內(nèi)容 * </p> */ public static String scanner(String tip) { Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append("請(qǐng)輸入" + tip + ":"); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); if (StringUtils.isNotEmpty(ipt)) { return ipt; } } throw new MybatisPlusException("請(qǐng)輸入正確的" + tip + "!"); } public static void main(String[] args) { // 代碼生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor("YuanXing"); //是否打開(kāi)輸出的目錄,默認(rèn)true gc.setOpen(false); //覆蓋已有的文件,默認(rèn)false(第一次生成時(shí)放開(kāi)) // gc.setFileOverride(true); gc.setBaseResultMap(true); gc.setBaseColumnList(true); // 設(shè)置日期類(lèi)型為Date(若不設(shè)置時(shí)間類(lèi)型都會(huì)變成LocalDateTime部分連接池例如druid是無(wú)法識(shí)別的) gc.setDateType(DateType.ONLY_DATE); mpg.setGlobalConfig(gc); // 數(shù)據(jù)源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/malldata-dev?useUnicode=true&characterEncoding=UTF-8&useSSL=false"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("密碼"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); // pc.setModuleName(scanner("模塊名")); pc.setParent("com.fengyun.mallmanage"); //自定義實(shí)體包名(不同的模塊自己手動(dòng)修改) pc.setEntity("mapper.goods.entity"); //自定義mapper包名(不同的模塊自己手動(dòng)修改) pc.setMapper("mapper.goods"); //自定義mapper.xml包名(不同的模塊自己手動(dòng)修改) pc.setXml("mapper.goods"); //自定義service包名(不同的模塊自己手動(dòng)修改) pc.setService("service.goods"); //自定義serviceImpl包名(不同的模塊自己手動(dòng)修改) pc.setServiceImpl("service.goods.impl"); //自定義controller包名(不同的模塊自己手動(dòng)修改) pc.setController("controller.goods"); mpg.setPackageInfo(pc); // 自定義配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; // 如果模板引擎是 freemarker String xmlPath = "/templates/mapper.xml.ftl"; // 自定義輸出配置 List<FileOutConfig> focList = new ArrayList<>(); // 自定義配置會(huì)被優(yōu)先輸出 focList.add(new FileOutConfig(xmlPath) { @Override public String outputFile(TableInfo tableInfo) { // 自定義輸出文件名 , 如果你 Entity 設(shè)置了前后綴、此處注意 xml 的名稱(chēng)會(huì)跟著發(fā)生變化??! return projectPath + "/src/main/java/com/fengyun/mallmanage/mapper/goods" + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); templateConfig.setXml(null); mpg.setTemplate(templateConfig); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); //是否為lombok模型,默認(rèn)為false strategy.setEntityLombokModel(true); //前后端分離時(shí)可開(kāi)啟 // strategy.setRestControllerStyle(true); strategy.setInclude(scanner("表名,多個(gè)英文逗號(hào)分割").split(",")); //RequestMapping駝峰轉(zhuǎn)連字符 // strategy.setControllerMappingHyphenStyle(true); //生成實(shí)體時(shí)生成生成數(shù)據(jù)庫(kù)字段注解 strategy.setEntityTableFieldAnnotationEnable(true); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }
然后運(yùn)行即可
需要注意的是由于我在代碼生成器代碼中包配置的時(shí)候注釋掉了輸入模塊名(即這一段pc.setModuleName(scanner("模塊名"));),所以會(huì)導(dǎo)致controller中的RequestMapping的路徑有兩個(gè)//表名的駝峰命名,假設(shè)輸入模塊名的話RequestMapping的路徑就會(huì)是/模塊名/表名的駝峰命名,但是這樣的話生成的類(lèi)的包就不是我現(xiàn)在這樣的了,這個(gè)看自己的需求吧,具體可以看一下生成包的源碼
更多Java內(nèi)容,請(qǐng)點(diǎn)擊下方名片。
到此這篇關(guān)于關(guān)于mybatis-plus-generator的簡(jiǎn)單使用的文章就介紹到這了,更多相關(guān)mybatis-plus-generator使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MybatisX-Generator不生成domain文件夾的問(wèn)題及解決
- MyBatis代碼自動(dòng)生成器Mybatis-Generator的使用詳解
- mybatis-generator生成多次重復(fù)代碼問(wèn)題以及解決
- 解決mybatis generator MySQL自增ID出現(xiàn)重復(fù)問(wèn)題MySQLIntegrityConstraintViolationException
- MyBatis?Generator生成數(shù)據(jù)庫(kù)模型實(shí)現(xiàn)示例
- MybatisGenerator文件生成不出對(duì)應(yīng)文件的問(wèn)題
相關(guān)文章
詳解在Spring中如何使用AspectJ來(lái)實(shí)現(xiàn)AOP
這篇文章主要介紹了詳解在Spring中如何使用AspectJ來(lái)實(shí)現(xiàn)AOP,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06解決子線程中獲取不到HttpServletRequest對(duì)象的問(wèn)題
這篇文章主要介紹了解決子線程中獲取不到HttpServletRequest對(duì)象的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07shiro實(shí)現(xiàn)單點(diǎn)登錄(一個(gè)用戶(hù)同一時(shí)刻只能在一個(gè)地方登錄)
這篇文章主要介紹了shiro實(shí)現(xiàn)單點(diǎn)登錄(一個(gè)用戶(hù)同一時(shí)刻只能在一個(gè)地方登錄)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-08-08WebDriver中實(shí)現(xiàn)對(duì)特定的Web區(qū)域截圖方法
這篇文章主要介紹了WebDriver中實(shí)現(xiàn)對(duì)特定的Web區(qū)域截圖方法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06spring aop實(shí)現(xiàn)接口超時(shí)處理組件的代碼詳解
這篇文章給大家介紹了spring aop實(shí)現(xiàn)接口超時(shí)處理組件,文中有詳細(xì)的實(shí)現(xiàn)思路,并通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02SpringBoot升級(jí)3.2報(bào)錯(cuò)Invalid value type for
這篇文章給大家介紹了SpringBoot升級(jí)3.2報(bào)錯(cuò)Invalid value type for attribute ‘factoryBeanObjectType‘: java.lang.String的解決方案,文中有詳細(xì)的原因分析,需要的朋友可以參考下2023-12-12MyBatis批量插入的五種方式小結(jié)(MyBatis以集合方式批量新增)
本文主要介紹了MyBatis批量插入的五種方式小結(jié)(MyBatis以集合方式批量新增),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01詳解Maven profile配置管理及激活profile的幾種方式
這篇文章主要介紹了詳解Maven profile配置管理及激活profile的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01SpringBoot配置攔截器實(shí)現(xiàn)過(guò)程詳解
在系統(tǒng)中經(jīng)常需要在處理用戶(hù)請(qǐng)求之前和之后執(zhí)行一些行為,例如檢測(cè)用戶(hù)的權(quán)限,或者將請(qǐng)求的信息記錄到日志中,即平時(shí)所說(shuō)的"權(quán)限檢測(cè)"及"日志記錄",下面這篇文章主要給大家介紹了關(guān)于在SpringBoot項(xiàng)目中整合攔截器的相關(guān)資料,需要的朋友可以參考下2022-10-10