gpt4 book ai didi

java - MyBatis 问题与 IN 条件
转载 作者:行者123 更新时间:2023-11-29 03:25:16 29 4
gpt4 key购买 nike

我必须使用 MyBatis 构建一个 IN 条件,其中必须传递一个 PARENT_VALUES 列表,以根据下面的 foreach 循环获取....

我试过但无法解决这个问题。我不确定是否

传递的值是:

Map input = new HashMap();
input.put("somedata");
List<String> inConditionList = new ArrayList<String>();
inConditionList.add("P1");
inConditionList.add("P2");
input.put(inConditionList);
sqlSessionTemplate.selectList("getNameAgeDetails", input);

所需的 SQL:

 SELECT P.NAME, P.AGE
FROM PERSON_DETAILS P
WHERE SOMECOLUMN is NULL AND DATA IN
(SELECT DATA FROM PARENT_TABLE WHERE PARENT_VALUE IN ("P1, "P2"))
ORDER BY P.NAME
FETCH FIRST 10 ROW ONLY

MyBatis 映射器 SQL:

<select id="getNameAgeDetails" parameterType="map" resultMap="someResultMap">
<![CDATA[
SELECT P.NAME, P.AGE
FROM PERSON_DETAILS P
WHERE
SOMECOLUMN is NULL
AND DATA IN
(SELECT DATA
FROM PARENT_TABLE
WHERE PARENT_VALUE IN
<FOREACH item="item" index="index" collection="list" separator="," open="(" close=")">
${item}
</FOREACH>
   )
ORDER BY P.NAME
FETCH
FIRST 10 ROW ONLY
]]>
</select>

以下是我在尝试运行单元测试用例时遇到的错误:

### The error occurred while setting parameters
### SQL: SELECT P.NAME, P.AGE
FROM PERSON_DETAILS P
WHERE
SOMECOLUMN is NULL
AND DATA IN
(SELECT DATA
FROM PARENT_TABLE
WHERE PARENT_VALUE IN <FOREACH item="item" index="index" collection="list" separator="," open="(" close=")"> ? </FOREACH>  
### Cause: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=PARENT_VALUE IN
<foreach it;TION
WHERE;<space>, DRIVER=3.63.75
; bad SQL grammar []; nested exception is com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=PARENT_VALUE IN

最佳答案

你的 Select 语句会像这样

<select id="getNameAgeDetails" parameterType="map" resultMap="someResultMap">
SELECT P.NAME, P.AGE
FROM PERSON_DETAILS P
WHERE
SOMECOLUMN is NULL
AND DATA IN
(SELECT DATA
FROM PARENT_TABLE
WHERE PARENT_VALUE IN
<FOREACH item="item" index="index" collection="list" separator="," open="(" close=")">
${item}
</FOREACH>
)
ORDER BY P.NAME
FETCH
FIRST 10 ROW ONLY
</select>

关于java - MyBatis 问题与 IN 条件 <foreach with List inside a Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21374677/

29 4 0

Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com