java Spring JDBC:如何创建表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2101439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:27:58  来源:igfitidea点击:

Spring JDBC: How to create the tables?

javaspringjdbcdao

提问by Juri Glass

I am using Spring JdbcTemplatewith 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 JdbcTemplateto execute statements, I am only looking for the right place to do it.

我正在使用JdbcTemplate带有 DAO 模式的Spring来访问数据库。我不是手动创建数据库表,而是寻找一种在 DAO 层中生成表的方法。我知道我可以使用JdbcTemplate来执行语句,我只是在寻找合适的地方来执行此操作。

Is there a best practice for that?

有没有最好的做法?

采纳答案by beny23

Slightly offtopic:

有点题外话:

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.

您是否绝对需要从代码中执行 DDL 命令?事实上,我确实认为将 db admin 和 db 使用分开是个好主意。我们这里的 Oracle 数据库安全设置实际上是这样设置的,即使用不同的数据库用户 (DB_OWNER) 设置表,而不是运行 SELECT、INSERT、DELETE 的用户由 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.

这可以防止意外删除表或修改架构,还允许设置 DB_USER,以便只授予绝对必要的权限,这增加了一层安全性。

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).

我想这取决于您的服务/应用程序的性质,但想想在代码中创建表的好处(以及 DDL 代码中可能的错误是否会意外破坏生产数据)。

回答by matt b

You can use the execute(String) method:

您可以使用execute(String) 方法

public void execute(String sql) throws DataAccessException

Issue a single SQL execute, typically a DDL statement.
Specified by: execute in interface JdbcOperations

Parameters: sql - static SQL to execute

Throws: DataAccessException - if there is any problem

发出单个 SQL 执行,通常是 DDL 语句。
指定者:在接口 JdbcOperations 中执行

参数: sql - 要执行的静态 SQL

抛出:DataAccessException - 如果有任何问题

However as beny23 mentions I would be suspicious of an actual need to do this programatically in a live application.

然而,正如 beny23 提到的,我怀疑在实时应用程序中以编程方式执行此操作的实际需要。

回答by Esko

Use .update()methods available in the (Simple)JdbcOperations, the number they return is the number of affected rows. They're supposed to be specifically used for both INSERTand UPDATEstatements.

使用 中.update()可用的方法(Simple)JdbcOperations,它们返回的数字是受影响的行数。它们应该专门用于INSERTUPDATE语句。