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

    ? 动态SQL在xml中支持的标签

    • if
    • choose
    • where
    • trim
    • foreach
    • bind

    if

    if在where中使用

    <select id="findById" resultType="SysUser" parameterType="map">
        select * from sys_user;
        <where>
            <if test="userName !=null and userName!=''">
                and userName=#{userName}
            </if>
            <if test="userInfo !=null and userInfo!=''">
                and userInfo=#{userInfo}
            </if>
        </where>
    </select>
    

    test属性值是一个符合OGNL要求的判断表达式,结果为true或false。在数值中,所有的非0都是true,只有0为false。

    • property!=null:适用于任何类型的字段,用于判断是否为空

    • property!='':判断字符串

    • and 和 or:and -> &&,可以使用小括号分组

      模糊查询:

    name like concat('%',#{name},'%')
    name like '%${name}%'
    

    在update中使用if

    <update id="update" parameterType="SysUser">
        update sys_user
        <set>
            <if test="userName !=null and userName!=''">
                and userName=#{userName},
            </if>
            <if test="userInfo !=null and userInfo!=''">
                and userInfo=#{userInfo},
            </if>
            <if test="headImg!=null">
                headImg=#{headImg, jdbcType=BLOB},
            </if>
            <if test="createTime!=null">
                createTime=#{createTime, jdbcType=timestamp},
            </if>
        </set>
        where id=#{id}
    </update>
    

    trim

    使用trim实现where

    <trim prefix="where" prefixOverrides="and |or ">
        <if test="xx">
        </if>
    </trim>
    

    foreach

    foreach实现in

    <foreach collection="list" open="(" close=")" separator="," item="id" index="i">
    	#{id}
    </foreach>
    

    findAll(List)

    • collection的值:list|array|collection|_parameter

    list,array,collection,_parameter都是map参数的key

    / DefaultSqlSession.wrapCollection方法

      private Object wrapCollection(final Object object) {
        if (object instanceof Collection) {
          StrictMap<Object> map = new StrictMap<>();
          map.put("collection", object);
          if (object instanceof List) {
            map.put("list", object);
          }
          return map;
        } else if (object != null && object.getClass().isArray()) {
          StrictMap<Object> map = new StrictMap<>();
          map.put("array", object);
          return map;
        }
        return object;
      }
    

    foreach实现批量插入

    <insert id="insertList" parameterType="list">
        insert into user(
        id,username,password
        )
        values
        <foreach collection="list" item="user" separator=",">
            (
            #{id},#{username},#{password}
            )
        </foreach>
    </insert>
    

    foreach实现动态更新

    当参数为Map的时候,foreach的index不是索引,而是map的key,利用这个key实现动态更新

    <update id="updateByMap" parameterType="map">
        update sys_user
        set 
        <foreach collection="_parameter" item="val" index="key" separator=",">
            #{key}=#{val}
        </foreach>
        where id=#{id}
    </update>
    

    updateByMap(new HashMap<String, Object>());

    bind

    bind标签中可以使用OGNL表达式

    • 模糊查询
    <select id="findAll">
        select * from sys_user
        <where>
            <if test="username!=null and username!=''">
                and username like concat('%',#{username},'%')
            </if>
        </where>
    </select>
    

    使用了MySQL的函数(concat),不兼容

    • 替代方法,使用字符串拼接
    <select id="findAll">
        select * from sys_user
        <where>
            <if test="username!=null and username!=''">
                <bind name="nameLike" value="'%'+username+'%'"/>
                and username like #{nameLike}
            </if>
        </where>
    </select>
    

    bind中的OGNL表达式可以防止SQL注入

    mybatis适配不同的数据库,生成不同的SQL

    • mybat-config.xml
      <databaseIdProvider type="DB_VENDOR">
        <property name="SQL Server" value="sqlserver"/>
        <property name="DB2" value="db2"/>
        <property name="Oracle" value="oracle"/>
        <property name="MySql" value="mysql"/>
        <property name="PostgreSQL" value="postgresql"/>
        <property name="Derby" value="derby"/>
        <property name="HSQL" value="hsqldb"/>
        <property name="H2" value="h2"/>
      </databaseIdProvider>
    
    • mapper.xml
    <select id="findAll" databaseId="mysql">
        select * from sys_user
        <where>
            <if test="username!=null and username!=''">
                and username like concat('%',#{username},'%')
            </if>
        </where>
    </select>
    <select id="findAll" databaseId="oracle">
        select * from sys_user
        <where>
            <if test="username!=null and username!=''">
                and username like '%'||#{username}||'%'
            </if>
        </where>
    </select>
    
  • 相关阅读:
    借助backtrace和demangle实现异常类Exception
    mysql数据库表里首行和末行的取得方法
    斐波那契高效算法(4种算法综合分析)
    sublime学习之--批量在多行首位加入代码
    Zabbix --&gt; client安装以及怎样加入数据库的动态监控视图
    sdut 3-6 静态数据成员与静态成员函数
    poj3411Paid Roads
    python中get pass用法
    Qt之QListWidget:项目的多选与单选设置
    Qt之QListWidget:项目的多选与单选设置
  • 原文地址:https://www.cnblogs.com/zhuxiang1633/p/11571234.html
Copyright © 2011-2022 走看看