MyBatis的动态SQL解析

动态SQL

在Mapper文件中编写

if

  • 最常用的判断语句,
  • 类似Java中的if语句,主要实现某些简单的条件选择
<!-- 根据客户工作获取客户信息 -->
 	<select id="findCustomerBySomethingIf" parameterType="customer" resultType="com.aqiuo.po.Customer">
 		select * from t_customer 
 		<where>
 			<if test="username!=null">
 				and username like concat('%',#{username},'%')
 			</if>
 			<if test="jobs!=null">
 				and jobs=#{jobs}
 			</if>
 			<if test="phone!=null">
 				and phone=#{phone}
 			</if>
 		</where>
 	</select>

Choose、when、otherwise

  • 在某些场景下需要多个选项中选择一个去执行
  • 此场景更适合
	<!-- 根据客户工作获取客户信息 -->
 	<select id="findCustomerBySomethingChoose" parameterType="customer" resultType="com.aqiuo.po.Customer">
 		select * from t_customer 
 		<where>
 			<choose>
 				<when test="jobs!=null">
 				jobs=#{jobs}
 			</when>
 				<when test="username!=null">
 					and username like concat('%',#{username},'%')
 			</when>
 			<otherwise>
 				and phone=#{phone}
 			</otherwise>
 			</choose>
 		</where>
 	</select>

where

  • 在编写的SQL后面都加上where 1=1条件有些冗余
  • 可以用替换
  • 只有在where元素内的条件成立时才会在SQL中加入where关键字,否则不会添加
  • 即使where之后的内容有多余的' and ' 或‘ OR ’ 元素也可以将他们去除
  • (见<if>)

set

  • 如果要更新一个对象,大多数要更新一个或多个字段
  • 如果更新一条数据要将他所有属性更新一遍。执行效率极差
  • 用元素来完成更新操作
  • 作用:在动态包含的SQL语句前输出一个SET关键字、并将SQL语句中最后一个多余的逗号去除
  • 注意:和进行update语句动态SQL组装时,如果元素内包含的内容为空,会出现SQL语法错误
  • 注意2:where语句别写在内,会有bug
  • <!-- 修改客户信息 -->
     	<update id="updateCustomer" parameterType="com.aqiuo.po.Customer">
     		update t_customer 
    		<set>
    			<if test="username!=null">
    				username=#{username},
    			</if>
    			
    			<if test="jobs!=null">
    				jobs=#{jobs},
    			</if>
    			<if test="phone!=null">
    				phone=#{phone},
    			</if>
    		
    		</set>
    		<where>
    				<if test="id!=null">
    				id=#{id}
    			</if>
    		</where>
     	</update>

foreach

  • 场景需要:假设1000条数据,现在要将id小于100的客户信息全部查询出来
    • 一条一条查询(累死)
    • Java中用循环,将查询语句放入循环体,效率极低
    • so,使用标签
  • MyBatis已经提供了一种用于数组和集合循环遍历的方式,拿就是元素
  • 属性描述
    • item:配置的是循环中当前的元素
    • index:配置的是当前元素的下标
    • collection:配置的list是传递过来的参数类(首字母小写),可以是array,list或collection,Map集合的键、POJO包装类中的数组或集合类型的属性名
    • open和close:配置的是开头和结束符号
    • separator:配置的是各个元素的间隔符
  • 注意:
    • 在使用时必须指定collection属性
      • 如果传入的是单参且类型是数组和List,collection属性值分别为array和list
 	<select id="findCustomerByForeach" parameterType="list" resultType="com.aqiuo.po.Customer">
 		select * from t_customer where id in
 			<foreach collection="list" item="id" open="(" close=")" separator="," >
 				#{id}
 			</foreach>
 		
 	</select>

                 如果是多个参数就要封装成一个map,这时collection的属性就要是map的键

 

                如果传入的参数是POJO包装类,collection属性值就为包装类中需要遍历的数组或集合的属性名

 

 <bind>

<!-- 根据客户编号获取客户信息 -->
 	<select id="findCustomerByUsername" parameterType="map" resultType="com.aqiuo.po.Customer">
 		<bind name="pattern_username" value="'%'+username+'%'" />
 		select *
 		from t_customer
 		 where username like #{pattern_username}
 	</select>

//没了? 噂嘟假嘟 O.o 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五敷有你

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值