这是用mybatis的xml文件来模糊查询不定参数的sql写法:
1.对应的java的interface:
public interface MyMapper {
List<Apple> queryAppleByName(@Param("names")String[] names);
}
2.对应的xml文件:
<select id="queryAppleByName" resultMap="BaseResultMap">
select size,age from apple@orcl
where 1=1
<if test="names!=null and names.length>0" > <!-- 先判断传来的数组是否为空 -->
and
<foreach collection="names" item="name" index="index" separator="OR">
name like '%'||#{name} ||'%'
</foreach>
</if>
</select>
说明:要先判断传来的数组是否为空,再进行循环,有几个值就迭代几次。