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
查看更多关于基于mybatis中<include>标签的作用说明的详细内容...