6、动态SQL

if

<!-- resultType为HashMap  -->
<!-- 别名作为map的key -->	
<!-- 动态SQL -->
<!-- 在where后面加1=1或者使用<where>标签解决and问题,<where>标签会自动处理第一个and -->
<select id="queryAllPersonByNameOrAgeWithSQLTag" parameterType="Person" resultType="HashMap" >
	select id "id",name "name" from Person
	<where>
	    <if test="name!=null and name!='' ">
		    and name=#{name}
		</if>
		<if test="age!=null and age!=0 ">
		    and age=#{age}
		</if>
	</where>
</select>

for

<select id="queryAllPersonByNameOrAgeWithSQLTag" parameterType="Person" resultType="HashMap" >
	select id "id",name "name" from Person
	<where>
	    <if test="ids!=null and ids.size>0 ">
	        <!-- open里面开头空格不能省略 -->
		    <foreach collection="ids" open=" and stuno in (" close=")" item="id" separator=",">
		       #{id}
		    </foreach>
		</if>
	</where>
</select>


4是分隔符

输入参数为集合属性、集合、数组、对象数组

如果输入参数是简单类型数组,例如:parameterType=“int[]”
则 test="array!=null and array.length>0 ",即输入参数名固定为array,与传入参数名无关
如果输入参数是集合,例如:parameterType=“list”
则 test="list!=null and list.size>0 ",即输入参数名固定为list,与传入参数名无关
如果输入参数是对象数组,则parameterType=“Object[]”,foreach里面还是array
在这里插入图片描述

SQL片段,对于重复率比较高的sql片段,可以提取出来

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值