项目中where条件中用到in,我理所当然的拼了个字符串传进去了,郁闷的是程序一直运行正常,测试case一直没有覆盖到这种情况,今天发现了,原来是程序的问题,我以为mybatis有bug呢。。。。。故记下此问题,留作笔记。
1.解决方法(多参数)
Map.xml
- <select id="getEntityList" resultType="App" parameterType="map">
- select * from t_app
- where status=#{status}
- <if test="flag!=null ">
- and id not in
- <foreach item="item" index="index" collection="ids" open="("
- separator="," close=")">
- #{item}
- </foreach>
- </if>
- </select>
传入的参数为Map<String,Object>
数据:status:1
ids:int[]{101,103,61,75}
2.一个参数
- a.如果参数的类型是List, 则在使用时,collection属性要必须指定为 list
findByIds(List<Long> ids)
- <select id="findByIdsMap" resultMap="BaseResultMap">
-
- Select
-
- <include refid="Base_Column_List" />
-
- from jria where ID in
- <foreach item="item" index="index" collection="list"
- open="(" separator="," close=")">
- #{item}
- </foreach>
- </select>
- b.如果参数的类型是Array,则在使用时,collection属性要必须指定为 array
findByIds(Long[] ids)
- <select id="findByIdsMap" resultMap="BaseResultMap">
- select
- <include refid="Base_Column_List" />
- from jria where ID in
- <foreach item="item" index="index" collection="array"
- open="(" separator="," close=")">
- #{item}
- </foreach>
- </select>
参考:http://www.blogjava.NET/xmatthew/archive/2011/08/31/355879.html 推荐