使用 Spring DATA 实现 DAO [英] using Spring DATA to implement DAO

查看:17
本文介绍了使用 Spring DATA 实现 DAO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是:我必须创建一个 AccountRepository 接口,并且我必须在我的 AccountRepositoryImpl 本身中实现所有方法,那么我该怎么做?

My requirement is this: I have to create a AccountRepository interface and i have to implement all methods in my AccountRepositoryImpl itself, so how can I do this?

示例:

1) 界面

/* this is the interface */  
public interface AccountRepository extends JpaRepository
{
    List<Account> getAllAccounts();
}

2) 实施?

public class AccountRepositoryImpl implements AccountRepository
{
    public List<Account> getAllAccounts() {
        // now what?
    }
}

推荐答案

Spring Data 的重点是你不要实现存储库.无论如何,通常不会.相反,典型的用法是您提供一个接口,而 Spring 会注入一些您从未见过的实现.

The point of Spring Data is you don't implement the Repository. Not usually, anyway. Instead the typical usage is that you provide an interface and Spring injects some implementation that you never see.

通过扩展 org.springframework.data.repository.CrudRepository 自动处理非常基本的内容(findOne、findAll、save、delete 等).该接口为您提供方法名称.

The very basic stuff (findOne, findAll, save, delete, etc.) is taken care of automatically by extending the org.springframework.data.repository.CrudRepository. That interface supplies the method names for you.

然后在某些情况下,您可以编写方法签名,以便 Spring Data 知道要获取什么(如果您了解 Grails,在概念上类似于 GORM),这称为按方法名称创建查询".您可以像这样在界面中创建一个方法(复制 spring data jpa 文档中的一个例子):

Then there are cases where you can write the method signature so that Spring Data knows what to fetch (similar in concept to GORM if you know Grails), this is called "query creation by method names". You can create a method in the interface like this (copying an example from the spring data jpa documentation):

List<Person> findByLastnameAndFirstnameAllIgnoreCase(
    String lastname, String firstname);

Spring Data 会根据名称找出您需要的查询.

and Spring Data will figure out the query that you need from the name.

最后,为了处理复杂的情况,您可以提供一个 Query 注释来指定您要使用的 JPQL.

Finally, to handle the complex cases you can provide a Query annotation that specifies the JPQL you want to use.

因此,每个实体(实际上是每个聚合根)都有不同的存储库接口.您想要在其中执行基本 CRUD 但还有一个您想要执行的特殊查询的 Account 实体的存储库可能看起来像

So you have a different repository interface for each entity (actually for each aggregate root). A repository for an Account entity where you want to do basic CRUD but also have a special query you want to execute might look like

// crud methods for Account entity, where Account's PK is 
// an artificial key of type Long
public interface AccountRepository extends CrudRepository<Account, Long> {
    @Query("select a from Account as a " 
    + "where a.flag = true " 
    + "and a.customer = :customer")
    List<Account> findAccountsWithFlagSetByCustomer(
        @Param("customer") Customer customer);
}

大功告成,不需要实现类.(大部分工作是编写查询并在持久实体上放置正确的注释.您必须将存储库连接到您的 spring 配置中.)

and you're done, no implementation class required. (Most of the work is writing the queries and putting the right annotations on the persistent entities. And you have to wire the repositories into your spring configuration.)

这篇关于使用 Spring DATA 实现 DAO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