您的位置:首页 > 数据库

如何一文快速了解 Mybatis 的动态 SQL 语句编写

2020-04-21 20:22 627 查看

文章目录

  • 3、动态 SQL 之< where >标签
  • 4、动态标签之< foreach > 标签
  • 5、Mybatis 中简化编写的 SQL 片段
  • 1、动态SQL出现的背景

    动态SQL都是用于查询场景。在Mybatis 的映射文件中,有些时候业务逻辑复杂时,比如多条件的组合查询中,此时 SQL 语句是需要根据用户传入的参数来动态变化的,则以前普通的 SQL 语句就不能满足要求了。
    参考的官方文档,描述如下:

    2、动态 SQL 之 < if > 标签

      我们根据实体类的不同取值,使用不同的 SQL 语句来进行查询。比如在 id 如果不为空时可以根据 id 查询,如果 username 不同空时还要加入用户名作为条件。这种情况在我们的多条件组合查询中经常会碰到。

    2.1、持久层 Dao 接口

    /**
    * 根据用户信息,查询用户列表
    * @param user
    * @return
    */
    List<User> findByUser(User user);

    2.2、持久层 Dao 的映射配置

    <select id="findByUser" resultType="user" parameterType="user">
    select * from user where 1=1
    <if test="username!=null and username != '' ">
    and username like #{username}
    </if>
    <if test="address != null">
    and address like #{address}
    </if>
    </select>

      注意:< if >标签的 test 属性中写的是对象的属性名,如果是包装类的对象要使用 OGNL 表达式的写法。另外要注意 where 1=1 的作用 和 test属性中 and 连接符,不能在这里使用&符号表示‘且’

    3、动态 SQL 之< where >标签

    为了简化上面 where 1=1 的条件拼装,我们可以采用< where >标签来简化开发。

    3.1、持久层 Dao 的映射配置

    <!-- 根据用户信息查询 -->
    <select id="findByUser" resultType="user" parameterType="user">
    select * from user
    <where>
    <if test="username!=null and username != '' ">
    and username like #{username}
    </if>
    <if test="address != null">
    and address like #{address}
    </if>
    </where>
    </select>

    4、动态标签之< foreach > 标签

    4.1、需求和需求分析

    传入多个 id 查询用户信息,用下边两个 sql 实现:
    SELECT * FROM USERS WHERE username LIKE ‘%张%’ AND (id =10 OR id =89 OR id=16)
    SELECT * FROM USERS WHERE username LIKE ‘%张%’ AND id IN (10,89,16)
    这样我们在进行范围查询时,就要将一个集合中的值,作为参数动态添加进来。
    这样我们将如何进行不确定个数的同一列名参数的传递呢?

    4.2、在 QueryVo 中加入一个 List 集合用于封装参数

    /**
    *
    * <p>Title: QueryVo</p>
    * <p>Description: 查询的条件</p>
    */
    public class QueryVo implements Serializable {
    private List<Integer> ids;
    public List<Integer> getIds() {
    return ids;
    }
    public void setIds(List<Integer> ids) {
    this.ids = ids;
    }
    }

    4.3、持久层 Dao 接口

    /**
    * 根据 id 的集合查询用户
    * @param vo
    * @return
    */
    List<User> findInIds(QueryVo vo);

    4.4、持久层 Dao 的映射配置

    <!-- 查询所有用户在 id 的集合之中 -->
    <select id="findInIds" resultType="user" parameterType="queryvo">
    <!-- select * from user where id in (1,2,3,4,5); -->
    select * from user
    <where>
    <if test="ids != null and ids.size() > 0">
    <foreach collection="ids" open="id in ( " close=")" item="uid" separator=",">
    #{uid}
    </foreach>
    </if>
    </where>
    </select>

    SQL 语句:
      select 字段 from user where id in (…)
    < foreach >标签用于遍历集合,它的属性:
      collection:代表要遍历的集合元素,注意编写时不要写#{}
      open:代表语句的开始部分
      close:代表结束部分
      item:代表遍历集合的每个元素,生成的变量名
      separator:代表集合元素之间拼接的分隔符

    5、Mybatis 中简化编写的 SQL 片段

    Sql 中可将重复的 sql 提取出来,使用时用 include 标签引用即可,最终达到 sql 重用的目的。

    5.1、使用< sql >标签定义代码片段:

    <!-- 抽取重复的语句代码片段,设置被引用时的 id 标识 -->
    <sql id="defaultSql">
    select * from user
    </sql>

    5.2、使用< include >标签引用已定义的代码片段

    <!-- 引用1:配置查询所有操作 -->
    <select id="findAll" resultType="user">
    <include refid="defaultSql"></include>
    </select>
    
    <!-- 引用2:配置根据 id 查询 -->
    <select id="findById" resultType="UsEr" parameterType="int">
    <include refid="defaultSql"></include>
    where id = #{uid}
    </select>
    • 点赞
    • 收藏
    • 分享
    • 文章举报
    cjh-gdAcm997 发布了37 篇原创文章 · 获赞 5 · 访问量 3872 私信 关注
    内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: