MyBatis - 使用IN运算符时返回空值

时间:2013-03-22 14:04:43

标签: ibatis mybatis

我有如下查询,

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

通过传递以下值来调用服务方法。

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

但它的回归...... []

帮助我为什么没有获得值而不是获得空列表。

P.S:我检查了日志,并且在没有任何问题的情况下创建了它的语句

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

2 个答案:

答案 0 :(得分:3)

将列名列表放在一个List参数中,然后将其传递给查询:

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

然后更新查询以获取列表并使用foreach标记迭代列表值并将它们放在in子句中:

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

答案 1 :(得分:1)

如果要放置将用于创建PreparedStatement的参数(不传递参数),则应使用${}表示法:

${columnNames}