文章目录
Mybatis动态sql
Mybatis的一个强大特性之一通常是它的动态SQL能力。
大概分为以下四种
1、 if
2、 choose,when,otherwise
3、 where set trim
4、 foreach
1. if(判断)
案例:查询男性用户,如果输入了用户名,按用户名模糊查询
在UserMapper接口中定义方法:
public List<User> queryUserListLikeUserName(@Param("userName")String userName);
mapper.xml:
<select id="queryUserListLikeUserName" resultType="User">
select * from tb_user where sex=1
<!-- if:判断
test:判断表达式 -->
<if test="userName!=null and userName.trim()!=''">
and user_name like '%' #{userName} '%'
</if>
</select>
2. choose when otherwise(选择)
案例:查询男性用户,如果输入了用户名则按照用户名模糊查找,否则如果输入了年龄则按照年龄查找,否则查找用户名为“zhangsan”的用户。
mapper.xml:
<select id="queryUserListLikeUserNameOrAge" resultType="User">
select * from tb_user where sex=1
<!-- choose:条件选择
when:test-判断条件,一旦有一个when成立,后续的when都不再执行
otherwise:所有的when都不成立时,才会执行 -->
<choose>
<when test="userName!=null and userName.trim()!=''">and user_name like '%' #{userName} '%'</when>
<when test="age != null">and age = #{age}</when>
<otherwise>and user_name = 'zhangsan' </otherwise>
</choose>
</select>
3. where(过滤)
案例:查询所有用户,如果输入了用户名按照用户名进行模糊查询,如果输入年龄,按照年龄进行查询,如果两者都输入,两个条件都要成立。
<select id="queryUserListLikeUserNameAndAge" resultType="User">
<!-- 1.自动添加where关键字
2.有一定的纠错功能:去掉sql语句块之前多余的一个and|or
通常结合if或者choose使用 -->
select * from tb_user
<where>
<if test="userName!=null and userName.trim()!=''">user_name like '%' #{userName} '%'</if>
<if test="age!=null">and age = #{age}</if>
</where>
</select>
4.set(用于修改操作)
案例:修改用户信息,如果参数user中的某个属性为null,则不修改。
<update id="updateUserSelective" >
UPDATE tb_user
<!-- 1.set自动添加set关键字
2.也有一定的纠错功能:自动去掉sql语句块之后多余的一个逗号 -->
<set>
<if test="userName!=null and userName.trim()!=''">user_name = #{userName},</if>
<if test="password!=null and password.trim()!=''">password = #{password},</if>
<if test="name!=null and name.trim()!=''">name = #{name},</if>
<if test="age!=null">age = #{age},</if>
<if test="sex!=null">sex = #{sex}</if>
</set>
WHERE
(id = #{id});
</update>
5. trim(兼具where和set标签的作用)
使用trim可以代替where 或者 set
改造where:
<select id="queryUserListLikeUserNameAndAge" resultType="User">
select * from tb_user
<!--
prefix:添加前缀
prefixOverrides:指定要去除sql语句块前的字符串
-->
<trim prefix="where" prefixOverrides="and|or">
<if test="userName!=null and userName.trim()!=''">user_name like '%' #{userName} '%'</if>
<if test="age!=null">and age = #{age}</if>
</trim>
</select>
改造 set:
<update id="updateUserSelective" >
UPDATE tb_user
<trim prefix="set" suffixOverrides=",">
<if test="userName!=null and userName.trim()!=''">user_name = #{userName},</if>
<if test="password!=null and password.trim()!=''">password = #{password},</if>
<if test="name!=null and name.trim()!=''">name = #{name},</if>
<if test="age!=null">age = #{age},</if>
<if test="sex!=null">sex = #{sex}</if>
</trim>
</update>
6. foreach(循环)
案例:按照多个id查询用户信息
<select id="queryUserListByIds" resultType="User">
select * from tb_user where id in
<!--
foreach:遍历集合
collection:接收的集合参数
item:遍历的集合中的一个元素
separator:分隔符
open:以什么开始
close:以什么结束
-->
<foreach collection="ids" item="id" separator="," open="(" close=")">#{id}</foreach>
</select>