您的位置:首页 > 数据库

一双跑鞋的mybatis(四)---动态sql语句

2014-05-12 14:47 387 查看
<?xml version="1.0" encoding="UTF-8" ?> 
    <!DOCTYPE mapper 
      PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
   "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
   <mapper namespace="org.senssic.bean.Address">
      
   <insert id="add" parameterType="org.senssic.bean.Address">
      insert into address(addrs,code) value(#{addrs},#{code})
   </insert>
   <!-- if -->
   <select id="slectif" parameterType="map" resultType="org.senssic.bean.Address">
      select * from address where addrs like #{addrs}
      <if test="code!=null">
         and code=#{code}
      </if>
    </select>
    <!-- choose,when,otherwise -->
      <select id="slectchoose" parameterType="map" resultType="org.senssic.bean.Address">
      select * from address where 
    <choose>
       <when test="code!=null">
           and code=#{code}
       </when>
       <when test="addrs!=null">
          and addrs like #{addrs}
       </when>
       <otherwise>
         and active=1
       </otherwise>
    </choose>
    </select>
    <!-- where避免and或者无条件时候 -->
    <select id="selectwhere" parameterType="map" resultType="org.senssic.bean.Address">
      select * from address
      <where>
         <if test="code!=null">
            code=#{code}
         </if>
         <if test="addrs!=null and active!=null">
           and addrs=#{addrs} and active=#{active}
         </if>
      </where>
      
    </select>
    <!-- set 动态设置set方法 -->
     <update id="updateAddress" parameterType="map">
        update address 
        <set>
           <if test="code!=null">code=#{code}</if>
           <if test="addrs!=null">addrs=#{addrs}</if>
        </set>
        where id=#{id}
        
     </update>
     <!-- foreach构建与in条件中,可以将外部的集合(list,数组等作为查询的子条件) -->
     <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>
   </mapper>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: