MyBatis3_06_动态SQL

动态SQL是指sql语句是动态变化的,根据条件不同,所执行的sql语句是随条件动态变化

比如说查询,查询条件是不定的,根据查询条件的不同,来查询到满足的结果。

1.if 条件   choose,2.when 和 otherwise 条件   3.where 条件   4.trim 条件   5.foreach 循环     6.set 条件
通过上一篇表的例子。

根据不同条件查询学生。 


if条件

在StudentMapper 定义接口: 

public List<Student> searchStudent(Map<String, Object> map);

 根据输入的Map,返回学生集合。

在xml中实现方法:

	<select id="searchStudent" parameterType="HashMap" resultMap="StudentResult">
		select * from t_student s join t_grade g on s.gradeId=g.id join t_address a on s.addressId=a.id
		where 1=1
		<if test="name!=null">
			and name like #{name}
		</if>
		<if test="age!=null">
			and age=#{age}
		</if>
		<if test="gradeName!=null">
			and gradeName like #{gradeName}
		</if>
		<if test="sheng!=null">
			and sheng like #{sheng}
		</if>
		
	</select>
</mapper>

注意一下几点:where是必须写的。在这里写的是一个1=1.


when 和 otherwise 条件

可能有的时候需求是根据用户输入的一个特定的条件来查询。

注意:只能根据一个条件查询。并且这个条件是用户选择的。

这个时候用when otherwise适合。

定义查询接口2:

public List<Student> searchStudent2(Map<String, Object> map);

 在xml中实现方法:

	<select id="searchStudent2" parameterType="HashMap" resultMap="StudentResult">
		select * from t_student s join t_grade g on s.gradeId=g.id join t_address a on s.addressId=a.id
		<choose>
			<when test="searchBy=='gradeName'">
				where gradeName like #{gradeName}
			</when>
			<when test="searchBy=='sheng'">
				where sheng like #{sheng}
			</when>
			<otherwise>
				
			</otherwise>
		</choose>
	</select>

where 条件

1,自动加上 where;

2,如果 where 子句以 and 或者 or 开头,则自动删除第一个 and 或者 or;

在if条件的查询中,我们加了个where 1=1.这样是开发中不常用的:逻辑性差,并且性能不高

采用where标签。

	<select id="searchStudent3" parameterType="HashMap" resultMap="StudentResult">
		select * from t_student s join t_grade g on s.gradeId=g.id join t_address a on s.addressId=a.id
		<where>
			<if test="name!=null">
				and name like #{name}
			</if>
			<if test="age!=null">
				and age=#{age}
			</if>
			<if test="gradeName!=null">
				and gradeName like #{gradeName}
			</if>
			<if test="sheng!=null">
				and sheng like #{sheng}
			</if>
		</where>
	</select>

 


trim 条件

功能和 where 元素类似,提供了前缀,后缀功能,更加灵活

xml中:

	<select id="searchStudent4" parameterType="HashMap" resultMap="StudentResult">
		select * from t_student s join t_grade g on s.gradeId=g.id join t_address a on s.addressId=a.id
		<trim prefix="where" prefixOverrides="and/or" >
			<if test="name!=null">
				and name like #{name}
			</if>
			<if test="age!=null">
				and age=#{age}
			</if>
			<if test="gradeName!=null">
				and gradeName like #{gradeName}
			</if>
			<if test="sheng!=null">
				and sheng like #{sheng}
			</if>
		</trim>
	</select>

 prefix是指前缀。

prefixOverrides是自动删除第一个and或or


foreach 循环 

	<select id="searchStudent5" parameterType="HashMap" resultMap="StudentResult">
		select * from t_student s join t_grade g on s.gradeId=g.id join t_address a on s.addressId=a.id
		<where>
			<if test="name!=null">
				and name like #{name}
			</if>
			<if test="names!=null">
				name in
				<foreach collection="names" index="name" open="(" close=")" separator=",">
					#{name}
				</foreach>
			</if>	
			<if test="age!=null">
				and age=#{age}
			</if>
			<if test="gradeName!=null">
				and gradeName like #{gradeName}
			</if>
			<if test="sheng!=null">
				and sheng like #{sheng}
			</if>
		</where>
	</select>

其实就是数据库中的In的用法,


set 条件

1,自动加上 set; 2,自动剔除最后一个逗号“,”;

	<update id="update" parameterType="Student">
		update t_student 
		<set>
			<if test="name!=null">
				name=#{name},
			</if>
			<if test="age!=null">
				age=#{age},
			</if>
		</set>
		where id=#{id}
	</update>

 注意的是:逗号不能掉,最后要有where

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值