java 简单的 jdbc 包装器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/299597/
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 11:50:10  来源:igfitidea点击:

simple jdbc wrapper

javajdbcdata-accessspring-jdbc

提问by Mark Vital

To implement data access code in our application we need some framework to wrap around jdbc (ORM is not our choice, because of scalability).

为了在我们的应用程序中实现数据访问代码,我们需要一些框架来包装 jdbc(ORM 不是我们的选择,因为可扩展性)。

The coolest framework I used to work with is Spring-Jdbc. However, the policy of my company is to avoid external dependencies, especially spring, J2EE, etc. So we are thinking about writing own handy-made jdbc framework, with functionality similar Spring-jdbc: row mapping, error handling, supporting features of java5, but without transaction support.

我曾经使用过的最酷的框架是Spring-Jdbc。但是我公司的政策是避免外部依赖,特别是spring、J2EE等,所以我们正在考虑写自己的handy-made jdbc框架,功能类似Spring-jdbc:行映射,错误处理,支持java5的特性,但没有事务支持。

Does anyone have experience of writing such jdbc wrapper framework? If anyone has experience of using other jdbc wrapper frameworks, please share your experience.

有没有人有编写这样的 jdbc 包装器框架的经验?如果有人有使用其他 jdbc 包装器框架的经验,请分享您的经验。

Thanks in advance.

提前致谢。

采纳答案by jdigital

We wrote our own wrapper. This topic is worthy of a paper but I doubt I'll ever have time to write it, so here are some key points:

我们编写了自己的包装器。这个话题值得写一篇论文,但我怀疑我永远不会有时间写它,所以这里有一些关键点:

  • we embraced sql and made no attempt to hide it. the only tweak was to add support for named parameters. parameters are important because we do not encourage the use of on-the-fly sql (for security reasons) and we always use PreparedStatements.

  • for connection management, we used Apache DBCP. This was convenient at the time but it's unclear how much of this is needed with modern JDBC implementations (the docs on this stuff is lacking). DBCP also pools PreparedStatements.

  • we didn't bother with row mapping. instead (for queries) we used something similar to the Apache dbutil's ResultSetHandler, which allows you to "feed" the result set into a method which can then dump the information wherever you'd like it. This is more flexible, and in fact it wouldn't be hard to implement a ResultSetHandler for row mapping. for inserts/updates we created a generic record class (basically a hashmap with some extra bells and whistles). the biggest problem with row mapping (for us) is that you're stuck as soon as you do an "interesting" query because you may have fields that map to different classes; because you may have a hierarchical class structure but a flat result set; or because the mapping is complex and data dependent.

  • we built in error logging. for exception handling: on a query we trap and log, but for an update we trap, log, and rethrow an unchecked exceptions.

  • we provided transaction support using a wrapper approach. the caller provides the code that performs transaction, and we make sure that the transaction is properly managed, with no chance of forgetting to finish the transaction and with rollback and error handling built-in.

  • later on, we added a very simplistic relationship scheme that allows a single update/insert to apply to a record and all its dependencies. to keep things simple, we did not use this on queries, and we specifically decided not to support this with deletes because it is more reliable to use cascaded deletes.

  • 我们接受了 sql 并没有试图隐藏它。唯一的调整是添加对命名参数的支持。参数很重要,因为我们不鼓励使用动态 sql(出于安全原因)并且我们总是使用 PreparedStatements。

  • 对于连接管理,我们使用了 Apache DBCP。这在当时很方便,但目前尚不清楚现代 JDBC 实现需要多少这样的东西(缺少关于这些东西的文档)。DBCP 还汇集 PreparedStatements。

  • 我们没有打扰行映射。相反(对于查询)我们使用了类似于 Apache dbutil 的 ResultSetHandler 的东西,它允许您将结果集“馈送”到一个方法中,然后该方法可以将信息转储到您想要的任何地方。这更灵活,实际上为行映射实现 ResultSetHandler 并不难。对于插入/更新,我们创建了一个通用记录类(基本上是一个带有一些额外花里胡哨的哈希图)。行映射(对我们而言)的最大问题是,一旦执行“有趣的”查询,您就会陷入困境,因为您可能有映射到不同类的字段;因为你可能有一个分层的类结构但一个扁平的结果集;或者因为映射很复杂并且依赖于数据。

  • 我们内置了错误日志。对于异常处理:在查询中我们捕获并记录,但对于更新我们捕获、记录并重新抛出未经检查的异常。

  • 我们使用包装器方法提供事务支持。调用者提供执行事务的代码,我们确保事务得到妥善管理,不会忘记完成事务,并且内置回滚和错误处理。

  • 后来,我们添加了一个非常简单的关系方案,允许将单个更新/插入应用于记录及其所有依赖项。为简单起见,我们没有在查询中使用它,我们特别决定不支持删除,因为使用级联删除更可靠。

This wrapper has been successfully used in two projects to date. It is, of course, lightweight, but these days everyone says their code is lightweight. More importantly, it increases programmer productivity, decreases the number of bugs (and makes problems easier to track down), and it's relatively easy to trace through if need be because we don't believe in adding lots of layers just to provide beautiful architecture.

