使用 Jdbctemplate 时可以避免架构更改期间的代码更改吗?
当使用 spring 的 JdbcTemplate 时,我使用行映射器来映射返回的结果。
这样做的好处是,如果我更改 mysql 模式等,我需要更改代码的地方会更少。
关于在 mysql 中添加/删除列时如何最小化代码更改,还有其他提示吗?
When using spring's JdbcTemplate
, I am using the row mapper to map results coming back.
The benefit with this is that there are less places where I have to change my code if I change my mysql schema etc.
Are there any other tips on how to minimize changes in code when adding/removing columns in mysql?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您按名称检索列(
SELECT col1, col2, col3
),您将不受添加和重新排列列的影响。切勿使用SELECT *
。但是,如果您要删除列,则别无选择。事实上,这应该如何运作?之前您获取了例如
price
列并在业务层中使用了它。现在该列不存在 - 如何处理?但添加列是安全的,除非新列不可为空。在这种情况下,添加新记录时会遇到问题,因为
VALUES
语句不会包含新列。可选的列很好。If you are retrieving columns by name (
SELECT col1, col2, col3
) you will be immune to adding and rearranging of columns. Never useSELECT *
.However if you are removing columns, you have no choice. In fact, how was this suppose to work? Previously you fetched e.g.
price
column and used it in your business layer. Now the column does not exist - how to handle this?But adding columns is safe, unless new columns are non-nullable. In this case you will have a problem when adding new records, since
VALUES
statement won't include new columns. Optional columns are fine.一个技巧是不要执行
SELECT *
,而是选择特定列,这样即使您添加内容也不会破坏代码:)One tip is to not do
SELECT *
, select on specific columns so in case you add stuff you don't break your code :)