基于mybatis中<include>標(biāo)簽的作用說(shuō)明
MyBatis中sql標(biāo)簽定義SQL片段,include標(biāo)簽引用,可以復(fù)用SQL片段
sql標(biāo)簽中id屬性對(duì)應(yīng)include標(biāo)簽中的refid屬性。通過(guò)include標(biāo)簽將sql片段和原sql片段進(jìn)行拼接成一個(gè)完整的sql語(yǔ)句進(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>
引用同一個(gè)xml中的sql片段
<include refid='sqlid'/>
引用公用的sql片段
<include refid='namespace.sqlid'/>
include標(biāo)簽中也可以用property標(biāo)簽,用以指定自定義屬性。
在sql標(biāo)簽中通過(guò)${}取出對(duì)應(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)行輸出映射,只有查詢出來(lái)的列名和pojo中的屬性名一致,該列才可以映射成功。
如果查詢出來(lái)的列名和pojo的屬性名不一致,通過(guò)定義一個(gè)resultMap對(duì)列名和pojo屬性名之間作一個(gè)映射關(guān)系。
resultMap:適合使用返回值是自定義實(shí)體類(lèi)的情況
resultType:適合使用返回值得數(shù)據(jù)類(lèi)型是非自定義的,即jdk的提供的類(lèi)型
補(bǔ)充:mybatis include標(biāo)簽傳參特性測(cè)試
1 前言mybatis的include標(biāo)簽主要是用于sql語(yǔ)句的可重用,并且可以接收參數(shù)來(lái)生成動(dòng)態(tài)sql。為了進(jìn)一步了解include標(biāo)簽的傳參特性,我寫(xiě)了一段測(cè)試代碼來(lái)測(cè)試一下include標(biāo)簽的特性。
2 測(cè)試代碼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ù)名一樣,但值為#號(hào)方式傳值--><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');
測(cè)試結(jié)果
test1: luck123lucktheuniversetest2: 報(bào)錯(cuò)test3: luck123luck123test4: luck123luck1233 結(jié)論
1.采用${}取參數(shù)時(shí),include標(biāo)簽的property屬性的優(yōu)先級(jí)要高于外圍mapper的參數(shù);
2.采用#{}取參數(shù)只能取到外圍mapper傳過(guò)來(lái)的參數(shù)。
4 test2報(bào)錯(cuò)原因test2報(bào)錯(cuò)是因?yàn)椋琲nclude中${luck}取了property中的#{luck},但是#{}自帶了雙引號(hào)。所以得到的sql就成了
select #{luck}||’#{luck}’ from dual
最終轉(zhuǎn)化為preparedStatement,會(huì)報(bào)java.sql.SQLException: 無(wú)效的列索引
select ?||’?’ from dual
’?’是不能被單引號(hào) ’ 包圍的
所以要謹(jǐn)慎,不要在#{}傳入的參數(shù)周?chē)由蠁我?hào)
把include代碼塊修改為,可以得到輸出為luck123luck123
<sql id='luck'> #{luck}||${luck} </sql>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. mysql innodb的重要組件匯總2. MySQL修改安全策略時(shí)報(bào)錯(cuò):ERROR 1193 (HY000)的解決辦法3. Mybatis 實(shí)現(xiàn)一個(gè)搜索框?qū)Χ鄠€(gè)字段進(jìn)行模糊查詢4. Oracle 9i輕松取得建表和索引DDL語(yǔ)句5. MySQL 數(shù)據(jù)丟失排查案例6. MySQL大數(shù)據(jù)下Limit使用7. Access日期與時(shí)間函數(shù)匯總8. 使用SQL語(yǔ)句獲取SQL Server數(shù)據(jù)庫(kù)登錄用戶權(quán)限9. 用 Node + MySQL 處理 100G 數(shù)據(jù)10. SQL?DCL數(shù)據(jù)控制語(yǔ)言的使用
