Search code examples
ibatismybatis

MyBatis - returning empty value when IN operator is used


I have a query like below,

<select id="getTableName" resultType="java.lang.String" parameterType="java.lang.String">
    Select Distinct TableName From TABLE_COLUMN_MAPPING Where COLUMNNAME IN (#{columnNames})
</select> 

am calling the service method by passing the values as below.

sampleService.getTableName("'BANKRM','APPLICANTID','APPLICANTTYPE','APPLICANTTYPE','TOTAL','SCQUES9'");

but its returning... [ ]

Help me why am not getting values instead am getting empty List.

P.S: I checked the log and its statements are created with out any issues

2013-03-22 19:16:27,521 DEBUG [main] org.apache.ibatis.datasource.pooled.PooledDataSource (debug:27) - Created connection 23845098.
2013-03-22 19:16:27,590 DEBUG [main] java.sql.Connection (debug:27) - ooo Connection Opened
2013-03-22 19:16:27,670 DEBUG [main] java.sql.PreparedStatement (debug:27) - ==>  Executing: Select Distinct TableName From Config_FieldDetails Where COLUMNNAME IN (?) 
2013-03-22 19:16:27,670 DEBUG [main] java.sql.PreparedStatement (debug:27) - ==> Parameters: 'BANKRM','APPLICANTID','APPLICANTTYPE','APPLICANTTYPE','TOTAL','SCQUES9'(String)
2013-03-22 19:16:27,954 DEBUG [main] java.sql.Connection (debug:27) - xxx Connection Closed
2013-03-22 19:16:27,955 DEBUG [main] org.apache.ibatis.datasource.pooled.PooledDataSource (debug:27) - Returned connection 23845098 to pool.
2013-03-22 19:16:27,955 INFO [main] com.hcl.cob.mybatis.bpm.service.impl.COBBPMCommonServiceImpl (getTableNameFromConfigFieldDetails:41) - getTableNameFromConfigFieldDetails(String columnName) -- End

Solution

  • Put the list of column names in one List parameter and then pass that to the query:

    List<String> columnNames = Arrays.asList(["BANKRM", "APPLICANTID", "APPLICANTTYPE", "APPLICANTTYPE","TOTAL","SCQUES9"]); 
    sampleService.getTableName(columnNames);
    

    Then update the query to take a list and use the foreach tag to iterate over the list values and put them in the in clause:

    <select id="getTableName" resultType="java.lang.String" parameterType="java.util.List">
        Select Distinct TableName From TABLE_COLUMN_MAPPING Where COLUMNNAME IN 
            <foreach item="columnName" index="index" collection="columnNames" open="(" separator="," close=")">
                #{columnName}
            </foreach>
    </select>