简体   繁体   English

用Java访问数据库的不同方法

[英]Different ways to access database in Java

There are a lot of technologies to access data using Java. 有很多使用Java访问数据的技术。 I was reading about some of them, including: 我正在阅读其中的一些内容,包括:

  • JPQL JPQL
  • HQL 高品质
  • Criteria API 标准API
  • Querydsl 查询查询
  • jOOQ OO
  • JaQu 杰曲
  • JDBC JDBC
  • ActiveJDBC 主动JDBC

Now I am really confused because of the overhead. 现在,由于开销,我真的很困惑。 What are the main differences and similarities between these technologies? 这些技术之间的主要区别和相似之处是什么? Which are most commonly used? 哪个最常用? Short comparison is more than welcome. 简短的比较绝对值得欢迎。

The fundamental foundation for database access in Java is JDBC. Java中数据库访问的基本基础是JDBC。 It is an interface (no implementation) that permits database vendors to expose their databases in a standard way, so Java programmers don't have to radically alter their code to support different relational databases. 它是一个接口(没有实现),允许数据库供应商以标准方式公开其数据库,因此Java程序员不必从根本上更改其代码即可支持不同的关系数据库。

That said, it accepts SQL, and SQL is standard, with so many variants that the standards do not permit cross database portability. 就是说,它接受SQL,并且SQL是标准的,具有太多的变体,因此标准不允许跨数据库的可移植性。

The rest of the platforms tend to build upon JDBC in an attempt to ease the cost of integrating with other databases. 其余平台倾向于在JDBC上构建,以减轻与其他数据库集成的成本。 The numbers of ways one can interact with a database is varied, basically you get improvement on issuing SQL commands or the additional library takes over the writing of a compatible SQL command for you. 人们与数据库交互的方式多种多样,基本上,您在发出SQL命令方面得到了改进,或者由其他库代替为您编写兼容的SQL命令。

The two major categories are "database like" and "object storage" where object storage focuses on you storing a Java Object, and the libraries take care of most of the details of SQL generation. 两个主要类别是“类似数据库”和“对象存储”,其中对象存储主要用于存储Java对象,而库负责处理SQL生成的大多数细节。

Technology                 / query language / type         / notes

Java Persistance API       / JPQL           / object store / not an implementation, but an interface allowing multiple implementations, query language is not table specific, but object specific
Hibernate                  / HQL            / object store / a popular Java solution, but tied to Hibernate only.
Java Persistance API       / Criteria API   / object store / Criteria API is the code (programming) equivalent to the JPQL strings.
Java Persistance API & JDO / QueryDSL API   / object store / Java API to build queries similar to Criteria API, but different
JDBC                       / jOOQ           / direct JDBC  / Java API that replaces SQL strings construction with method calls that are table centric
H2 database                / JaQu           / object store / Linked directly to one database, non-portable.  Follows Microsoft LINQ syntax.
JDBC                       / ActiveJDBC     / object store / Looks like a branded version of almost-JPA for webapps.  

The two fundamental APIs in this space are JDBC and JDO, where the subset of JDO you wish to use if you are only going to support relational databases is JPA. 这个领域的两个基本API是JDBC和JDO,如果您仅打算支持关系数据库,则希望使用的JDO子集是JPA。 Neither JDBC nor JDO provide a database connection directly, they are pure-play APIs. JDBC和JDO都不直接提供数据库连接,它们是纯API。 That said, a lot of database vendors push their APIs which don't leverage these technologies, I would advise not using any technology that isn't based on JDBC and JPA. 就是说,许多数据库供应商都推销其不利用这些技术的API,我建议不要使用任何不基于JDBC和JPA的技术。

Again to leverage multiple implementations, I would also advise that you not use a query language that isn't based on JPQL (if you build queries in Strings) or the CriteriaAPI (if you build queries in code), both which are conceptual components of JPA. 再次利用多个实现,我还建议您不要使用不基于JPQL(如果您使用字符串构建查询)或CriteriaAPI(如果您使用代码构建查询)的查询语言,这两种语言都是JPA。 If you are using JDBC directly, use PreparedStatements for all issued SQL, and keep in mind that while you may be able to leverage your existing SQL codebase with a pure JDBC solution, you will likely get a better quality implementation (and possibly faster delivery) using JPA, because there are lots of corner cases in mapping Relational Databases to Java that very few existing database codebases handle. 如果直接使用JDBC,请对所有已发布的SQL使用PreparedStatements,并请注意,虽然您可以通过纯JDBC解决方案利用现有的SQL代码库,但实现的质量可能会更高(交付速度可能更快)之所以使用JPA,是因为在关系数据库到Java的映射中存在很多极端情况,而现有数据库代码库所处理的情况很少。

Do not write JDBC code. 不要编写JDBC代码。 You will never do a better job of it than MyBatis and/or Hibernate. 您将永远不会比MyBatis和/或Hibernate做得更好。

Instead, learn and use either MyBatis or Hibernate. 相反,学习并使用MyBatis或Hibernate。

MyBatis is simpler (and most likely sufficient for anything you will need) and Hibernate has a million features (none of which you probably need). MyBatis更简单(并且很可能足以满足您的需求),而Hibernate具有一百万个功能(您可能根本不需要)。

  1. Using JDBC API and registering your DB class and making the connection. 使用JDBC API并注册您的数据库类并建立连接。
  2. Using Datasource , where you define DB details on your server like WebSphere or Weblogic server And you lookup the defined datasource. 使用Datasource ,在其中定义服务器(例如WebSphere或Weblogic服务器)上的数据库详细信息,然后查找定义的数据源。
  3. Using some ORM framework like Hibernate. 使用诸如Hibernate之类的ORM框架

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM