07_动态SQL与模糊查询

1 需求

    实现多条件查询用户
  • 姓名模糊匹配
  • 年龄在指定的最小值到最大值之间

2 准备表和数据

  1. create table d_user(
  2. id int primary key auto_increment,
  3. name varchar(10),
  4. age int(3)
  5. );
  6. insert into d_user(name,age) values('Tom',12);
  7. insert into d_user(name,age) values('Bob',13);
  8. insert into d_user(name,age) values('Jack',18);

3 ConditionUser(查询条件实例类)

  1. private String name;
  2. private int minAge;
  3. private int maxAge;

4 userMapper.xml(映射文件)

  1. <mapper namespace="cn.imentors.mybatis.test7.userMapper">
  2. <!--
  3. 实现多条件查询用户(姓名模糊匹配, 年龄在指定的最小值到最大值之间)
  4. -->
  5. <select id="getUser" parameterType="ConditionUser" resultType="User">
  6. select * from d_user where
  7. <if test='name != "%null%"'>
  8. name like #{name} and
  9. </if>
  10. age between #{minAge} and #{maxAge}
  11. </select>
  12. </mapper>

5 测试

  1. @Test
  2. public void test(){
  3. SqlSessionFactory factory = MybatisUtils.getFactory();
  4. SqlSession session = factory.openSession();
  5. String statement = "cn.imentors.mybatis.test7.userMapper.getUser";
  6. String name = "o";
  7. name = null;
  8. ConditionUser parameter = new ConditionUser("%"+name+"%", 13, 18);
  9. List<User> list = session.selectList(statement, parameter);
  10. System.out.println(list);
  11. session.close();

6 MyBatis中可用的动态SQL标签

6.1 if

6.2 choose

  1. <select id=”findActiveBlogLike”parameterType=”Blog” resultType=”Blog”>
  2. SELECT * FROM BLOG WHERE state = „ACTIVE‟
  3. <choose>
  4. <when test=”title != null”>
  5. AND title like #{title}
  6. </when>
  7. <when test=”author != null and author.name != null”>
  8. AND title like #{author.name}
  9. </when>
  10. <otherwise>
  11. AND featured = 1
  12. </otherwise>
  13. </choose>
  14. </select>

6.3 where

  1. <select id=”findActiveBlogLike” parameterType=”Blog” resultType=”Blog”>
  2. SELECT * FROM BLOG
  3. <where>
  4. <if test=”state != null”>
  5. state = #{state}
  6. </if>
  7. <if test=”title != null”>
  8. AND title like #{title}
  9. </if>
  10. <if test=”author != null and author.name != null”>
  11. AND title like #{author.name}
  12. </if>
  13. </where>
  14. </select>

6.4 set

  1. <update id="updateAuthorIfNecessary"
  2. parameterType="domain.blog.Author">
  3. update Author
  4. <set>
  5. <if test="username != null">username=#{username},</if>
  6. <if test="password != null">password=#{password},</if>
  7. <if test="email != null">email=#{email},</if>
  8. <if test="bio != null">bio=#{bio}</if>
  9. </set>
  10. where id=#{id}
  11. </update>

6.5 foreach

  1. <select id="selectPostIn" resultType="domain.blog.Post">
  2. SELECT *
  3. FROM POST P
  4. WHERE ID in
  5. <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
  6. #{item}
  7. </foreach>
  8. </select>
注:你可以传递一个List实例或者数组作为参数对象传给MyBatis。当你这么做的时候,MyBatis会自动将它包装在一个Map中,用名称作为键。List实例将会以“list”作为键,而数组实例将会以“array”作为键


捐赠我们
    良师益友工作室一直在致力于帮助编程爱好更加快速方便地学习编程,如果您对我们的成果表示认同并且觉得对你有所帮助,欢迎您对我们捐赠^_^。
    
原文地址:https://www.cnblogs.com/imentors/p/4810470.html