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

    1.动态SQL基本标签

    •if

    •choose (when, otherwise)

    •trim (where, set)

    •foreach

    2.IF 具体用法

    <select id="XX"
     resultType="Blog">
     SELECT * FROM BLOG
     WHERE state = ‘ACTIVE’
     <if test="title != null">
     AND title like #{title}
     </if>
    </select>
    

      多个IF注意第二个if语句里的表达式

    <select id="XX"
     resultType="Blog">
     SELECT * FROM BLOG WHERE state = ‘ACTIVE’
     <if test="title != null">
     AND title like #{title}
     </if>
     <if test="author != null and author.name != null">
     AND author_name like #{author.name}
     </if>
    </select>
    

      3.类似 if ,else用法例子

    <select id="XX"
     resultType="Blog">
     SELECT * FROM BLOG WHERE state = ‘ACTIVE’
     <choose>
     <when test="title != null">
     AND title like #{title}
     </when>
     <when test="author != null and author.name != null">
     AND author_name like #{author.name}
     </when>
     <otherwise>
     AND featured = 1
     </otherwise>
     </choose>
    

      4.Where用法例子

    <select id="XX"
     resultType="Blog">
     SELECT * FROM BLOG
     WHERE
     <if test="state != null">
     state = #{state}
     </if>
     <if test="title != null">
     AND title like #{title}
     </if>
     <if test="author != null and author.name != null">
     AND author_name like #{author.name}
     </if>
    </select>
    

      

    <select id="XX"
     resultType="Blog">
     SELECT * FROM BLOG
     <where>
     <if test="state != null">
     state = #{state}
     </if>
     <if test="title != null">
     AND title like #{title}
     </if>
     <if test="author != null and author.name != null">
     AND author_name like #{author.name}
     </if>
     </where>
    </select>
    

    6.trim用法

    <trim prefix="WHERE" prefixOverrides="AND |OR ">
     ...
    </trim>
    

      7.set用法注意set是sql语句中update table set.....不是将一个参数set到一个属性中

    <update id="updateAuthorIfNecessary">
     update Author
     <set>
     <if test="username != null">username=#{username},</if>
     <if test="password != null">password=#{password},</if>
     <if test="email != null">email=#{email},</if>
     <if test="bio != null">bio=#{bio}</if>
     </set>
     where id=#{id}
    </update>

    8.foreach用法

    <select id="selectPostIn" resultType="domain.blog.Post">
     SELECT *
     FROM POST P
     WHERE ID in
     <foreach item="item" index="index" collection="list"
     open="(" separator="," close=")">
     #{item}
     </foreach>
    </select>
    

      

      

  • 相关阅读:
    WSP部署错误—SharePoint管理框架中的对象“SPSolutionLanguagePack Name=0”依赖其他不存在的对象
    Elevate Permissions To Modify User Profile
    Error with Stsadm CommandObject reference not set to an instance of an object
    ASP.NET MVC3添加Controller时没有Scaffolding options
    测试使用Windows Live Writer写日志
    配置TFS 2010出现错误—SQL Server 登录的安全标识符(SID)与某个指定的域或工作组帐户冲突
    使用ADO.NET DbContext Generator出现错误—Unable to locate file
    CSS
    HTML DIV标签
    数据库
  • 原文地址:https://www.cnblogs.com/maybo/p/5183585.html
Copyright © 2011-2022 走看看