Mybatis动态SQL
1.动态SQL:if语句
<select id="selectUserByIf" resultType="com.zhiyou100.mcl.bean.Users" parameterType="com.zhiyou100.mcl.bean.Users"> select * from user where <if test="username != null"> username=#{username} </if> <if test="username != null"> and sex=#{sex} </if> </select>
这样的代码有缺陷,如果第一个条件条件不成立那么sql语句拼接成select * from users where and sex=#{sex}从而产生sql错误,这个时候可以用if+where结合语句来避免错误
2.动态SQL:if+where语句
<select id="selectByWhere" parameterType="int" resultType="com.zhiyou100.mcl.bean.Users"> select * from users <where> <if test="id>0"> and id=#{id} </if> <if test="name!=null"> and name=#{name} </if> <if test="sex!=null"> and sex=#{sex} </if> <if test="age>0"> and age=#{age} </if> </where> </select>
这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉
3.动态SQL:if+set语句
<!-- 动态SQL:if 语句 --> <update id="updateUser" > update users <set> <if test="name!=null"> name=#{name}, </if> <if test="sex!=null"> sex=#{sex}, </if> <if test="age>0"> age=#{age} </if> where id=#{id} </set> </update>
这样写,如果第一个条件 username 为空,那么 sql 语句为:update users u set u.sex=? where id=?
如果第一个条件不为空,那么 sql 语句为:update users u set u.username = ? ,u.sex = ? where id=?
它会帮我们在条件成立时在第一个条件前加上set,并且会帮助我们把代码中间最后一个条件后的逗号去除。
4.动态SQL:choose(when,otherwise) 语句
当我们不想用到所有的查询条件,只想选择其中的一个,查询条件有一个满足即可,使用 choose 标签可以解决此类问题,类似于 Java 的 switch 语句
<!-- 动态SQL:choose(when,otherwise) 语句 -->
<select id="selectByChoose" resultType="com.zhiyou100.mcl.bean.Users">
select * from users
<where>
<choose>
<when test="id!=null and id!=''">
id=#{id}
</when>
<when test="name!=null and name!=''">
and name=#{name}
</when>
<when test="age!=null and age!=''">
and age=#{age}
</when>
<otherwise>
and sex=#{sex}
</otherwise>
</choose>
</where>
</select>
也就是说,这里我们有四个条件,id,name,age,sex只能选择一个作为查询条件
如果 id 不为空,那么查询语句为:select * from users where id=?
如果 id 为空,那么看name 是否为空,如果不为空,那么语句为 select * from users where username=?;
如果 name 为空,那么查询语句为 select * from users where sex=?
如果 sex为空,那么查询语句为 select * from users where age=?
5.动态SQL:trim 语句
trim标记是一个格式化的标记,可以完成set或者是where标记的功能
①、用 trim 改写上面第二点的 if+where 语句
<select id="selectBytrim" resultType="com.zhiyou100.mcl.bean.Users"> select * from users <trim prefix="where" prefixOverrides="and | or"> <if test="id>0"> and id=#{id} </if> <if test="name!=null"> and name=#{name} </if> <if test="sex!=null"> and sex=#{sex} </if> <if test="age>0"> and age=#{age} </if> </trim> </select>
prefix:前缀
prefixoverride:去掉第一个and或者是or
②、用 trim 改写上面第三点的 if+set 语句
<update id="updateUser2"> update users <trim prefix="set" suffixOverrides=","> <if test="name!=null"> name=#{name}, </if> <if test="sex!=null"> sex=#{sex}, </if> <if test="age>0"> age=#{age} </if> where id=#{id} </trim> </update>
suffix:后缀
suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)
6、动态SQL: SQL 片段
<sql id="public">
id,name,age,sex from users
</sql>
通过id调用sql片段。在第七点中会展示
①、最好基于 单表来定义 sql 片段,提高片段的可重用性
②、在 sql 片段中不要包括 where
7、动态SQL: foreach 语句
需求:我们需要查询 user 表中 id 分别为1,2,3的用户
sql语句:select * from users where id=1 or id=2 or id=3
select * from users where id in (1,2,3)
①、建立一个 UsersDao 类,里面封装一个 List<Integer> ids 的属性
package com.zhiyou100.mcl.dao; import java.util.List; import org.apache.ibatis.annotations.Param; import com.zhiyou100.mcl.bean.Users; public interface UsersDao { public void addUser(Users user); public void updateUser(Users user);
public void updateUser2(Users user); public List<Users> selectByWhere(Users user); public List<Users> selectByChoose(Users user); public List<Users> selectBytrim(Users user); public List<Users> selectByForeach(@Param("ids")List<Integer> ids); }
②、我们用 foreach 来改写 select * from users where id=1 or id=2 or id=3
<select id="selectByForeach" parameterType="com.zhiyou100.mcl.bean.Users" resultType="com.zhiyou100.bean.Users"> select * from user <where> <!-- collection:指定输入对象中的集合属性 item:每次遍历生成的对象 open:开始遍历时的拼接字符串 close:结束时拼接的字符串 separator:遍历对象之间需要拼接的字符串 select * from user where 1=1 and (id=1 or id=2 or id=3) --> <foreach collection="ids" item="id" open="(" close=")" separator="or"> id=#{id} </foreach> </where> </select>
测试:
public void testSelectUserByListId(){ String statement = "com.zhiyou100.mcl.UsersMapper.selectUserByForeach"; UsersDao usersDao== new UsersDao (); List<Integer> ids = new ArrayList<>(); ids.add(1); ids.add(2); ids.add(3); usersDao.setIds(ids); List<Users> list = usersDao.selectByForeach(ids); System.out.println(list); }
③、我们用 foreach 来改写 select * from users where id in (1,2,3)
<select id="selectByForeach" resultType="com.zhiyou100.mcl.bean.Users">
select
<!-- collection:指定输入对象中的集合属性 item:每次遍历生成的对象 open:开始遍历时的拼接字符串 close:结束时拼接的字符串 separator:遍历对象之间需要拼接的字符串 --> <-- 使用include refid="sql片段id号"调用 -->
<include refid="public"></include>
<where>
<foreach collection="ids" open="id in(" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>

浙公网安备 33010602011771号