基于mybatis中<include>標(biāo)簽的作用說明

MyBatis中sql標(biāo)簽定義SQL片段,include標(biāo)簽引用,可以復(fù)用SQL片段
sql標(biāo)簽中id屬性對應(yīng)include標(biāo)簽中的refid屬性。通過include標(biāo)簽將sql片段和原sql片段進(jìn)行拼接成一個完整的sql語句進(jìn)行執(zhí)行。
<sql id='sqlid'> res_type_id,res_type</sql><select resultType='com.property.vo.PubResTypeVO'> select <include refid='sqlid'/> from pub_res_type</select>
引用同一個xml中的sql片段
<include refid='sqlid'/>
引用公用的sql片段
<include refid='namespace.sqlid'/>
include標(biāo)簽中也可以用property標(biāo)簽,用以指定自定義屬性。
在sql標(biāo)簽中通過${}取出對應(yīng)的屬性值。
<select 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進(jìn)行輸出映射,只有查詢出來的列名和pojo中的屬性名一致,該列才可以映射成功。
如果查詢出來的列名和pojo的屬性名不一致,通過定義一個resultMap對列名和pojo屬性名之間作一個映射關(guān)系。
resultMap:適合使用返回值是自定義實(shí)體類的情況
resultType:適合使用返回值得數(shù)據(jù)類型是非自定義的,即jdk的提供的類型
補(bǔ)充:mybatis include標(biāo)簽傳參特性測試
1 前言mybatis的include標(biāo)簽主要是用于sql語句的可重用,并且可以接收參數(shù)來生成動態(tài)sql。為了進(jìn)一步了解include標(biāo)簽的傳參特性,我寫了一段測試代碼來測試一下include標(biāo)簽的特性。
2 測試代碼mapper.xml
<!--需要include的代碼塊--><sql id='luck'>#{luck}||’${luck}’</sql><!--property標(biāo)簽name屬性和參數(shù)名一樣,但值不同--><select resultType='java.lang.String'>select<include refid='luck'><property name='luck' value='lucktheuniverse'/></include>from dual</select><!--property標(biāo)簽name屬性和參數(shù)名一樣,但值為#號方式傳值--><select resultType='java.lang.String'>select<include refid='luck'><property name='luck' value='#{luck}'/></include>from dual</select><!--property標(biāo)簽name屬性和參數(shù)名一樣,但值為$方式傳值--><select resultType='java.lang.String'>select<include refid='luck'><property name='luck' value='${luck}'/></include>from dual</select><!--property標(biāo)簽name屬性和參數(shù)名不同--><select resultType='java.lang.String'>select<include refid='luck'><property name='luck1' value='lucktheuniverse'/></include>from dual</select>
mapper.java
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
String test1 = mapper.test1('luck123');String test2 = mapper.test2('luck123');String test3 = mapper.test1('luck123');String test4 = mapper.test2('luck123');
測試結(jié)果
test1: luck123lucktheuniversetest2: 報錯test3: luck123luck123test4: luck123luck1233 結(jié)論
1.采用${}取參數(shù)時,include標(biāo)簽的property屬性的優(yōu)先級要高于外圍mapper的參數(shù);
2.采用#{}取參數(shù)只能取到外圍mapper傳過來的參數(shù)。
4 test2報錯原因test2報錯是因?yàn)椋琲nclude中${luck}取了property中的#{luck},但是#{}自帶了雙引號。所以得到的sql就成了
select #{luck}||’#{luck}’ from dual
最終轉(zhuǎn)化為preparedStatement,會報java.sql.SQLException: 無效的列索引
select ?||’?’ from dual
’?’是不能被單引號 ’ 包圍的
所以要謹(jǐn)慎,不要在#{}傳入的參數(shù)周圍加上單引號
把include代碼塊修改為,可以得到輸出為luck123luck123
<sql id='luck'> #{luck}||${luck} </sql>
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. mysql的like模式2. MySQL分區(qū)的優(yōu)點(diǎn)3. Oracle根據(jù)逗號拆分字段內(nèi)容轉(zhuǎn)成多行的函數(shù)說明4. 加密你的Access數(shù)據(jù)庫asp打開方法5. MYSQL(電話號碼,身份證)數(shù)據(jù)脫敏的實(shí)現(xiàn)6. 什么是Access數(shù)據(jù)庫7. MySQL 字符串函數(shù):字符串截取8. 如何實(shí)現(xiàn)MySQL數(shù)據(jù)庫的備份與恢復(fù)9. mysql數(shù)據(jù)庫中最常用的時間轉(zhuǎn)換函數(shù)的用法10. mysql like語句問題

網(wǎng)公網(wǎng)安備