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>
    
  • 相关阅读:
    Android高级开发第一讲如何在Android应用中避免内存溢出OOM问题
    Windows Phone 31 日谈——第24日:嵌入字体
    Windows Phone 31 日谈——第22日:应用?还是 游戏?
    Windows Phone 7 开发探索笔记6——页面间传值
    Windows Phone 31 日谈——第23日:提供试用版应用程序
    Windows Phone 7 开发探索笔记1——触控操作之Touch
    修改windowsphone7的默认起始页面
    ObjectC 入门(转)
    Windows Phone 7 开发探索笔记9——菜单栏
    Windows Phone 7 开发探索笔记5——页面间导航
  • 原文地址:https://www.cnblogs.com/abel-huang/p/7784090.html
Copyright © 2011-2022 走看看