1

我有一个如下查询,

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

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

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

PS:我检查了日志,它的语句创建没有任何问题

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
4

2 回答 2

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>  
于 2013-03-22T16:00:57.607 回答
1

如果要放置将用于创建PreparedStatement的参数(而不是传递参数),则应使用${}符号:

${columnNames}
于 2013-03-22T14:06:53.030 回答