FluentMybatis實(shí)現(xiàn)mybatis動(dòng)態(tài)sql拼裝和fluent api語(yǔ)法
開始第一個(gè)例子: Hello World
新建Java工程,設(shè)置maven依賴
新建maven工程,設(shè)置項(xiàng)目編譯級(jí)別為Java8及以上,引入fluent mybatis依賴包。
<dependencies>
<!-- 引入fluent-mybatis 運(yùn)行依賴包, scope為compile -->
<dependency>
<groupId>com.github.atool</groupId>
<artifactId>fluent-mybatis</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 引入fluent-mybatis-processor, scope設(shè)置為provider 編譯需要,運(yùn)行時(shí)不需要 -->
<dependency>
<groupId>com.github.atool</groupId>
<artifactId>fluent-mybatis-processor</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
新建演示用的數(shù)據(jù)庫(kù)結(jié)構(gòu)
create schema fluent_mybatis_tutorial;
create table hello_world
(
id bigint unsigned auto_increment primary key,
say_hello varchar(100) null,
your_name varchar(100) null,
gmt_create datetime DEFAULT NULL COMMENT '創(chuàng)建時(shí)間',
gmt_modified datetime DEFAULT NULL COMMENT '更新時(shí)間',
is_deleted tinyint(2) DEFAULT 0 COMMENT '是否邏輯刪除'
) ENGINE = InnoDB
CHARACTER SET = utf8 comment '簡(jiǎn)單演示表';
創(chuàng)建數(shù)據(jù)庫(kù)表對(duì)應(yīng)的Entity類
創(chuàng)建數(shù)據(jù)庫(kù)表對(duì)應(yīng)的Entity類: HelloWorldEntity, 你只需要簡(jiǎn)單的做3個(gè)動(dòng)作:
- 根據(jù)駝峰命名規(guī)則命名Entity類和字段
- HelloWorldEntity繼承IEntity接口類
- 在HelloWorldEntity類上加注解 @FluentMybatis
@FluentMybatis
public class HelloWorldEntity implements IEntity {
private Long id;
private String sayHello;
private String yourName;
private Date gmtCreate;
private Date gmtModified;
private Boolean isDeleted;
// get, set, toString 方法
}
很簡(jiǎn)單吧,在這里,你即不需要配置任何mybatis xml文件, 也不需要寫任何Mapper接口, 但你已經(jīng)擁有了強(qiáng)大的增刪改查的功能,并且是Fluent API,讓我們寫一個(gè)測(cè)試來(lái)見(jiàn)證一下Fluent Mybatis的魔法力量!
運(yùn)行測(cè)試來(lái)見(jiàn)證Fluent Mybatis的神奇
為了運(yùn)行測(cè)試, 我們還需要進(jìn)行JUnit和Spring Test相關(guān)配置。
配置spring bean定義
數(shù)據(jù)源DataSource配置
mybatis的mapper掃描路徑
mybatis的SqlSessionFactoryBean
@ComponentScan(basePackages = "cn.org.atool.fluent.mybatis.demo1")
@MapperScan("cn.org.atool.fluent.mybatis.demo1.entity.mapper")
@Configuration
public class HelloWorldConfig {
/**
* 設(shè)置dataSource屬性
*
* @return
*/
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/fluent_mybatis_tutorial?useUnicode=true&characterEncoding=utf8");
dataSource.setUsername("root");
dataSource.setPassword("password");
return dataSource;
}
/**
* 定義mybatis的SqlSessionFactoryBean
*
* @param dataSource
* @return
*/
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean;
}
}
使用Junit4和Spring-test來(lái)執(zhí)行測(cè)試
- 使用spring-test初始化spring容器
- 注入HelloWorldEntity對(duì)應(yīng)的Mapper類: HelloWorldMapper, 這個(gè)類是fluent mybatis編譯時(shí)生成的。
- 使用HelloWorldMapper進(jìn)行刪除、插入、查詢、修改操作。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = HelloWorldConfig.class)
public class HelloWorldTest {
/**
* fluent mybatis編譯時(shí)生成的Mapper類
*/
@Autowired
HelloWorldMapper mapper;
@Test
public void testHelloWorld() {
/**
* 為了演示方便,先刪除數(shù)據(jù)
*/
mapper.delete(mapper.query()
.where.id().eq(1L).end());
/**
* 插入數(shù)據(jù)
*/
HelloWorldEntity entity = new HelloWorldEntity();
entity.setId(1L);
entity.setSayHello("hello world");
entity.setYourName("fluent mybatis");
entity.setIsDeleted(false);
mapper.insert(entity);
/**
* 查詢 id = 1 的數(shù)據(jù)
*/
HelloWorldEntity result1 = mapper.findOne(mapper.query()
.where.id().eq(1L).end());
/**
* 控制臺(tái)直接打印出查詢結(jié)果
*/
System.out.println("1. HelloWorldEntity:" + result1.toString());
/**
* 更新id = 1的記錄
*/
mapper.updateBy(mapper.updater()
.update.sayHello().is("say hello, say hello!")
.set.yourName().is("fluent mybatis is powerful!").end()
.where.id().eq(1L).end()
);
/**
* 查詢 id = 1 的數(shù)據(jù)
*/
HelloWorldEntity result2 = mapper.findOne(mapper.query()
.where.sayHello().like("hello")
.and.isDeleted().eq(false).end()
.limit(1)
);
/**
* 控制臺(tái)直接打印出查詢結(jié)果
*/
System.out.println("2. HelloWorldEntity:" + result2.toString());
}
}
執(zhí)行Junit4測(cè)試方法,控制臺(tái)輸出
1. HelloWorldEntity:HelloWorldEntity{id=1, sayHello='hello world', yourName='fluent mybatis', gmtCreate=null, gmtModified=null, isDeleted=false}
2. HelloWorldEntity:HelloWorldEntity{id=1, sayHello='say hello, say hello!', yourName='fluent mybatis is powerful!', gmtCreate=null, gmtModified=null, isDeleted=false}
神奇吧! 我們?cè)俚綌?shù)據(jù)庫(kù)中查看一下結(jié)果

