zoukankan      html  css  js  c++  java
  • 2017-10-14-MyBaits动态SQL

    MyBaits动态SQL

    if

    <select id="getDeptLikeByName" parameterType="string" resultMap="deptMap">
            select id, dept_id, dept_name from dept where 1=1
            <if test="_parameter != null and _parameter != '' " >
            and dept_name like concat('%', #{deptName}, '%')
            </if>
    </select>
    

    注意_parameter的使用,如果直接使用deptName,会报错,
    org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'deptName' in 'class java.lang.String

    choose、when、 otherwise

    <select id="getDeptByIdOrName" parameterType="dept" resultMap="deptMap">
             select id, dept_id, dept_name from dept where 1=1
                    <choose>
                            <when test="deptId > 0">
                                    and dept_id = #{deptId}
                            </when>
                            <when test="deptName != null and deptName != '' " >
                                    and dept_name like concat('%', #{deptName}, '%')
                            </when>
                            <otherwise>
                                    and  id > 0
                            </otherwise>
                    </choose>
    </select>
    

    trim、 where、 set

    不知道怎么表述了,where用法如下

    <select id="getDeptLikeByName" parameterType="string" resultMap="deptMap">
                    select id, dept_id, dept_name from dept
                    <where>
                            <if test="_parameter != null and _parameter != '' " >
                                    and dept_name like concat('%', #{deptName}, '%')
                            </if>
                    </where>
     
            </select>
    

    trim用法类似,也是用于去除一些SQL的特殊字段;
    set主要用于更新一个表中的部分字段,如果在更新表把所有的字段都传输回服务器,对性能影响很大。

    <update id="updateDept" parameterType="Dept">
                    update dept
        <set>
            <if test="deptId > 0 ">
                                    dept_id = #{deptId},
                            </if>
                            <if test="deptName != null and deptName !='' ">
                                    dept_name = #{deptName}
                            </if>
                    </set>
         where id = #{id}
    </update>
    

    foreach

    foreach用于遍历集合, 往往用于SQL语句中的in语句。

      <select id="listDept" resultMap="deptMap">
                    select *  from dept where id in
                    <foreach item = "item" index = "index" collection="list"
                             open="(" separator="," close=")">
                            #{item}
                    </foreach>
            </select>
    

    test

    主要用于条件判断。

    bind

    主要用于自定义一个上下文变量,如模糊查询,可以方便的在其他地方使用。

      <select id="getDeptLikeByName" parameterType="string" resultMap="deptMap">
                    <bind name="pattern" value="'%' + _parameter +'%'"/>
                    select id, dept_id, dept_name from dept
                    <where>
                            <if test="_parameter != null and _parameter != '' " >
                                    and dept_name like #{pattern}
                            </if>
                    </where>
     
            </select>
    
  • 相关阅读:
    Netflix Ribbon(负载均衡)介绍
    Annotation 注解
    框架设计的灵魂-反射
    idea maven java.lang.outofmemoryerror gc overhead limit exceeded
    洛谷P4427 [BJOI2018]求和
    洛谷P1196 [NOI2002]银河英雄传说
    CF191C Fools and Roads
    洛谷P2296 寻找道路
    洛谷P3389 【模板】高斯消元法
    洛谷P1351 联合权值
  • 原文地址:https://www.cnblogs.com/abel-huang/p/7784090.html
Copyright © 2011-2022 走看看