没有 Spring 的 Spring JDBC 模板

发布于 2024-12-20 03:29:29 字数 320 浏览 3 评论 0原文

是否有像 Spring JDBC Template 这样的 Java 库,具有相同质量的代码和文档以及类似的数据访问异常层次结构,但不依赖于其他 Spring 模块(根据 http://mvnrepository.com/artifact/org.springframework/spring-jdbc/3.0.6。发布)?

Is there Java library like Spring JDBC Template, with same quality of code and documentation and similar data access exceptions hierarchy, but without dependancies on other Spring modules (core/beans/context modules according to http://mvnrepository.com/artifact/org.springframework/spring-jdbc/3.0.6.RELEASE)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

近箐 2024-12-27 03:29:29

Spring-jdbc 直接依赖于以下库:spring-corespring-beansspring-tx。其余的依赖项是可选的,因此您实际上不需要它们。

如果这些依赖项对您来说已经很多了,那么您可以看看 Apache DbUtils

Spring-jdbc has direct dependency to the following libraries: spring-core, spring-beans and spring-tx. The rest of the dependencies are optional, so you don't need them actually.

If these dependencies are already a lot for you, then you might have a look at Apache DbUtils.

倾其所爱 2024-12-27 03:29:29

对已接受答案的小修正。据我所知,至少还需要一个罐子。它恰好在访问数据访问异常层次结构时发挥作用(底层数据库驱动程序抛出异常并将其包装到 Spring 异常中)。该 jar 是 spring-asm(字节码操作)。
当我的 SQL 查询出现问题并且堆栈跟踪显示类似 MissingClassException 的内容时,我偶然发现了它。
(我宁愿只在答案中添加评论,但看起来我还不符合资格)。

A minor correction to the accepted answer. As far as I can tell there is at least one more jar required. It comes into play exactly when data access exception hierarchy is accessed (an exception is thrown by underlying database driver and it's wrapped into Spring exception). The jar is spring-asm (byte-code manipulation).
I discovered it accidentally when I had a problem in my SQL query and stack trace showed something like MissingClassException.
(I would to prefer just to add a comment to the answer, but it looks I'm not eligible yet).

甜尕妞 2024-12-27 03:29:29

还有另一种非常接近JdbcTemplate的替代方法,您可以使用库sql2o仅有 slf4jguava 依赖项。请参阅下面来自他们网站的简单示例。此外,作为奖励,您仍然可以获得更好的性能,正如您在此基准上看到的那样(披露:我是不是 sql2o 项目的成员,我只是在项目中使用它)。

public class Task{
    private int id;
    private String category;
    private Date dueDate;
    // getters and setters here
}
Sql2o sql2o = new Sql2o(DB_URL, USER, PASS);

String sql =
    "SELECT id, category, duedate " +
    "FROM tasks " +
    "WHERE category = :category";

try(Connection con = sql2o.open()) {
    List<Task> tasks = con.createQuery(sql)
        .addParameter("category", "foo")
        .executeAndFetch(Task.class);
}

There is another alternative way very close to JdbcTemplate, you can use the library sql2o which has just slf4j and guava dependencies. See below a simple example from their web site. Also, as a bonus you still get better performance as you can see on this benchmark (disclosure: I'm NOT a member of sql2o project, I'm just using it in a project).

public class Task{
    private int id;
    private String category;
    private Date dueDate;
    // getters and setters here
}
Sql2o sql2o = new Sql2o(DB_URL, USER, PASS);

String sql =
    "SELECT id, category, duedate " +
    "FROM tasks " +
    "WHERE category = :category";

try(Connection con = sql2o.open()) {
    List<Task> tasks = con.createQuery(sql)
        .addParameter("category", "foo")
        .executeAndFetch(Task.class);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文