编组和解编结果集:处理空字符串
当我从 ResultSet 创建域对象的实例时,是否应该将 null 字符串转换为空字符串。相反,写入数据库时是否应该将空字符串转换为空字符串?
这里的最佳实践是什么?或者它仅仅取决于应用程序?
非常感谢。
When I'm creating instances of domain objects from a ResultSet, should I convert Strings which are null to an empty String. Conversely, when writing to the database should empty Strings be converted to null Strings?
What's best practise here? Or is it just dependent on the application?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
空字符串
""
与其他字符串一样。对我来说,将""
存储为NULL
与将""
存储为''
并存储“abc”
为NULL
。所以,保持简单:将
""
存储为''
,并将null
引用存储为空。
引用MySQL参考手册:
The empty string,
""
is a string just as any other. To me it, storing""
asNULL
makes as much sense as storing""
as''
and storing"abc"
asNULL
.So, keep things simple: Store
""
as''
, and store anull
reference asNULL
.To quote the MySQL reference manual:
IMO,你应该将空值保留为空值。根据您的数据库架构,空字符串和 null 是两个不同的值。如果您将这些值混为一谈,您的代码可能会产生意外的结果——尤其是当您有多个开发人员在同一代码库上工作时。您可能必须在下游代码中考虑这两种情况,但这很可能会更清晰且更可维护/可测试。
IMO, you should keep nulls as nulls. Depending on your DB schema, empty string and null are two distinct values. If you conflate those values, your code may produce unexpected results -- especially if you have multiple developers working on the same code base. You may have to account for both cases in downstream code, but that will most likely be clearer and more maintainable/testable.