动态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