開(kāi)始第一個(gè)例子: Hello World
新建Java工程,設(shè)置maven依賴
新建maven工程,設(shè)置項(xiàng)目編譯級(jí)別為Java8及以上,引入fluent mybatis依賴包。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< 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)
1
2
3
4
5
6
7
8
9
10
11
12
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
@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)行刪除、插入、查詢、修改操作。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
@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)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/tryternity/article/details/109062608