MyBatis动态SQL

元素

元素作用
if条件判断,进行动态
choose类似于 switch,与 when 和 otherwise 搭配
where可以替代 where 过滤 and ,or
set替代 set 动态更新SQL
trim我感觉很强,有四个属性 prefix,suffix, prefixOverrides, suffixOverrides
foreach迭代,可以用于 in

if+where

我这里是判断是否为空。
当where元素里面没有值的时候 where 也不会插入 ,会自动过滤and和or
	<select id="getProviderList" resultType="Provider">
	 	select * from smbms_provider
	 	<where>
	 		<if test="proCode!=null and proCode!=''">
	 			and proCode LIKE CONCAT('%',#{proCode},'%')
	 		</if>
	 		<if test="proName!=null and proName!=''">
	 			and proName LIKE CONCAT('%',#{proName},'%')
	 		</if>
	 	</where>
	</select>

if+set

这里也是判断非空,来决定是否更改那一列。
set元素 会自动过滤“ , ”(逗号)
<update id="modify" parameterType="User">
		UPDATE smbms.smbms_user
		 <set>
		 	<if test="userCode!=null">userCode = #{userCode},</if>
		 	<if test="userName!=null"> userName = #{userName} ,</if>
		 	<if test="userPassword!=null">userPassword = #{userPassword} ,</if>
		 	<if test="gender!=null">gender = #{gender} ,</if>
		 	<if test="birthday!=null">birthday = #{birthday} , </if>
		 	<if test="phone!=null">phone = #{phone} ,</if>
		 	<if test="address!=null">address = #{address},</if>
		 	<if test="userRole!=null">userRole = #{userRole} ,</if>
		 	<if test="createdBy!=null">createdBy = #{createdBy} ,</if>
		 	<if test="creationDate!=null">creationDate = #{creationDate} ,</if>
		 	<if test="modifyBy!=null">modifyBy = #{modifyBy} ,</if>
		 	<if test="modifyDate!=null">modifyDate = #{modifyDate},</if>
		 </set>
		  WHERE id = #{id} 	
	</update>

choose

test 表达式判断
满足一个后面都不执行,都不满足就执行 otherwise 元素
<select id="getUserList_choose" resultType="User">
		select * from smbms_user where 1=1
		<choose>
			<when test="userName!=null and userName!=''">
				and userName like CONCAT('%',#{userName},'%')
			</when>
			<when test="userCode!=null and userCode!=''">
				and userCode like CONCAT('%',#{userCode},'%')
			</when>
			<when test="userRole!=null">
				and userRole=#{userRole}
			</when>
			<otherwise>
				and  YEAR(creationDate) =YEAR(#{creationDate})
			</otherwise>
		</choose>
	</select>

if-trim

trim 有四个属性 
	prefix:前缀,trim元素里面 有值就在 trim 包含的内容前加上前缀
	suffix:后缀,内容后加上后缀
	prefixOverrides:trim 包含的内容的首部进行指定内容忽略
	suffixOverrides:trim 包含的内容的尾部进行指定内容忽略
	<update id="modifyProvider" parameterType="Provider">
		UPDATE smbms.smbms_provider 
		<trim prefix="set" suffixOverrides="," suffix="WHERE id = #{id} ;">
			<if test="proCode!=null and proCode!=''">proCode = #{proCode} ,</if>
			<if test="proName!=null and proName!=''">proName = #{proName} , </if>
			<if test="proDesc!=null and proDesc!=''">proDesc = #{proDesc} ,</if>
			<if test="proContact!=null and proCode!=''">proContact = #{proContact} , </if>
			<if test="proPhone!=null and proPhone!=''">proPhone = #{proPhone} , </if>
			<if test="proAddress!=null and proAddress!=''">proAddress = #{proAddress} , </if>
			<if test="proFax!=null and proFax!=''">proFax = #{proFax} , </if>
			<if test="modifyDate!=null and modifyDate!=''">modifyDate = #{modifyDate} , </if>
			<if test="modifyBy!=null">modifyBy = #{modifyBy},</if>
		</trim>
	</update>

foreach

基本属性
	item:集合中每个元素的别名
	index: 指定一个名称,代表每次迭代到的位置
	open:表示语句以什么东西开始
	separator:每次迭代的值以什么符号分隔
	close:表示以什么结束
	collection:传参进来的类型 
			list集合传参 他值就是 list;数组 就是 array;
			map集合里面如果有list集合  并且遍历list集合 这个的值就为 这个list在 map集合里面的KEY
	<!-- 这就是map里面有list -->
	<!-- 根据用户角色列表和性别(多参数),获取该角色列表下用户列表信息 map -->
	<select id="getUserByConditionMap_foreach_map" resultMap="userMapByRole">
		select * from smbms_user where gender=#{gender} and userRole in
		<foreach collection="roleIds" item="roleIdList" open="(" separator="," close=")">
			#{roleIdList}
		</foreach>
	</select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值