現(xiàn)在,我們已經(jīng)通過(guò)一個(gè)簡(jiǎn)單例子演示了fluent mybatis的強(qiáng)大功能,
在進(jìn)一步介紹fluent mybatis更強(qiáng)大功能前,我們揭示一下為啥我們只寫了一個(gè)數(shù)據(jù)表對(duì)應(yīng)的Entity類,
卻擁有了一系列增刪改查的數(shù)據(jù)庫(kù)操作方法。
fluent mybatis根據(jù)Entity類上@FluentMybatis注解在編譯時(shí),
會(huì)在target目錄class目錄下自動(dòng)編譯生成一系列文件:

核心接口類, 使用時(shí)需要了解
- mapper/*Mapper: mybatis的Mapper定義接口, 定義了一系列通用的數(shù)據(jù)操作接口方法。
- dao/*BaseDao: Dao實(shí)現(xiàn)基類, 所有的DaoImpl都繼承各自基類
- 根據(jù)分層編碼的原則,我們不會(huì)在Service類中直接使用Mapper類,而是引用Dao類。我們?cè)贒ao實(shí)現(xiàn)類中根據(jù)條件實(shí)現(xiàn)具體的數(shù)據(jù)操作方法。
- wrapper/*Query: fluent mybatis核心類, 用來(lái)進(jìn)行動(dòng)態(tài)sql的構(gòu)造, 進(jìn)行條件查詢。
- wrapper/*Updater: fluent mybatis核心類, 用來(lái)動(dòng)態(tài)構(gòu)造update語(yǔ)句。
- entity/*EntityHelper: Entity幫助類, 實(shí)現(xiàn)了Entity和Map的轉(zhuǎn)換方法
- 輔助實(shí)現(xiàn)時(shí), 實(shí)現(xiàn)fluent mybatis動(dòng)態(tài)sql拼裝和fluent api時(shí)內(nèi)部用到的類,使用時(shí)無(wú)需了解
- 在使用上,我們主要會(huì)接觸到上述5個(gè)生成的java類。Fluent Mybatis為了實(shí)現(xiàn)動(dòng)態(tài)拼接和Fluent API功能,還生成了一系列輔助類。
- helper/*Mapping: 表字段和Entity屬性映射定義類
- helper/*SqlProviderP: Mapper接口動(dòng)態(tài)sql提供者
- helper/*WrapperHelper: Query和Updater具體功能實(shí)現(xiàn), 包含幾個(gè)實(shí)現(xiàn):select, where, group by, having by, order by, limit
到此這篇關(guān)于FluentMybatis實(shí)現(xiàn)mybatis動(dòng)態(tài)sql拼裝和fluent api語(yǔ)法的文章就介紹到這了,更多相關(guān)FluentMybatis實(shí)現(xiàn)mybatis動(dòng)態(tài)sql內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Spring Cloud中Hystrix的請(qǐng)求合并
這篇文章主要介紹了詳解Spring Cloud中Hystrix的請(qǐng)求合并,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
你肯定能看懂的Java IO相關(guān)知識(shí)總結(jié)
群里有大佬說(shuō)想讓我寫一篇NIO,一直也沒(méi)寫,但是和同事聊天也說(shuō)對(duì)Java的IO不是很清晰,因此今天就寫下Java的io,先打個(gè)基礎(chǔ),下次寫NIO,需要的朋友可以參考下2021-05-05
POI對(duì)Excel自定義日期格式的讀取(實(shí)例代碼)
下面小編就為大家?guī)?lái)一篇POI對(duì)Excel自定義日期格式的讀取(實(shí)例代碼)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11
淺談springboot中tk.mapper代碼生成器的用法說(shuō)明
這篇文章主要介紹了淺談springboot中tk.mapper代碼生成器的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
Java 8 Stream.distinct() 列表去重的操作
這篇文章主要介紹了Java 8 Stream.distinct() 列表去重的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
SpringBoot Maven 項(xiàng)目 pom 中的 plugin&n
本文詳細(xì)介紹了Spring Boot Maven項(xiàng)目打包成jar文件時(shí)使用的spring-boot-maven-plugin插件,深入探討了插件的配置元素,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-01-01
MyBatis-Plus多表聯(lián)查的實(shí)現(xiàn)方法(動(dòng)態(tài)查詢和靜態(tài)查詢)
本文用示例介紹使用MyBatis-Plus進(jìn)行多表查詢的方法,包括靜態(tài)查詢和動(dòng)態(tài)查詢,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-03-03
java根據(jù)擴(kuò)展名獲取系統(tǒng)圖標(biāo)和文件圖標(biāo)示例
這篇文章主要介紹了java根據(jù)擴(kuò)展名獲取系統(tǒng)圖標(biāo)和文件圖標(biāo)示例,需要的朋友可以參考下2014-03-03