迄今为止,该包装器已成功用于两个项目。当然,它是轻量级的,但现在每个人都说他们的代码是轻量级的。更重要的是,它提高了程序员的生产力,减少了错误的数量(并使问题更容易追踪),并且在需要时追踪相对容易,因为我们不相信添加大量层只是为了提供漂亮的架构。

回答by jlpp

Spring-JDBC is fantastic. Consider that for an open source project like Spring the down side of external dependency is minimized. You can adopt the most stable version of Spring that satisfies your JDBC abstraction requirements and you know that you'll always be able to modify the source code yourselves if you ever run into an issue -- without depending on an external party. You can also examine the implementation for any security concerns that your organization might have with code written by an external party.

Spring-JDBC 非常棒。考虑到对于像 Spring 这样的开源项目,外部依赖的不利方面被最小化了。您可以采用满足您的 JDBC 抽象要求的最稳定的 Spring 版本,并且您知道如果遇到问题,您始终可以自己修改源代码——而无需依赖外部方。您还可以检查您的组织可能对外部方编写的代码存在的任何安全问题的实施。

回答by egaga

The one I prefer: Dalesbred. It's MIT licensed.

我更喜欢的是:Dalesbred。它是 MIT 许可的。

A simple example of getting all rows for a custom class (Department).

获取自定义类(部门)的所有行的简单示例。

List<Department> departments = db.findAll(Department.class,
    "select id, name from department");

when the custom class is defined as:

当自定义类定义为:

public final class Department {
    private final int id;
    private final String name;

    public Department(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

Disclaimer: it's by a company I work for.

免责声明:这是由我工作的公司提供的。

回答by Amir Fo

Jedoo

杰多

There is a wrapper class called Jedooout there that uses database connection pooling and a singleton pattern to access it as a shared variable. It has plenty of functions to run queries fast.

有一个名为Jedoo的包装类,它使用数据库连接池和单例模式将其作为共享变量访问。它有很多功能可以快速运行查询。

Usage

用法

To use it you should add it to your project and load its singleton in a java class:

要使用它,您应该将它添加到您的项目中并在一个 java 类中加载它的单例:

import static com.pwwiur.util.database.Jedoo.database;

And using it is pretty easy as well:

使用它也很容易:

if(database.count("users") < 100) {
    long id = database.insert("users", new Object[][]{
        {"name", "Amir"},
        {"username", "amirfo"}
    });

    database.setString("users", "name", "Amir Forsati", id);

    try(ResultSetHandler rsh = database.all("users")) {
         while(rsh.next()) {
             System.out.println("User ID:" + rsh.getLong("id"));
             System.out.println("User Name:" + rsh.getString("name"));
         }
    }
}

There is also some usefull function that you can find in the documentation linked above.

您还可以在上面链接的文档中找到一些有用的功能。

回答by yegor256

Try JdbcSessionfrom jcabi-jdbc. It's as simple as JDBC should be, for example:

JdbcSessionjcabi-jdbc尝试。它应该像 JDBC 一样简单,例如:

String name = new JdbcSession(source)
  .sql("SELECT name FROM foo WHERE id = ?")
  .set(123)
  .select(new SingleOutcome<String>(String.class));

That's it.

而已。

回答by Matt

This sounds like a very short sighted decision. Consider the cost of developing/maintaining such a framework, especially when you can get it, and it's source code for free. Not only do you not have to do the development yourself, you can modify it at will if need be.

这听起来像是一个非常短视的决定。考虑开发/维护这样一个框架的成本,尤其是当您可以获得它并且它是免费的源代码时。不仅不用自己开发,还可以根据需要随意修改。

That being said, what you really need to duplicate is the notion of JdbcTemplate and it's callbacks (PreparedStatementCreator, PreparedStatementCallback), as well and RowMapper/RowCallbackHandler. It shouldn't be overcomplicated to write something like this (especially considering you don't have to do transaction management).

话虽如此,您真正需要复制的是 JdbcTemplate 的概念及其回调(PreparedStatementCreator、PreparedStatementCallback),以及 RowMapper/RowCallbackHandler。编写这样的东西不应该过于复杂(特别是考虑到您不必进行事务管理)。

Howver, as i've said, why write it when you can get it for free and modify the source code as you see fit?

但是,正如我所说的,当您可以免费获得它并根据需要修改源代码时,为什么还要编写它呢?

回答by Mikhail Fursov

mJDBC: https://mjdbc.github.io/

mJDBC:https://mjdbc.github.io/

I use it for years and found it very useful.

我使用它多年,发现它非常有用。

It is inspired by JDBI library but has no dependencies, adds transactions support, provides performance counters and allows to switch to the lowest possible SQL level in Java (old plain JDBC API) easily in case if you really need it.

它受到 JDBI 库的启发,但没有依赖项,添加了事务支持,提供了性能计数器,并允许在您确实需要的情况下轻松切换到 Java(旧的普通 JDBC API)中尽可能低的 SQL 级别。

回答by Anatoly

Try mine library as alternative:

试试我的图书馆作为替代:

<dependency>
  <groupId>com.github.buckelieg</groupId>
  <artifactId>jdbc-fn</artifactId>
  <version>0.2</version>
</dependency>

More info here

更多信息在这里