可能很多童鞋都還不是很清楚在mybatis可以使用各種腳本語(yǔ)言來(lái)定義Mapper文件里面的動(dòng)態(tài)SQL;目前mybatis支持的腳本語(yǔ)言有XML(默認(rèn)的);Velocity和Freemarker三種。使用不同的腳本語(yǔ)言來(lái)完成Mapper文件的編寫,第一是使用自己熟悉的腳本語(yǔ)言,第二是能夠定義更多豐富的自定義指令來(lái)簡(jiǎn)化Mapper的開(kāi)發(fā),關(guān)于MyBatis支持腳本的原理分析,自定義腳本指令后面再寫文章分析,本文先介紹mybatis中velocity腳本的使用方式。
mybatis-velocity項(xiàng)目可以允許你方便的使用velocity作為腳本語(yǔ)言來(lái)完成Mapper文件中各種動(dòng)態(tài)sql的編寫。
注意,在腳本中大量使用velocity開(kāi)發(fā),如果不熟悉,可以先去看看velocity腳本;
安裝
在maven中添加
1
2
3
4
5
|
<dependency> <groupId>org.mybatis.scripting</groupId> <artifactId>mybatis-velocity</artifactId> <version> 1.2 </version> </dependency> |
注意我們使用的是mybatis-velocity1.2版本,該版本需要mybatis3.3支持;
在mybatis配置文件中,將velocity腳本引擎設(shè)置為默認(rèn)的mapper文件引擎:
1
2
3
4
5
6
7
|
<typeAliases> ... <typeAlias type= "org.mybatis.scripting.velocity.Driver" alias= "velocity" /> </typeAliases> <settings> ... <setting name= "defaultScriptingLanguage" value= "velocity" /> </settings> |
配置即完成。
接下來(lái)就可以在mapper文件中使用velocity腳本了:
1
2
3
4
|
<select id= "findPerson" lang= "velocity" > #set( $pattern = $_parameter.name + '%' ) SELECT * FROM person WHERE name LIKE @{pattern, jdbcType=VARCHAR} </select> |
注意:
如果使用了velocity,參數(shù)要使用@{}來(lái)引用,因?yàn)関elocity的指令就是以#開(kāi)頭的,比如#set #if等;
使用velocity腳本,在參數(shù)中也可以配置對(duì)應(yīng)的javaType和jdbcType;配置格式為:@{ property, attr1=val1, attr2=val2, ... };可配置項(xiàng)有javaType, jdbcType, mode, numericScale, resultMap, typeHandler, jdbcTypeName;
在velocity的指令中使用context中的變量,需要使用$_parameter來(lái)作為前綴引用,比如
1
2
3
4
|
# if ($_parameter.name) #set($_name = '%' +$_parameter.name+ '%' ) AND name LIKE @{_name} #end |
mybatis-velocity內(nèi)建的指令
除了velocity的指令,mybatis-velocity項(xiàng)目為mybatis定義了一些內(nèi)建的velocity指令:
trim
1
|
#trim( prefix prefixOverrides suffix suffixOverrides ) body #end |
其中的參數(shù)含義同XML的trim參數(shù)含義相同;
一個(gè)例子:
1
2
3
4
5
6
|
#trim( " WHERE " " AND| OR" ) # if ($_parameter.name) #set($_name = '%' +$_parameter.name+ '%' ) AND name LIKE @{_name} #end #end |
where
1
2
|
#where() body #end #where()同XML中的<where>相同,可以替換條件前的AND/OR,替換為WHERE;注意一定是\where()有括號(hào)的; |
mset
1
2
|
#mset() body #end #mset前面加一個(gè)m,為的是和velocity本身的#set指令區(qū)別,#mset等同于XML中的<set>元素,可以在條件前加上set語(yǔ)句,并且去掉set塊最后的分號(hào); |
一個(gè)例子:
1
2
3
4
5
6
7
8
9
|
<update id= "update" > UPDATE USER #mset() # if ($_parameter.name) name=@{name}, #end # if ($_parameter.age) age=@{age}, #end # if ($_parameter.bornDate) borndate=@{bornDate} #end #end WHERE id = @{id} </update> |
repeat
1
2
|
#repeat( collection var separator open close ) body #end #repeat指令和XML中的<foreach>元素相同,能夠方便的遍歷集合/數(shù)組類型元素,并使用其中的每一個(gè)元素: |
一個(gè)例子:
1
2
3
4
5
6
|
SELECT *FROM City #where() #repeat( $_parameter.ids $id "," " state_id IN (" ")" ) @{id} #end #end |
in
1
|
#in( collection var field ) body #end |
#in指令是一個(gè)新的指令,能夠快速的專門針對(duì)SQL中的IN條件生成對(duì)應(yīng)的field IN()語(yǔ)句;參數(shù)列表中,collection代表要遍歷的IN中的內(nèi)容;var代表遍歷中的每一個(gè)對(duì)象的臨時(shí)引用名稱;field代表在IN語(yǔ)句之前生成的字段名稱;
一個(gè)例子:
1
2
3
4
5
6
|
SELECT *FROM City #where() #in( $_parameter.ids $id "state_id" ) @{id} #end #end |
自定義指令
mybatis-velocity允許你方便的自定義自己的指令用于簡(jiǎn)化開(kāi)發(fā),自定義指令的步驟為:
在classpath中添加一個(gè) mybatis-velocity.properties配置文件;
創(chuàng)建自己的Velocity指令解析類;
將自己創(chuàng)建的Velocity指令解析類添加到配置文件中;
在mapper.xml文件中使用指令;
一個(gè)例子:
1
2
3
4
5
6
7
8
9
|
// User defined directivepackage com.myproject.directives; //自定義的指令類需要繼承Directive類; public class MyDirective extends Directive { } //mybatis-velocity.properties //如果有多個(gè)自定義指令類,使用分號(hào)隔開(kāi); userdirective=com.myproject.directives.MyDirective; // mapper xml file SELECT *FROM City #myDirective() ...... #end |
綜合使用
如果使用velocity-mybatis,一個(gè)典型的CRUD的mapper就可以看起來(lái)是這個(gè)樣子:
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
|
<mapper namespace= "mybatis.com._520it.mybatis.UserMapper" > <resultMap type= "User" id= "user_mapping" > <id column= "id" property= "id" /> <result column= "name" property= "name" /> <result column= "age" property= "age" /> <result column= "borndate" property= "bornDate" /> </resultMap> <insert id= "add" keyColumn= "id" keyProperty= "id" useGeneratedKeys= "true" > INSERT INTO USER(name,age,borndate) VALUES (@{name,javaType=string,jdbcType=VARCHAR},@{age},@{bornDate}) </insert> <update id= "update" > UPDATE USER #mset() # if ($_parameter.name) name=@{name}, #end # if ($_parameter.age) age=@{age}, #end # if ($_parameter.bornDate) borndate=@{bornDate} #end #end WHERE id = @{id} </update> <delete id= "delete" parameterType= "long" > DELETE FROM USER WHERE id = @{id} </delete> <sql id= "user_column" > id,name,age,borndate </sql> <select id= "get" resultMap= "user_mapping" > SELECT <include refid= "user_column" /> FROM USER WHERE id = @{id} </select> <select id= "list" resultMap= "user_mapping" > SELECT <include refid= "user_column" /> FROM USER </select> <select id= "listByName" resultMap= "user_mapping" parameterType= "string" > SELECT <include refid= "user_column" /> FROM USER WHERE name = @{name} </select> <select id= "queryBy" resultMap= "user_mapping" > SELECT id,name,age,borndate FROM USER #where() # if ($_parameter.name) #set($_name = '%' +$_parameter.name+ '%' ) AND name LIKE @{_name} #end #end # if ($_parameter.orderBy) ORDER BY @{orderBy} @{orderType} #end # if ($_parameter.pageSize>- 1 ) LIMIT @{start},@{pageSize} #end </select> </mapper> |
以上所述是小編給大家介紹的Mybatis velocity腳本的使用教程詳解(推薦),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://www.jianshu.com/p/cecc187410be