• 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>
    
  • 相关阅读:
    YTU 2625: B 构造函数和析构函数
    YTU 2623: B 抽象类-形状
    YTU 2622: B 虚拟继承(虚基类)-沙发床(改错题)
    YTU 2621: B 继承 圆到圆柱体
    YTU 2620: B 链表操作
    YTU 2619: B 友元类-计算两点间距离
    刷题总结——切蛋糕(ssoj)
    刷题总结——拦截导弹(ssoj)
    算法复习——费用流模板(poj2135)
    算法复习——网络流模板(ssoj)
  • 原文地址:https://www.cnblogs.com/abel-huang/p/7784090.html
Copyright © 2020-2023  润新知