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>
    
  • 相关阅读:
    Oracle(二)常用操作语句
    Oracle(一)概念理解
    Spring MVC实现文件上传和下载
    Spring MVC 的执行流程
    Spring MVC原理及配置详解
    idea创建maven web项目
    Spring Bean的生命周期
    integer和int的区别
    web项目搜索框智能提示
    html-tab page
  • 原文地址:https://www.cnblogs.com/abel-huang/p/7784090.html
Copyright © 2011-2022 走看看