springboot3.0整合mybatis-flex實(shí)現(xiàn)逆向工程的示例代碼
什么是mybatis逆向工程?
mybatis需要程序員編寫(xiě)SQL語(yǔ)句。mybatis官網(wǎng)提供了逆向工程,針對(duì)對(duì)于單表可自動(dòng)生成mybatis所要執(zhí)行的代碼(mapper.java、mapper.xml、pojo…)
實(shí)際開(kāi)發(fā)中常用的逆向工程方式:
由數(shù)據(jù)庫(kù)的表生成代碼,之所以強(qiáng)調(diào)“單表”兩個(gè)字,是因?yàn)閙ybatis逆向工程生成的Mapper接口所進(jìn)行的操作都是針對(duì)單表的。
引入基本依賴
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>5.0.1</version> </dependency> <dependency> <groupId>com.mybatis-flex</groupId> <artifactId>mybatis-flex-spring-boot-starter</artifactId> <version>1.5.3</version> </dependency> <dependency> <groupId>com.mybatis-flex</groupId> <artifactId>mybatis-flex-codegen</artifactId> <version>1.5.3</version> </dependency>
mybatis-flex yml配置
mybatis-flex: # 啟動(dòng)時(shí)檢查是否存在 MyBatis XML 文件,默認(rèn)不檢查 check-config-location: false # 是否控制臺(tái)打印 MyBatis-Flex 的 LOGO 及版本號(hào) global-config: print-banner: true normal-value-of-logic-delete: 0 deleted-value-of-logic-delete: 1
生成器,修改為自己的配置即可
package gen; import com.mybatisflex.codegen.Generator; import com.mybatisflex.codegen.config.GlobalConfig; import com.zaxxer.hikari.HikariDataSource; import org.junit.Test; /** * @author 程序員星星 * @date 2023/7/27 * @Description */ public class Codegen { //數(shù)據(jù)庫(kù)名和模塊名(這里數(shù)據(jù)庫(kù)名和模塊名一樣) private static final String databaseName = "five_cube_test"; //主機(jī) ip private static final String host = "192.168.157.129"; //數(shù)據(jù)庫(kù)用戶名 private static final String username = "root"; //數(shù)據(jù)庫(kù)密碼 private static final String password = "root"; //生成代碼的路徑 private static final String path = "D:\\myProject\\my\\test_mybatis_flex"; //表名 private static final String tableName = "user"; //作者 private static final String author = "lzx"; //父包模塊名 private static final String moduleName = "mybatisFlex"; //邏輯刪除字段 private static final String logicDeleteColumn = "status"; //樂(lè)觀鎖字段 private static final String versionColumn = "version"; @Test public void genCode() { //配置數(shù)據(jù)源 HikariDataSource dataSource = new HikariDataSource(); dataSource.setJdbcUrl("jdbc:mysql://" + host + ":3306/" + databaseName + "?characterEncoding=utf-8"); dataSource.setUsername(username); dataSource.setPassword(password); //創(chuàng)建配置內(nèi)容,兩種風(fēng)格都可以。 GlobalConfig globalConfig = createGlobalConfigUseStyle2(); //通過(guò) datasource 和 globalConfig 創(chuàng)建代碼生成器 Generator generator = new Generator(dataSource, globalConfig); //生成代碼 generator.generate(); } public static GlobalConfig createGlobalConfigUseStyle2() { //創(chuàng)建配置內(nèi)容 GlobalConfig globalConfig = new GlobalConfig(); //設(shè)置根包 globalConfig.getPackageConfig() .setSourceDir(path + "/src/main/java") .setBasePackage("com." + author + "." + moduleName) .setMapperXmlPath(path + "/src/main/java/com/" + author + "/" + moduleName + "/mapper/xml"); globalConfig.getJavadocConfig() .setAuthor(author); //設(shè)置表前綴和只生成哪些表,setGenerateTable 未配置時(shí),生成所有表 globalConfig.getStrategyConfig() // .setTablePrefix("tb_") .setLogicDeleteColumn(logicDeleteColumn) .setVersionColumn(versionColumn) .setGenerateTable(tableName);//生成多個(gè)表的寫(xiě)法"sys_user","sys_dept" //設(shè)置生成 entity 并啟用 Lombok globalConfig.enableEntity() .setWithLombok(true); //設(shè)置生成 mapper globalConfig.enableMapper(); //設(shè)置生成 mapper.xml globalConfig.enableMapperXml(); //設(shè)置生成service globalConfig.enableService(); globalConfig.enableServiceImpl(); //設(shè)置生成controller globalConfig.enableController(); //設(shè)置生成tableDef globalConfig.enableTableDef(); return globalConfig; } }
config配置類(可選)
package com.lzx.mybatisFlex.config; import com.mybatisflex.core.FlexGlobalConfig; import com.mybatisflex.core.audit.AuditManager; import com.mybatisflex.core.audit.ConsoleMessageCollector; import com.mybatisflex.core.audit.MessageCollector; import com.mybatisflex.spring.boot.MyBatisFlexCustomizer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Configuration; /** * @author 程序員星星 * @date 2023/6/8 * @Description */ @Configuration public class MyBatisFlexConfiguration implements MyBatisFlexCustomizer { private static final Logger logger = LoggerFactory.getLogger("mybatis-flex-sql"); @Override public void customize(FlexGlobalConfig flexGlobalConfig) { //開(kāi)啟審計(jì)功能 AuditManager.setAuditEnable(true); //設(shè)置 SQL 審計(jì)收集器 MessageCollector collector = new ConsoleMessageCollector(); AuditManager.setMessageCollector(collector); //設(shè)置 SQL 審計(jì)收集器 AuditManager.setMessageCollector(auditMessage -> logger.info("{},{}ms", auditMessage.getFullSql(), auditMessage.getElapsedTime()) ); } }
到此這篇關(guān)于springboot3.0整合mybatis-flex實(shí)現(xiàn)逆向工程的示例代碼的文章就介紹到這了,更多相關(guān)springboot mybatis-flex逆向工程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot如何使用MyBatisPlus逆向工程自動(dòng)生成代碼
- SpringBoot整合MybatisPlusGernerator實(shí)現(xiàn)逆向工程
- Spring Boot整合MybatisPlus逆向工程(MySQL/PostgreSQL)
- springboot整合mybatis-plus逆向工程的實(shí)現(xiàn)
- mybatis逆向工程與分頁(yè)在springboot中的應(yīng)用及遇到坑
- SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper實(shí)例詳解
- 淺析Spring和MyBatis整合及逆向工程
- spring和Mybatis逆向工程的實(shí)現(xiàn)
相關(guān)文章
Spring MVC簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Spring MVC屬于SpringFrameWork的后續(xù)產(chǎn)品,已經(jīng)融合在Spring Web Flow里面。今天先從寫(xiě)一個(gè)Spring MVC的HelloWorld開(kāi)始,讓我們看看如何搭建起一個(gè)Spring mvc的環(huán)境并運(yùn)行程序,感興趣的朋友一起學(xué)習(xí)吧2017-08-08基于Java在netty中實(shí)現(xiàn)線程和CPU綁定
這篇文章主要介紹了基于Java在netty中實(shí)現(xiàn)線程和CPU綁定,文章圍繞主題的相關(guān)內(nèi)容展開(kāi)詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05JAVA進(jìn)階篇之詳細(xì)了解File文件的常用API
這篇文章主要給大家介紹了關(guān)于JAVA進(jìn)階篇之詳細(xì)了解File文件的常用API的相關(guān)資料,File用于表示文件系統(tǒng)中的一個(gè)文件或目錄,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11Java StringUtils字符串分割轉(zhuǎn)數(shù)組的實(shí)現(xiàn)
這篇文章主要介紹了Java StringUtils字符串分割轉(zhuǎn)數(shù)組的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09SpringBoot防止大量請(qǐng)求攻擊的實(shí)現(xiàn)
在有些特定的時(shí)候需要加上IP訪問(wèn)時(shí)間限制,防止一個(gè)IP多次訪問(wèn)請(qǐng)求,本文主要介紹了SpringBoot防止大量請(qǐng)求攻擊的實(shí)現(xiàn),感興趣的可以了解一下2021-11-11關(guān)于Mybatis插入對(duì)象時(shí)空值的處理
這篇文章主要介紹了關(guān)于Mybatis插入對(duì)象時(shí)空值的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06springboot的類加載器(org.springframework.boot.loader)過(guò)程詳解
這篇文章主要介紹了springboot的類加載器(org.springframework.boot.loader),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11