12.动态SQL


  • if
<select id="queryBlogIF" parameterType="map" resultType="blog">
    select * from mybatis.blog where 1=1
  <if test="title!=null">and title=#{title}</if>
  <if test="author!=null">and author=#{author}</if>
  • choose
        select * from mybatis.blog
     <where>
      <choose>
          <when test="title!=null">
              title=#{title}
          </when>
          <when test="author!=null">
             and author=#{author}
          </when>
         <otherwise>
             and views=#{views}
         </otherwise>
      </choose>

     </where>
    </select>
  • set
 <update id="updateBlog" parameterType="map">
        update mybatis.blog
        <set>
            <if test="title!=null">
                title = #{title},
            </if>
            <if test="author!=null">
                author=#{author},
            </if>
        </set>
        where id =#{id}
    </update>
  • foreach

<!-- select * from blog where 1=1 and (id=1 or id =2 or id=3) -->
    <select id="queryBlogForeach" parameterType="map" resultType="blog">
        select * from mybatis.blog
     <where>
     <foreach collection="ids" item="id" open="and (" close=")" separator="or">
         id=#{id}
     </foreach>

     </where>
    </select>

posted @ 2022-08-10 23:54  天井听雨  阅读(13)  评论(0编辑  收藏  举报