在项目中where条件中用到in,我理所当然传了字符串,但是并不能达到理想的效果,百度了一下,找到如下解决方法,作为笔记记录一下。。。
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>