如何在Kotlin中将IN子句与JdbcTemplate一起使用
val days = arrayOf("TUESDAY", "WEDNESDAY")
jdbcTemplate.query("select * from days where days not in (?), mapper, days)
对于以下内容,我会得到:
ERROR: operator does not exist: text = character varying[]
答案 0 :(得分:0)
我有同样的问题。我正在寻找很多东西,但没有找到任何“干净”的解决方案。 这是我的解决方案:
@Repository
class AnyRepository (@Autowired var template: JdbcTemplate){
fun anyName(someList: Lis<int>): List<Any> = template.query("SELECT * FROM anyTable WHERE anyTable.id IN (${someList.toString().substring(1, someList.toString().length - 1)})"){ rs, _ -> ... }
}
如果找到更好的解决方案,我会发布
答案 1 :(得分:0)
处理JDBC模板(在Java或Spring中)的“ IN”情况的最佳方法是使用Spring中的NamedParameterJdbcTemplate
。如果您正在使用环境属性来连接数据源,那么其中之一应该已经可以作为自动插入的Bean使用。如果没有,则可以通过传递您拥有的JdbcTemplate
来创建一个。 / p>
val days = arrayOf("TUESDAY", "WEDNESDAY")
val namedParamJdbcTemplate = NamedParameterJdbcTemplate(jdbcTemplate) // or autowired in
// make sure this is not an empty collection! In your example it obviously is not empty.
// but if it is being passed in and could be empty, make sure to not query with it,
// otherwise the JdbcTemplate will make a where clause of days NOT IN (), which will fail on execution
val paramMap: Map<String, Any?> = mapOf("days" to days)
namedParamJdbcTemplate.query("select * from days where days not in (:days), paramMap, mapper)