Rob*_*anu 11 java spring jdbc spring-jdbc
我按照以下方式使用SimpleJdbcTemplate和MapSqlParameterSource:
MapSqlParameterSource parameterSource = new MapSqlParameterSource();
parameterSource.addValue("typeId", typeId, Types.BIGINT);
List<Long> ids = _jdbcTemplate.query(_selectIdByParameters, new EntityIdRowMapper(), parameterSource);
当typeId(是a Long)是null,则查询以下列方式查找:
SELECT id FROM XXX WHERE typeId = null
而我希望它会产生
SELECT id FROM XXX WHERE typeId IS NULL
我已经报告了这个问题并且回应是这样的
您必须根据查询参数提供适当的SQL语句.
结果我的代码充满了空检查.
是否有更优雅的方式处理发送到SimpleJdbcTemplate?的空参数?
他们有一个观点 - JdbcTemplate不是SQL解释器,它只是替换了占位符.
我建议你使用实用程序方法构造你的子句,并将其连接到查询字符串:
String createNullCheckedClause(String column, Object value) {
   String operator = (value == null ? "is" : "=");
   return String.format("(%s %s ?)", column, operator);
}
...
String query = "select * from table where " + createNullCheckedClause("col", x);
不是很漂亮 或者,也许你可以配置MySQL以允许"= NULL",但我不认为这是一个选项.
| 归档时间: | 
 | 
| 查看次数: | 6490 次 | 
| 最近记录: |