MyBatis中sql標簽定義SQL片段,include標簽引用,可以復用SQL片段
sql標簽中id屬性對應include標簽中的refid屬性。通過include標簽將sql片段和原sql片段進行拼接成一個完整的sql語句進行執行。
1
2
3
4
5
6
7
8
|
<sql id= "sqlid" > res_type_id,res_type </sql> < select id= "selectbyId" resultType= "com.property.vo.PubResTypeVO" > select <include refid= "sqlid" /> from pub_res_type </ select > |
引用同一個xml中的sql片段
1
|
<include refid= "sqlid" /> |
引用公用的sql片段
1
|
<include refid= "namespace.sqlid" /> |
include標簽中也可以用property標簽,用以指定自定義屬性。
在sql標簽中通過${}取出對應的屬性值。
1
2
3
4
5
6
7
8
9
|
< select id= "queryPubResType" parameterType= "com.property.vo.PubResTypeVO" resultMap= "PubResTypeList" > select a.res_type_id, <include refid= "com.common.dao.FunctionDao.SF_GET_LNG_RES_TYPE" > <property name = "AI_RES_TYPE_ID" value= "a.res_type_id" /> <property name = "lng" value= "#{lngId}" /> <property name = "female" value= "'女'" /> </include> as res_type from pub_res_type a </ select > |
使用resultType進行輸出映射,只有查詢出來的列名和pojo中的屬性名一致,該列才可以映射成功。
如果查詢出來的列名和pojo的屬性名不一致,通過定義一個resultMap對列名和pojo屬性名之間作一個映射關系。
resultMap
:適合使用返回值是自定義實體類的情況
resultType
:適合使用返回值得數據類型是非自定義的,即jdk的提供的類型
補充:mybatis include標簽傳參特性測試
1 前言
mybatis的include標簽主要是用于sql語句的可重用,并且可以接收參數來生成動態sql。為了進一步了解include標簽的傳參特性,我寫了一段測試代碼來測試一下include標簽的特性。
2 測試代碼
mapper.xml
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
|
<!--需要include的代碼塊--> < sql id = "luck" > #{luck}||'${luck}' </ sql > <!--property標簽name屬性和參數名一樣,但值不同--> < select id = "test1" resultType = "java.lang.String" > select < include refid = "luck" > < property name = "luck" value = "lucktheuniverse" /> </ include > from dual </ select > <!--property標簽name屬性和參數名一樣,但值為#號方式傳值--> < select id = "test2" resultType = "java.lang.String" > select < include refid = "luck" > < property name = "luck" value = "#{luck}" /> </ include > from dual </ select > <!--property標簽name屬性和參數名一樣,但值為$方式傳值--> < select id = "test3" resultType = "java.lang.String" > select < include refid = "luck" > < property name = "luck" value = "${luck}" /> </ include > from dual </ select > <!--property標簽name屬性和參數名不同--> < select id = "test4" resultType = "java.lang.String" > select < include refid = "luck" > < property name = "luck1" value = "lucktheuniverse" /> </ include > from dual </ select > |
mapper.java
1
2
3
4
|
String test1( @Param (value = "luck" ) String luck); String test2( @Param (value = "luck" ) String luck); String test3( @Param (value = "luck" ) String luck); String test4( @Param (value = "luck" ) String luck); |
test.java
1
2
3
4
|
String test1 = mapper.test1( "luck123" ); String test2 = mapper.test2( "luck123" ); String test3 = mapper.test1( "luck123" ); String test4 = mapper.test2( "luck123" ); |
測試結果
1
2
3
4
|
test1: luck123lucktheuniverse test2: 報錯 test3: luck123luck123 test4: luck123luck123 |
3 結論
1.采用${}取參數時,include標簽的property屬性的優先級要高于外圍mapper的參數;
2.采用#{}取參數只能取到外圍mapper傳過來的參數。
4 test2報錯原因
test2報錯是因為,include中${luck}取了property中的#{luck},但是#{}自帶了雙引號。所以得到的sql就成了
1
|
select #{luck}|| '#{luck}' from dual |
最終轉化為preparedStatement,會報java.sql.SQLException: 無效的列索引
1
|
select ?|| '?' from dual |
'?'是不能被單引號 ' 包圍的
所以要謹慎,不要在#{}傳入的參數周圍加上單引號
把include代碼塊修改為,可以得到輸出為luck123luck123
1
2
3
|
<sql id= "luck" > #{luck}||${luck} </sql> |
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。如有錯誤或未考慮完全的地方,望不吝賜教。
原文鏈接:https://www.cnblogs.com/yhgn/p/11187304.html