mybatis中sql标签定义sql片段,include标签引用,可以复用sql片段

sql标签中id属性对应include标签中的refid属性。通过include标签将sql片段和原sql片段进行拼接成一个完整的sql语句进行执行。

<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片段

<include refid="sqlid"/>

引用公用的sql片段

<include refid="namespace.sqlid"/>

include标签中也可以用property标签,用以指定自定义属性。

在sql标签中通过${}取出对应的属性值。

<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

	<!--需要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

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: luck123lucktheuniverse
test2: 报错
test3: luck123luck123
test4: luck123luck123

3 结论

1.采用${}取参数时,include标签的property属性的优先级要高于外围mapper的参数;

2.采用#{}取参数只能取到外围mapper传过来的参数。

4 test2报错原因

test2报错是因为,include中${luck}取了property中的#{luck},但是#{}自带了双引号。所以得到的sql就成了

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

最终转化为preparedstatement,会报java.sql.sqlexception: 无效的列索引

select ?||'?' from dual

‘?’是不能被单引号 ‘ 包围的

所以要谨慎,不要在#{}传入的参数周围加上单引号

把include代码块修改为,可以得到输出为luck123luck123

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。如有错误或未考虑完全的地方,望不吝赐教。