动态

if标签:
if是做条件判断的

choose标签:
采用多个选项中找一个,就有点类似于switch。。case。

trim标签:
如果使用第一个if语句的话,就会发现没有写where标签就会报错。而这类标签通常是搭配条件标签使用的。

foreach标签:
用于循环,用于遍历,如果我们传入的参数是一个数组或者集合类,那么这个标签可以循环遍历。一般我们都是使用sql中的in语句时才使用。
if

<mapper namespace="studentNamespace">
	<resultMap type="Student" id="studentMap">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="sal" column="sal"/>
	</resultMap>
 
	<select id="findConditionalStudent" parameterType="map" resultMap="studentMap">
		select id,name,sal from students
		<where>
			<if test="pid !=null">
				and id=#{pid}
			</if>
			<if test="pname !=null">
				and name=#{pname}
			</if>
			<if test="psal !=null">
				and sal=#{psal}
			</if>
		</where>
	</select>
</mapper>

if另一种情况

<update id="dynamicUpdate" parameterType="map">
	update students 
	<set>
		<if test="pname !=null">
			name=#{pname},
		</if>
		<if test="psal !=null">
			sal=#{psal},
		</if>
	</set>
	where id=#{pid}
</update>

数组和集合

//数组
<delete id="dynamicDelete">
	delete from students where id in 
	<foreach collection="array" open="(" close=")" separator="," item="arr">
		#{arr}
	</foreach>
</delete>

//集合
<delete id="dynamicDeleteList">
	delete from students where id in 
	<foreach collection="list" open="(" close=")" separator="," item="ids">
		#{ids}
	</foreach>
</delete>

没有输入参数parameterType,由于使用了foreach的collection属性已经指定了输入参数为array,所以就没必要再写了。foreach用于迭代数组元素,open表示开始符号,close表示结束符号,separator表示元素间的分割符,item表示迭代的数组。如果我们写sql语句:delete from students where id in (1,3,4,6,7…);而开始符号就是(,结束符号就是),分割符号是,item是我们指定的迭代数组。

更详细的解释点我

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值