SimpleJdbcTemplate 和 null 参数
我按以下方式使用 SimpleJdbcTemplate 和 MapSqlParameterSource:
MapSqlParameterSource parameterSource = new MapSqlParameterSource();
parameterSource.addValue("typeId", typeId, Types.BIGINT);
List<Long> ids = _jdbcTemplate.query(_selectIdByParameters, new EntityIdRowMapper(), parameterSource);
当 typeId
(这是 Long
)为 null
时,查询将在以下方式:
SELECT id FROM XXX WHERE typeId = null
虽然我希望它生成,但
SELECT id FROM XXX WHERE typeId IS NULL
我已经报告了此问题和响应那是
您必须根据查询参数提供适当的 SQL 语句。
因此,我的代码充满了空检查。
是否有更优雅的方法来处理发送到 SimpleJdbcTemplate 的空参数?
I'm using SimpleJdbcTemplate and MapSqlParameterSource in the folowing way:
MapSqlParameterSource parameterSource = new MapSqlParameterSource();
parameterSource.addValue("typeId", typeId, Types.BIGINT);
List<Long> ids = _jdbcTemplate.query(_selectIdByParameters, new EntityIdRowMapper(), parameterSource);
When typeId
( which is a Long
) is null
, then the query looks in the following way:
SELECT id FROM XXX WHERE typeId = null
whereas I would expect it to generate
SELECT id FROM XXX WHERE typeId IS NULL
I've reported this issue and the response was that
You will have to provide the appropriate SQL statement based on your query parameters.
and as a consequence my code is littered with null checks.
Is there a more elegant way of handling null parameters sent to the SimpleJdbcTemplate
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
他们有一个观点 - JdbcTemplate 不是 SQL 解释器,它只是替换您的占位符。
我建议您使用实用程序方法构建子句,并将其连接到查询字符串:
不太漂亮。 或者,也许您可以将 MySQL 配置为允许“= NULL”,但我认为这不是一个选项。
They have a point - JdbcTemplate isn't a SQL interpreter, it just replaces your placeholders.
I suggest you construct your clause with a utility method, and concat it to the query string:
Not very pretty. Alternatively, perhaps you can configure MySQL to allow "= NULL", but I don't think that's an option.