zoukankan      html  css  js  c++  java
  • Mybatis映射文件中的标签的使用

    <foreach>

     <!-- foreach -->
        <delete id="delMulti" parameterType="java.util.List">
           delete from user where id in
             <!--collection:输入参数为List集合时,必须写list,
                 item:为集合里的每一项起名,可以任意定义
                 separator:每一项中间的分割符
                 open:在执行循环体之前拼接的内容;
                 close:在执行循环体之后拼接的内容;-->
             <foreach collection="list" item="uid" separator="," open="(" close=")">
                #{uid}
             </foreach>
        </delete>

    where

     <!--where:解析成where关键字,会自动去掉第一个符合条件的限定条件中的and -->
        <select id="getByNameSex" parameterType="map" resultType="user">
           select * from user 
           <where>
              <if test="uname!=null and uname!=''">
                and  username like "%"#{uname}"%"
              </if>
              <if test="usex!=null and usex!=''">
                 and sex=#{usex}
              </if>
           </where>
    choose when otherwise
      <!--choose when otherwise:某个判断满足条件后,其他条件就不会再执行  -->
        <select id="getByNameSex1" parameterType="map" resultType="user">
           select * from user where
           <choose>
              <when test="uname!=null and uname!=''">
                 username like "%"#{uname}"%"
              </when>
              <when test="usex!=null and usex!=''">
                 sex=#{usex}
              </when>
              <otherwise>
                  1=1
              </otherwise>
           </choose>
        </select>

    set

    <!-- set:解析为set关键字,可以自动去掉最后一个更新的字段后面的逗号 -->
        <update id="updUser" parameterType="user">
           update  user
           <set>
              <if test="username!=null and username!=''">
                  username=#{username}, 
              </if>
              <if test="sex!=null and sex!=''">
                  sex=#{sex}
              </if>
           </set>
           where id=#{id}
        </update>

    trim:使用次数较少

    <update id="updUser1" parameterType="user">
          <!--prefix:前缀,在trim中内容执行之前拼接
              suffix:后缀:在trim中内容执行之后拼接
              suffixOverrides:忽略后缀
              prefixOverrides:忽略前缀-->
          <trim prefix="update  user set" suffix="where id=#{id}" suffixOverrides=",">
              
                  <if test="username!=null and username!=''">
                      username=#{username}, 
                  </if>
                  <if test="sex!=null and sex!=''">
                      sex=#{sex}
                  </if>
              
          </trim>
        </update>

    bind使用较少

    <select id="getByUname" parameterType="string" resultType="User">
          <!--name:新的字符串的变量名  -->
          <bind name="uname" value="'%'+_parameter+'%'"/>
          select * from user where username like #{uname}
       </select>
  • 相关阅读:
    Linux下C程序插入执行shell脚本
    #ifdef预编译相关用法
    LAMP开发之环境搭建(2014.12.7在ubuntu下)
    Qt在VS2010的安装与配置
    vs2010配备boost编程环境
    Ubuntu虚拟机与Window、Arm的通信
    大小端测试程序
    Ubuntu安装google Gtest
    设计模式之单例模式
    设计模式之原型模式
  • 原文地址:https://www.cnblogs.com/meani/p/12001659.html
Copyright © 2011-2022 走看看