zoukankan      html  css  js  c++  java
  • MyBatis动态sql

    数据库文件在http://www.cnblogs.com/wtzl/p/8758624.html

    1.if---条件成立则拼接sql语句

    1 <!-- if -->
    2     <select id="getUserListByUser" resultType="Person" parameterType="Person">
    3         select * from smbms_user 
    4             where userName like CONCAT ('%',#{userName},'%')
    5             <if test="userRole !=null and userRole !=''">
    6              and userRole = #{userRole}
    7             </if>    
    8     </select>
    View Code

    2.where---条件符合则自动加上where 且去掉第一个and(如果有and)

    1 <!-- where 条件符合则自动加上where 且去掉第一个and-->
    2     <select id="getUser" resultType="Person" parameterType="Person">
    3         select * from smbms_user 
    4         <where>
    5             <if test="userName !=null and userName !=''">
    6                 and userName=#{userName}
    7             </if>
    8         </where>
    9     </select>
    View Code

    3.set---可以更新一个或多个值,并且条件不成立为自动去掉多余的逗号

     1 <!-- set可以更新一个或多个值,并且条件不成立为自动去掉多余的逗号 -->
     2     <update id="updateUser"  parameterType="Person">
     3         update smbms_user 
     4         <set>
     5             <if test="userName !=null and userName !=''">
     6                  userName=#{userName},
     7             </if>
     8             <if test="userPassword !=null and userPassword !=''">
     9                  userPassword=#{userPassword}
    10             </if>
    11         </set>
    12         where id=#{id}
    13     </update>
    View Code

    4.trim---prefix前缀   prefixOverrides去掉前缀             suffix后缀   suffixOverrides去掉后缀

    1 <!-- trim 去掉and 加上where     prefix前缀   prefixOverrides去掉前缀             suffix后缀   suffixOverrides去掉后缀  -->
    2     <select id="where" parameterType="Person" resultType="Person">
    3         select * from smbms_user
    4         <trim prefix="where" prefixOverrides="and" suffix="" suffixOverrides="">
    5             <if test="userName !=null and userName !=''">
    6                 and userName=#{userName}
    7             </if>
    8         </trim>
    9     </select>
    View Code

    5.choose---哪个分支条件成立就拼接哪个 , 都不成立就执行otherwise分支(相当于switch case default)

     1 <!-- 哪个分支条件成立就拼接哪个         都不成立就执行otherwise分支 -->
     2     <select id="choose" parameterType="Person" resultType="Person">
     3         select * from smbms_user where i=1
     4         <choose>
     5              <when test="userName !=null and userName !=''">
     6                  and userName=#{userName}
     7              </when>
     8              <when test="userPassword !=null and userPassword !=''">
     9                  and userPassword=#{userPassword}
    10              </when>
    11              <otherwise>
    12                  and phone=#{phone}
    13              </otherwise>
    14         </choose>
    15     </select>
    View Code

    6.foreach---作用等同于in(?,?,?)

    1 <!-- 等同于in(?,?,?) -->
    2     <select id="foreach"  resultType="Person">
    3         select * from smbms_user where userRole in 
    4         <foreach collection="list" item="roleid" open="(" separator="," close=")">
    5             #{roleid}
    6         </foreach>     
    7     </select>
    View Code

     扩展

    foreach实现多对多查询:

     1 <!-- 多个供应商对应多个订单 -->
     2     <select id="forprobill"  resultMap="probill">
     3         select b.*,p.* from smbms_provider p,smbms_bill b where p.id=b.providerId and p.id in 
     4         <foreach collection="array" item="roleid" open="(" separator="," close=")">
     5             #{roleid}
     6         </foreach>     
     7     </select>
     8      <resultMap type="Provider" id="probill">
     9         <id property="id" column="id"/>
    10         <collection property="billList" ofType="Bill">
    11             <id  property="id" column="id"/>
    12             <result property="productName" column="productName"/>
    13         </collection>
    14     </resultMap> 
    View Code

    传入map多值查询

    1 <!-- map传参多个值      in的参数对应collection参数 为map对应的key值rmap   gender值为map对应的key值gender  区分大小写-->
    2     <select id="userMap" resultType="Person" parameterType="Map">
    3         select * from smbms_user where userRole in 
    4         <foreach collection="rmap" item="roid" open="(" separator="," close=")">
    5             #{roid}
    6         </foreach>
    7          and gender=#{gender} 
    8     </select>
    View Code
  • 相关阅读:
    [华为oj]放苹果
    [华为oj]iNOC产品部-杨辉三角的变形
    值传递和引用传递
    Struts2+Jquery+Json集成
    使用堆栈结构进行字符串表达式("7*2-5*3-3+6/3")的计算
    使用环形链表解决约瑟夫(丢手帕)问题
    tomcat配置数据源
    ORA-01652:无法通过128(在表空间TEMP中)扩展temp段
    (转)Java 代码优化过程的实例介绍
    (转)走进JVM,浅水也能捉鱼
  • 原文地址:https://www.cnblogs.com/wtzl/p/8782309.html
Copyright © 2011-2022 走看看