我想实现 “多个id 传入sql 语句“的操作。
1、我试着将数组 stringUtils.join(数组,","); //数组转string 并以逗号分隔
没成功。因为传到sql 里是“123,234,435” ,而我们需要的是“123”,“234”,“345” 这样格式。
2、oracle split string to table
select * from table1 where id in (select * from table(str2table(#{ids},‘,’)));
而 str2table可以这样写:
CREATE OR REPLACE TYPE TY_OBJECT AS OBJECT(COL_NAME VARCHAR2(200));
/
CREATE OR REPLACE TYPE TY_TABLE AS TABLE OF TY_OBJECT;
/
CREATE OR REPLACE FUNCTION STR2TABLE(V_STR IN VARCHAR2,
V_DELIMITER IN VARCHAR2)
--此函数的目的是将以特定字符分隔的字符串转换为游标形式,以例遍历此游标
RETURN TY_TABLE AS
V_TY_TABLE TY_TABLE;
BEGIN
SELECT TY_OBJECT(REGEXP_SUBSTR(V_STR,
'[^' || V_DELIMITER || ']+',
1,
LEVEL,
'i'))
BULK COLLECT
INTO V_TY_TABLE
FROM DUAL
CONNECT BY LEVEL <=
LENGTH(V_STR) -
LENGTH(REGEXP_REPLACE(V_STR, '' || V_DELIMITER || '', '')) + 1;
RETURN V_TY_TABLE;
END;
摘自:http://blog.csdn.net/e_wsq/article/details/52381846
这样就能获得“123”,“234”,“345”的格式
3、还可以用mybatis 自带的 foreach 方法
1. 当查询的参数只有一个时
findByIds(List<Long> ids)
1.a 如果参数的类型是List, 则在使用时,collection属性要必须指定为 list
<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>
1.b 如果参数的类型是Array,则在使用时,collection属性要必须指定为 array
<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>
2. 当查询的参数有多个时,例如 findByIds(String name, Long[] ids)
这种情况需要特别注意,在传参数时,一定要改用Map方式, 这样在collection属性可以指定名称
下面是一个示例
Map<String, Object> params = new HashMap<String, Object>(2);
params.put("name", name);
params.put("ids", ids);
mapper.findByIdsMap(params);
<select id="findByIdsMap" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from jria where ID in
<foreach item="item" index="index" collection="ids"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
摘自:http://blog.csdn.net/jarniyy/article/details/51169242
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)