mybatis动态sql查询条件中list的判断及取值

如果Mapper接口中的方法如果只有一个参数,则xml动态sql中可以直接引用参数名,如果有多个参数,保险做法是使用@Param注解设置参数别名,这样可以在xml动态sql中使用参数别名。

如:List searchSometh(@Param("parama") String parama, @Param("paramb") List<String> paramb);

<select id="searchSometh" ..>
select * from some_table 
<trim prefix="where" prefixOverrides="AND | OR">
     <if test="parama != null">
        and field_a = #{parama,jdbcType=VARCHAR}
      </if>
      <if test="paramb != null">
          <choose>
             <when test="paramb.size()==1">and field_b=#{paramb[0]}</when>
             <when test="paramb.size()>1">
                 and field_b in 
                 <foreach item="item" index="index" collection="paramb" open="(" separator="," close=")">#{item}</foreach>
              </when>
          </choose>
      </if>
</trim>
</select>

   对于List类型的集合参数,foreach中的collection属性值,如果参数没有使用@Param注解设置参数别名,则collection属性值为“list”,否则使用参数别名

猜你喜欢

转载自hzwei206.iteye.com/blog/2387152
今日推荐