您对 Java bean 和数据库中字段名称命名约定之间差距的看法
在过去的许多项目中,我观察到人们倾向于将数据库列名称保留为用“_”分隔的两个或三个单词,例如:first_name。 然而,同一列被映射到 Java bean 的变量:firstName。
我不知道为什么人们不保留像firstName这样的数据库字段名称,而是倾向于在中间使用_。 即使我们使用 iBatis,定义 resultmap 映射也是一项额外的工作。
问候, 贾坦
In many past projects, I observed that people tend to keep database column name as two or three words separated with '_', for example : first_name. However the same column is mapped to Java bean's variable : firstName.
I don't know why people don't keep database field names like firstName but tend to use _ in between. Even if we use iBatis, it is an additional work to define resultmap mapping.
Regards,
Jatan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
许多数据库的列名和表名不区分大小写(过去的情况比现在更是如此),因此如果您输入
camelCasedColumnName
,您实际得到的结果是CAMELCASEDCOLUMNNAME
。 在这些数据库中,在标识符中使用下划线更具可读性,如果您希望能够以最少的麻烦来更改后端数据库,那么即使在区分大小写的情况下,这也是一个很好的做法。在代码方面,驼峰命名法是 Java 中字段标识符的约定,但在许多其他语言中,下划线是变量和方法名称的约定。 并且有一些工具可以在 CamelCasedNames、underscored_names 或其他格式之间进行转换,而开发人员在处理数据库时无需付出任何额外的努力。 (例如,Ruby 的 ActiveRecord 知道类的
NameLikeThis
映射到数据库中表的name_like_this
。)Many databases have case insensitive column and table names (and this used to be even more the case than it is these days), so if you typed
camelCasedColumnName
, what you actually ended up with wasCAMELCASEDCOLUMNNAME
. Using underscores in your identifiers was much more readable in those databases, and a good practice even in case sensitive ones if you want to be able to change your backend database with a minimum of headaches.On the code side, camelCase is the convention in Java for field identifiers, but in many other languages underscores are the convention for variable and method names. And there are tools that can convert between CamelCasedNames, underscored_names, or other formats without any additional effort on the developer's part when dealing with databases. (Ruby's ActiveRecord, for example, knows that a
NameLikeThis
for a class maps to aname_like_this
for a table in your database.)我通常甚至在数据库字段中使用firstName,而且效果很好! 然而,使用first_name 可能有一些充分的理由。 也许,数据库管理系统、团队或公司的数据库可能有名字符号,或者有人可能喜欢这个符号!
I usually use firstName even for database fields and it works fine! However, there might be some good reasons to use first_name. Probably, the dbms, team or company may have first_name notation for its databases or someone may love that notation!
请记住,数据库在多个不同的应用程序之间共享是很常见的。 这些应用程序可能是用 Java 以外的语言编写的(如果您能想象这样的事情)。 即使数据库是为新应用程序构建的,将来也可能有其他应用程序与其交互。
因此,根据数据库模式的规范(通常意味着不区分大小写和下划线)来命名数据库是有意义的,而不是试图遵守前端语言的约定。
Bear in mind that it is common for the database to be shared between several different applications. Those applications might be written in languages other than Java (if you can imagine such a thing). Even if the database is being built for a new application there may well be other applications interacting with it in the future.
Consequently it makes sense for the database to be named according to the norms for database schemas (which generally means case insensitivity and underscores) rather than trying to conform to the conventions of the front end's language.