Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 11 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
JPA 似乎成为您的最佳选择。它几乎开箱即用。将直接数据库处理抽象化,并且可以独立使用(无需应用程序服务器)。
JDBC 仍然是一个有效的选择,但使用它进行的开发并不像 JPA 那样与数据库无关(JPA 可以看作是 JDBC 上的抽象层)。
这里是一个很好的例子。
最流行的 JPA 实现是 Hibernate 和 Eclipselink,其中 Eclipselink(以前的 TopLink)是参考实现。
JPA seems to be the optimal choice for you. It works almost-out-of-the-box. Abstracts the direct DB handling away and can be used standalone (without an application server).
JDBC is still a valid option but the development with it is not nearly as DB-agnostic as with JPA (which can be seen as an abstraction layer over JDBC).
Here is a nice example.
Most popular JPA implementations are Hibernate and Eclipselink with Eclipselink (former TopLink) being the reference implementation.
我会考虑 Hibernate 或其他 ORM 解决方案。
在最简单的情况下,您可以使用属性文件来交换数据库,而无需更改代码。如果您使用 Maven,您可能需要查看配置文件。
I would look at Hibernate or another ORM solution.
You can use property files to swap out the db without changes to the code in the simplest case. You may want to look at profiles if you are using Maven.
当 Hibernate 过于强大时,两个库可以提供帮助:
Two libraries that can help, when Hibernate is an overkill:
您应该开始寻找 Hibernate。这正是您正在寻找的。
You should start looking for Hibernate. This is exactly what you are searching for.
你需要的是一个设计模式看看这个。
我认为这可能会有所帮助
核心 J2EE 模式 - 数据访问对象
what you need is a design pattern have a look at this.
I think it might help
Core J2EE Patterns - Data Access Object
如果您确实希望具有数据库独立性,请不要在代码中放置任何语句。相反,请将它们存储在外部文件中。
您可以使用
mysql.ddl
来启动。然后,如果您切换到 Oracle,您将使用与mysql.ddl
相同的格式但具有不同的查询来创建oracle.ddl
。如果您的数据库访问是它自己的项目,那么这是可扩展的,因为现在与该数据库通信的其他应用程序也将能够利用您编写的现有 ddl 文件。
If you really want to have DB independence, don't put any statements in your code. Instead, store them in an external file.
You could have
mysql.ddl
to start. Then down the road, if you switch to oracle, you're makeoracle.ddl
with the same format asmysql.ddl
but with different queries.If your database access is its own project, this is extensible because now other applications that talk to this database also will be able to leverage the existing ddl file you wrote.