zoukankan      html  css  js  c++  java
  • MyBatis 示例-动态 SQL

    MyBatis 的动态 SQL 包括以下几种元素:

    详细的使用参考官网文档:http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html

    本章内容简单描述这些动态 SQL 在使用的过程中需要注意的地方。

    choose, when, otherwise

    比如我们要实现如下功能:

    • 当学生姓名不为空,则只用学生姓名作为条件查询
    • 当学生性别不为空,则只用学生性别作为条件查询
    • 当学生姓名和学生性别都为空,则要求学生学生证件号不为空

    针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。

    <select id="listByConditions" parameterType="studentQuery" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from t_student
        <where>
            <choose>
                <when test="name != null and name != ''">
                    AND name LIKE CONCAT('%', #{name}, '%')
                </when>
                <when test="sex != null">
                    AND sex = #{sex}
                </when>
                <otherwise>
                    AND selfcard_no is not null
                </otherwise>
            </choose>
        </where>
    </select>

    trim, where, set

    比如下面的动态 SQL 有什么问题?

    <select id="listByConditions" parameterType="studentQuery" resultMap="BaseResultMap">
        SELECT id, name, sex, selfcard_no, note
        FROM t_student
        WHERE
        <if test="ids != null and ids.size() > 0">
          id IN
          <foreach collection="ids" item="item" open="(" close=")" separator=",">
            #{item}
          </foreach>
        </if>
        <if test="name != null and name != ''">
          AND name LIKE CONCAT('%', #{name}, '%')
        </if>
        <if test="sex != null">
          AND sex = #{sex}
        </if>
        <if test="selfcardNo != null">
          AND selfcard_no = #{selfcardNo}
        </if>
    </select>

    如果这些条件没有一个能匹配上会发生什么?最终这条 SQL 会变成这样:

    SELECT id, name, sex, selfcard_no, note
    FROM t_student
    WHERE

    这样会导致 SQL 语句执行失败。如果仅仅第二个条件匹配又会怎样?这条 SQL 最终会是这样:

    SELECT id, name, sex, selfcard_no, note
    FROM t_student
    WHERE
    AND name LIKE CONCAT('%', 'a', '%')

    这个查询也会失败。

    MyBatis 提供了 <where> 元素可以解决上面的问题,将上面的动态 SQL 改成如下形式。

    <select id="listByConditions" parameterType="studentQuery" resultMap="BaseResultMap">
        SELECT id, name, sex, selfcard_no, note
        FROM t_student
        <where>
            <if test="ids != null and ids.size() > 0">
                AND id IN
                <foreach collection="ids" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="name != null and name != ''">
                AND name LIKE CONCAT('%', #{name}, '%')
            </if>
            <if test="sex != null">
                AND sex = #{sex}
            </if>
            <if test="selfcardNo != null">
                AND selfcard_no = #{selfcardNo}
            </if>
        </where>
    </select>

    foreach

    实现批量新增功能,动态 SQL 如下:

    <insert id="batchInsertByNoAutoInc" parameterType="list">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            select if(max(id) is null, 1, max(id) + 2) as newId from t_student
        </selectKey>
        insert into t_student (name, sex, selfcard_no, note)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.name,jdbcType=VARCHAR},
            #{item.sex,jdbcType=TINYINT},
            #{item.selfcardNo,jdbcType=BIGINT},
            #{item.note,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>

    批量新增的操作需要确保 list 中必须有值。

    MyBatis 实用篇

    MyBatis 概念

    MyBatis 示例-简介

    MyBatis 示例-类型处理器

    MyBatis 示例-传递多个参数

    MyBatis 示例-主键回填

    MyBatis 示例-动态 SQL

    MyBatis 示例-联合查询

    MyBatis 示例-缓存

    MyBatis 示例-插件

    求关注,求点赞,《架构学习》持续更新、完善、纠正 https://www.yuque.com/yinjianwei/vyrvkf
  • 相关阅读:
    1010 Radix (25 分)(二分)【回顾】
    1089 Insert or Merge (25 分)(two pointers)【回顾】
    1084 Broken Keyboard (20 分)(字符串处理)
    Listener监听器之HttpSessionListener
    在IE中如何在用户直接关闭窗口前清除Session
    C#文件操作简单封装
    几个实用的对String类的扩展
    C# 数据加密解密
    在IIS6中配置asp.net MVC网站时HTTP 错误 500.21 Internal Server Error解决方案
    C#常用的正则表达式
  • 原文地址:https://www.cnblogs.com/yinjw/p/11757136.html
Copyright © 2011-2022 走看看