Mybatis_03_动态语句

1.if标签

test里面的userName是实体类的属性。

    <!--根据条件查询-->
    <select id="findUserByConditon" resultMap="userMap" parameterType="user">
        select * from user where 1=1
        <if test="userName!=null">
          and username=#{userName}
        </if>
        <if test="sex!=null">
            and sex=#{sex}
        </if>

    </select>

2.where标签  不用谢where 1=1

    <!--根据条件查询-->
    <select id="findUserByConditon" resultMap="userMap" parameterType="user">
        select * from user
       <where>
            <if test="userName!=null">
              and username=#{userName}
            </if>
            <if test="sex!=null">
                and sex=#{sex}
            </if>
           </where>
    </select>

3.foreach标签

open一个(  close 一个)  item是每一项  separator使用 ',' 分隔。

 <!--根据queryVo中的id集合查询用户列表-->
    <select id="findUserInIds" resultMap="userMap" parameterType="queryVo">
      select * from user
      <where>
        <if test="ids!=null and ids.size()>0">
            <foreach collection="ids" open="and id in (" close=")" item="uid" separator=",">
               #{uid}
            </foreach>
        </if>

      </where>

    </select>
    

猜你喜欢

转载自www.cnblogs.com/cmdzhizhu/p/11113234.html