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的提供的类型
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>
|
以上为个人经验,希望能给大家一个参考,也希望大家多多支持快网idc。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://www.cnblogs.com/yhgn/p/11187304.html
相关文章
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-26 79
-
2025-05-24 65
-
详解PHP中instanceof关键字及instanceof关键字有什么作用
2025-05-29 55 -
2025-05-29 42
-
2025-06-04 65