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)文章
Springboot實(shí)現(xiàn)根據(jù)用戶ID切換動(dòng)態(tài)數(shù)據(jù)源
在很多具體應(yīng)用場(chǎng)景中,我們需要用到動(dòng)態(tài)數(shù)據(jù)源的情況,比如多租戶的場(chǎng)景,系統(tǒng)登錄時(shí)需要根據(jù)用戶信息切換到用戶對(duì)應(yīng)的數(shù)據(jù)庫(kù)。這篇文章主要介紹了SpringBoot根據(jù)用戶ID實(shí)現(xiàn)切換動(dòng)態(tài)數(shù)據(jù)源的示例代碼,感興趣的可以了解一下2021-12-12
java中vector與hashtable操作實(shí)例分享
java中vector與hashtable操作實(shí)例,有需要的朋友可以參考一下2014-01-01
java網(wǎng)上圖書(shū)商城(4)購(gòu)物車模塊1
這篇文章主要為大家詳細(xì)介紹了java網(wǎng)上圖書(shū)商城,購(gòu)物車模塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Java中的數(shù)組基礎(chǔ)知識(shí)學(xué)習(xí)教程
這篇文章主要介紹了Java中的數(shù)組基礎(chǔ)知識(shí)學(xué)習(xí)教程,文中同時(shí)也整理了Java對(duì)數(shù)字類型的支持狀況及Number類中的方法,需要的朋友可以參考下2016-02-02
從application.properties配置文件獲取的漢字亂碼的解決方法
平時(shí)從配置文件各種讀取配置參數(shù)都正常,但是有時(shí)候放了個(gè)中文就亂碼,你肯定試過(guò)網(wǎng)上好多方法,都沒(méi)解決,那么來(lái)看下面,恭喜你終于找這里了,本文給大家介紹了從application.properties配置文件獲取的漢字亂碼的解決方法,需要的朋友可以參考下2024-03-03
Mybatis返回map集合時(shí),列的順序與select不一致問(wèn)題
這篇文章主要介紹了Mybatis返回map集合時(shí),列的順序與select不一致問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
springcloud安裝rabbitmq并配置延遲隊(duì)列插件的過(guò)程詳解
本期主要講解如何利用docker快速安裝rabbitmq并且配置延遲隊(duì)列插件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
Spring Boot結(jié)成MyBatis-Plus最全配置指南
本文主要介紹了Spring Boot結(jié)成MyBatis-Plus最全配置指南,包括依賴引入、配置數(shù)據(jù)源、Mapper 掃描、基本CRUD操作等,具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03

