MyBatis problem with IN <foreach clause with list inside map

I need to build an IN condition using MyBatis where I need to pass a list PARENT_VALUES

to be retrieved based on the foreach loop below ....

I tried but couldn't solve it. I'm not sure if

Missing values:

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);

      

Required 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 Mapper 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>

      

Below is the error I get when I try to run my test cells:

### 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 

      

+1


source to share


2 answers


Your Select statement would like something like this



<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>

      

+3


source


I agree with Karthik Prasad and if you remove CDATA and want to

a_column>=6 AND b_column<10

      



you have to escape the XML like below:

a_column &gt;= 6 AND b_column &lt; 10

      

+1


source







All Articles