国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

基于mybatis中<include>標簽的作用說明

瀏覽:133日期:2023-10-19 18:34:13

基于mybatis中<include>標簽的作用說明

MyBatis中sql標簽定義SQL片段,include標簽引用,可以復用SQL片段

sql標簽中id屬性對應include標簽中的refid屬性。通過include標簽將sql片段和原sql片段進行拼接成一個完整的sql語句進行執(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標簽中也可以用property標簽,用以指定自定義屬性。

在sql標簽中通過${}取出對應的屬性值。

<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進行輸出映射,只有查詢出來的列名和pojo中的屬性名一致,該列才可以映射成功。

如果查詢出來的列名和pojo的屬性名不一致,通過定義一個resultMap對列名和pojo屬性名之間作一個映射關系。

resultMap:適合使用返回值是自定義實體類的情況

resultType:適合使用返回值得數(shù)據(jù)類型是非自定義的,即jdk的提供的類型

補充:mybatis include標簽傳參特性測試

1 前言

mybatis的include標簽主要是用于sql語句的可重用,并且可以接收參數(shù)來生成動態(tài)sql。為了進一步了解include標簽的傳參特性,我寫了一段測試代碼來測試一下include標簽的特性。

2 測試代碼

mapper.xml

<!--需要include的代碼塊--><sql id='luck'>#{luck}||’${luck}’</sql><!--property標簽name屬性和參數(shù)名一樣,但值不同--><select resultType='java.lang.String'>select<include refid='luck'><property name='luck' value='lucktheuniverse'/></include>from dual</select><!--property標簽name屬性和參數(shù)名一樣,但值為#號方式傳值--><select resultType='java.lang.String'>select<include refid='luck'><property name='luck' value='#{luck}'/></include>from dual</select><!--property標簽name屬性和參數(shù)名一樣,但值為$方式傳值--><select resultType='java.lang.String'>select<include refid='luck'><property name='luck' value='${luck}'/></include>from dual</select><!--property標簽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');

測試結果

test1: luck123lucktheuniversetest2: 報錯test3: luck123luck123test4: luck123luck1233 結論

1.采用${}取參數(shù)時,include標簽的property屬性的優(yōu)先級要高于外圍mapper的參數(shù);

2.采用#{}取參數(shù)只能取到外圍mapper傳過來的參數(shù)。

4 test2報錯原因

test2報錯是因為,include中${luck}取了property中的#{luck},但是#{}自帶了雙引號。所以得到的sql就成了

select #{luck}||’#{luck}’ from dual

最終轉化為preparedStatement,會報java.sql.SQLException: 無效的列索引

select ?||’?’ from dual

’?’是不能被單引號 ’ 包圍的

所以要謹慎,不要在#{}傳入的參數(shù)周圍加上單引號

把include代碼塊修改為,可以得到輸出為luck123luck123

<sql id='luck'> #{luck}||${luck} </sql>

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。

相關文章:
主站蜘蛛池模板: 齐河县| 溆浦县| 祁连县| 鹤岗市| 肃宁县| 天祝| 西林县| 云安县| 涟源市| 泗水县| 古丈县| 安乡县| 万安县| 柏乡县| 郯城县| 新宾| 平江县| 日喀则市| 北安市| 崇义县| 常宁市| 海阳市| 邯郸县| 潞西市| 米泉市| 海淀区| 陈巴尔虎旗| 沾化县| 临夏县| 平湖市| 民县| 腾冲县| 黄龙县| 临城县| 澄迈县| 迭部县| 五华县| 泉州市| 龙泉市| 花莲县| 易门县|