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

查看:85
本文介绍了使用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数据的重点是您不要实现存储库.无论如何,通常都不是.相反,通常的用法是您提供一个接口,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,保存,删除等).该界面会为您提供方法名称.

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相似),这称为按方法名称创建查询".您可以在这样的界面中创建一个方法(复制

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数据将从名称中找出您需要的查询.

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天全站免登陆