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)見證一下Fluent Mybatis的魔法力量!
運(yùn)行測(cè)試來(lái)見證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)文章
使用maven的profile構(gòu)建不同環(huán)境配置的方法
這篇文章主要介紹了使用maven的profile構(gòu)建不同環(huán)境配置的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01關(guān)于JAVA11中圖片與BASE64相互轉(zhuǎn)換的實(shí)現(xiàn)
這篇文章主要介紹了關(guān)于JAVA11中圖片與BASE64相互轉(zhuǎn)換的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Java三種獲取redis的連接及redis_String類型演示(適合新手)
這篇文章主要介紹了Java三種獲取redis的連接及redis_String類型演示(適合新手),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12SpringBoot使用Shiro實(shí)現(xiàn)動(dòng)態(tài)加載權(quán)限詳解流程
本文小編將基于?SpringBoot?集成?Shiro?實(shí)現(xiàn)動(dòng)態(tài)uri權(quán)限,由前端vue在頁(yè)面配置uri,Java后端動(dòng)態(tài)刷新權(quán)限,不用重啟項(xiàng)目,以及在頁(yè)面分配給用戶?角色?、?按鈕?、uri?權(quán)限后,后端動(dòng)態(tài)分配權(quán)限,用戶無(wú)需在頁(yè)面重新登錄才能獲取最新權(quán)限,一切權(quán)限動(dòng)態(tài)加載,靈活配置2022-07-07Springboot使用redis實(shí)現(xiàn)接口Api限流的示例代碼
本文主要介紹了Springboot使用redis實(shí)現(xiàn)接口Api限流的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07SpringBoot項(xiàng)目后端開發(fā)邏輯全面梳理
這篇文章主要介紹了SpringBoot項(xiàng)目后端開發(fā)邏輯全面梳理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Java String index out of range:100錯(cuò)誤解決方案詳解
這篇文章主要介紹了Java String index out of range:100錯(cuò)誤解決方案詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08