zoukankan      html  css  js  c++  java
  • mybatis 动态sql

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

      动态SQL实际使用的元素并不多,但是它们带来了灵活性,减少许多工作量的同时,也在很大程度上提高了程序的可读性和可维护性。下面讨论这些动态SQL元素的用法。

    if元素

      if元素是最常用的判断语句,相当于Java中的if语句,它常常与test属性联合使用。
    <select id="findRoles" parameterType="string" resultMap="roleResultMap">
        select role_no, role_name, note
        from t_role
        where 1=1
        <if test="roleName != null and roleName !=''">
            and role_name like concat('%', #{roleName}, '%')
        </if>
    </select>

    choose、when、otherwise元素

      类似switch...case...default...功能的语句。在映射器的动态语句中choose、when、otherwise这3个元素承担了这个功能。
    <select id="findRoles3" parameterType="role" resultMap="roleResultMap">
        select role_no, role_name, note from t_role where 1=1
        <choose>
            <when test="roleNo != null and roleNo !=''">AND role_no = #{roleNo}</when>
            <when test="roleName != null and roleName !=''">AND role_name like concat('%', #{roleName}, '%')</when>
            <otherwise>AND note is not null</otherwise>
        </choose>
    </select>

    trim、where、set元素

    <select id="findRoles3" parameterType="role" resultMap="roleResultMap">
        select role_no, role_name, note from t_role
        <where>
            <if test="roleName != null and roleName !=''">and role_name like concat('%', #{roleName}, '%')</if>
            <if test="note != null and note !=''">and note like concat('%', #{note}, '%')</if>
        </where>
    </select>
    <select id="findRoles" parameterType="string" resultMap="roleResultMap">
        select role_no, role_name, note from t_role
        <trim prefix="where" prefixOverrides="and">
            <if test="roleName != null and roleName !=''">and role_name like concat('%', #{roleName}, '%')</if>
        </trim>
    </select>
      trim元素意味着要去掉一些特殊的字符串,当时prefix代表的是语句的前缀,而prefixOverrides代表的是需要去掉哪种字符串。上面的写法基本与where是等效的。
    <update id="updateRole" parameterType="role">
        update t_role
        <set>
            <if test="roleName != null and roleName !=''">role_name = #{roleName},</if>
            <if test="note != null and note != ''">note = #{note}</if>
        </set>
        where role_no = #{roleNo}
    </update>

      set元素遇到了逗号,它会把对应的逗号去掉

    foreach元素

      foreach元素是一个循环语句,它的作用是遍历集合,它能够很好地支持数组和List、Set接口的集合,对此提供遍历功能。它往往用于SQL中的in关键字。
    <select id="findUserBySex" resultType="user">
        select * from t_role
        where role_no in
        <foreach item="roleNo" index="index" collection="roleNoList" open="(" separator="," close=")">#{roleNo}</foreach>
    </select>
    •collection配置的roleNoList是传递进来的参数名称,它可以是一个数组、List、Set等集合。
    •item配置的是循环中当前的元素。
    •index配置的是当前元素在集合的位置下标。
    •open和close配置的是以什么符号将这些集合元素包装起来。
    •separator是各个元素的间隔符。
      在SQL中常常用到in语句,但是对于大量数据的in语句要特别注意,因为它会消耗大量的性能。此外,还有一些数据库的SQL对执行的SQL长度有限制,所以使用它时要预估一下collection对象的长度,以避免出现类似问题。

    用test的属性判断字符串

      test用于条件判断语句,它在MyBatis中使用广泛。test的作用相当于判断真假,在大部分场景中,它都是用以判断空和非空的。有时候需要判断字符串、数字和枚举等。所以十分有必要讨论一下它的用法。通过if元素的介绍,可以知道如何判断非空。但是如果用if语句判断字符串呢?
    <select id="getRoleTest" parameterType="string" resultMap="roleResultMap">
        select role_no, role_name, note from t_role <if test=" type == ‘Y’.toString()">where 1=1</if>
    </select>
      如果把type=’Y’传递给SQL,就可以发现MyBatis加入了条件where 1=1。换句话说,这条语句判定成功了,所以对于字符串的判断,可以通过加入toString()的方法进行比较。它可以判断数值型的参数。对于枚举而言,取决于使用何种typeHandler。

    bind元素

      bind元素的作用是通过OGNL表达式去自定义一个上下文变量,这样更方便使用。在进行模糊查询时,如果是MySQL数据库,常常用到的是一个concat,它用“%”和参数相连。然而在Oracle数据库则没有,Oracle数据库用连接符号“||”,这样SQL就需要提供两种形式去实现。但是有了bind元素,就不必使用数据库的语言,而是使用MyBatis的动态SQL即可完成。
    <select id="findRole" parameterType="string" resultType="com.learn.chapter5.mybatis.bean.RoleBean">
        <bind name="pattern" value="'%' + _parameter + '%'"/>
        SELECT id, role_name as roleName, create_date as createDate, end_date as endFlag, end_flag as endFlag, note 
        FROM t_role where role_name like #{pattern}
    </select>
      这里的“_parameter”代表的是传递进来的参数,它和通配符(%)连接后赋给了pattern,然后就可以在select语句中使用这个变量进行模糊查询了。无论是MySQL还是Oracle都可以使用这样的语句,提高了代码的可移植性。
      因为MyBatis支持多个参数使用bind元素的用法,所以传递多个参数也没有问题。 
  • 相关阅读:
    开始几天的基本学习
    从这个博客开始我的机器学习深度学习之路
    剑指Offer:面试题3——二维数组中的查找(java实现)
    HIVE配置文件
    C++ 之旅:前言
    leetcode 349:两个数组的交集I
    python学习(三):matplotlib学习
    python学习(二):python基本语法
    Android环境搭建
    LeetCode:237
  • 原文地址:https://www.cnblogs.com/ooo0/p/10958914.html
Copyright © 2011-2022 走看看