简体   繁体   English

什么是CDI中的Spring-JDBC?

[英]What is the equivalent of Spring-JDBC in CDI?

Considering replacing Spring with CDI in new project, I've encountered a problem challenge: 考虑在新项目中用CDI替换Spring,我遇到了一个 问题 挑战:

We do a plenty of things with SQL queries (for which JPA isn't reasonable): invoking stored procedures, reporting, specialized queries with complicated syntax. 我们使用SQL查询做了很多事情(JPA不合理):调用存储过程,报告,具有复杂语法的专用查询。

There's a plenty of trivial examples for CDI database access in Internet, and for trivial examples JPA is sufficient. 在Internet上有很多关于CDI数据库访问的简单例子,对于简单的例子,JPA就足够了。 But how do you work with SQL in JPA? 但是你如何使用JPA中的SQL?

I'm looking for something like Spring-JDBC, with simplified interface for running SQL queries through JDBC and container managing transactions (through annotations), but I havent't found anything yet. 我正在寻找类似Spring-JDBC的东西,通过JDBC和容器管理事务(通过注释)运行SQL查询的简化接口,但我还没有找到任何东西。

How do one work with SQL queries in CDI? 如何在CDI中使用SQL查询? Is it possible with CDI core or extensions? 是否可以使用CDI核心或扩展?

There isn't anything in the Spec about doing this. 规范中没有任何关于这样做的事情。 You'd have to create your own library, create the transaction layer (or use JTA 1.2 from Java EE 7, or Apache Deltaspike) and build it all up that way. 您必须创建自己的库,创建事务层(或使用Java EE 7中的JTA 1.2或Apache Deltaspike)并以此方式构建它。 I did mention DeltaSpike , which in the (hopefully) soon to be released version 0.5 will have some of these query and other DB specific features. 我确实提到了DeltaSpike ,它在(希望)即将发布的版本0.5中将具有一些这些查询和其他DB特定功能。

CDI stands for Contexts/Dependency Injection (if I'm not mistaken). CDI代表上下文/依赖注入(如果我没有记错的话)。 By itself it has nothing to do with SQL. 它本身与SQL无关。 Using CDI with JPA allows you to work with the database, but even in this case JPA does all the work. 使用CDI和JPA可以使用数据库,但即使在这种情况下,JPA也可以完成所有工作。 CDI has very little to do with it. CDI与它几乎没有关系。

If you want to work with native queries and JPA's nativeQuery isn't enough, you'll have to either add the Spring-JDBC dependency to your project, or since you're moving away from Spring, another smaller library that would give you things similar to JdbcTemplate and such. 如果你想使用本机查询和JPA的nativeQuery是不够的,你将不得不将Spring-JDBC依赖项添加到你的项目中,或者因为你正在离开Spring,另一个较小的库会给你带来东西类似于JdbcTemplate等。

So to sum it up, CDI has no equivalent of Spring-JDBC, just as Spring doesn't have an equivalent of Swing GUI. 总而言之,CDI没有Spring-JDBC的等价物,就像Spring没有等效的Swing GUI一样。

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

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