Spring JDBC:如何创建表? [英] Spring JDBC: How to create the tables?
问题描述
我正在使用带有DAO模式的Spring JdbcTemplate
来访问数据库。我没有手动创建数据库表,而是在寻找一种在DAO层生成表的方法。
我明白我可以使用 JdbcTemplate
来执行语句,我只是在寻找合适的地方。
I am using Spring JdbcTemplate
with the DAO pattern to access a database. Instead of creating the database tables manually, I am looking for a way to generate the tables in the DAO layer.
I understand that I can use the JdbcTemplate
to execute statements, I am only looking for the right place to do it.
是否有最佳做法?
推荐答案
稍微偏离主题:
您是否需要在代码中执行DDL命令?事实上,我认为将db admin和db usage分开是个好主意。我们实际设置了这里的Oracle数据库安全设置,以便使用不同的数据库用户(DB_OWNER)设置表,而不是运行SELECT,INSERT,DELETE由DB_USER运行的表。
Is it absolutely necessary that you need to execute the DDL commands from within your code? In fact I do think it is a good idea to have separation between db admin and db usage. Our Oracle database security setup here is actually set up so that the tables are set up using a different database user (DB_OWNER), than the one running the SELECTs, INSERTs, DELETEs are run by DB_USER.
这可以防止意外删除表或修改模式,并允许设置DB_USER,以便只授予绝对必要的权限,这会增加一层安全性。
This prevents accidentially deleting tables or modifying the schema and also allows the DB_USER to be setup such that only the privileges that are absolutely necessary are granted, which adds a layer of security.
我认为这取决于您的服务/应用程序的性质,但考虑在代码中创建表的好处(以及DDL代码中的可能错误是否会意外破坏生产数据)。
I suppose it depends on the nature of your service/application, but think about the benefit of creating the tables inside the code (and whether a possible bug in the DDL code could accidentially destroy production data).
这篇关于Spring JDBC:如何创建表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!