动态条件查询

多条件查询

第一种方法:
<select id="dim" resultType="DimDyl.Demo05.Dio">
        select * from tb_brand
        where 1 = 1
        <if test="id != null">
            and id = #{id}
        </if>
        <if test="Pp_ename != null and Pp_ename != ''">
            and Pp_ename  like #{Pp_ename}
        </if>
        <if test="Qy_ename != null and Qy_ename != ''">
            and Qy_ename  like #{Qy_ename};
        </if>
    </select>
第二种方法:
<select id="dim" resultType="DimDyl.Demo05.Dio">
select * from tb_brand
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="Pp_ename != null and Pp_ename != ''">
and Pp_ename like #{Pp_ename}
</if>
<if test="Qy_ename != null and Qy_ename != ''">
and Qy_ename like #{Qy_ename};
</if>
</where>
</select>

单一条件查询

<select id="dim" resultType="DimDyl.Demo06.Dyl1">
       select * from tb_brand

      <where>  <!--没值是不会执行where语句的-->
          <choose>    <!--相当于switch-->
              <when test="id != null">
                  id  = #{id}
              </when>
              <when test="Pp_ename != null and Pp_ename != '' ">
                  Pp_ename = #{Pp_ename}
              </when>
              <when test="Qy_ename != null and Qy_ename != ''">
                  Qy_ename = #{Qy_ename}
              </when>

          </choose>
      </where>

   </select>

您可能还喜欢...