使用Querydsl和Spring Data时的最佳实践

pze*_*zko 10 spring querydsl spring-data spring-data-jpa

使用Spring Data nad Querydsl,我们可以声明存储库接口并跳过实现类.一些具有特定名称或使用@Query注释的方法,这就是全部.

但是有时候我想使用JPAQuery并自己定义方法的主体,比方说

@Repository
public class MyRepositoryImpl implements MyRepository {

    @PersistenceContext
    private EntityManager em;

    @Override
    public List<Tuple> someMethod(String arg) {
        JPAQuery query = new JPAQuery(em);
        ...
    }
Run Code Online (Sandbox Code Playgroud)

但这样我就必须实现其他MyRepository接口方法,这会破坏Spring Data的所有优点!

我可以看到两个选项:

  • 为每个存储库声明另一个接口,然后通常实现它(接口数量增加一倍)
  • 将EntityManager注入@Service类并在那里实现我的自定义方法

我更喜欢选项#2,但据我所知,在@Service类中我们应该只调用存储库方法,所以它也不是一个完美的解决方案.

那么程序员如何处理呢?

vto*_*tor 14

您不应该实现实际的Spring Data存储库,而是必须声明另一个自定义接口,您可以在其中放置自定义方法.

假设你有一个MyRepository,定义为

@Repository
public interface MyRepository extends JpaRepository<Tuple, Long> {}
Run Code Online (Sandbox Code Playgroud)

现在您要添加自定义findTuplesByMyArg(),为了您的目的,您需要创建自定义存储库接口

public interface MyRepositoryCustom {
   List<Tuple> findTuplesByMyArg(String myArg);
}
Run Code Online (Sandbox Code Playgroud)

之后是自定义界面的实现

public class MyRepositoryImpl implements MyRepositoryCustom {
    @PersistenceContext
    private EntityManager em;

    @Override
    public List<Tuple> findTuplesByMyArg(String myArg) {
        JPAQuery query = new JPAQuery(em);
        ...
    }    
}
Run Code Online (Sandbox Code Playgroud)

我们需要更改MyRepository声明,因此它扩展了自定义存储库,以便

@Repository
public interface MyRepository extends JpaRepository<Tuple, Long>, MyRepositoryCustom {}
Run Code Online (Sandbox Code Playgroud)

您可以findTuplesByMyArg()通过注射轻松访问您MyRepository,例如

@Service
public class MyService {
   @Autowired
   private MyRepository myRepository;

   public List<Tuple> retrieveTuples(String myArg) { 
      return myRepository.findTuplesByMyArg(myArg);
   }
}
Run Code Online (Sandbox Code Playgroud)

注意名称在这里很重要(你需要Impl在repo实现中默认配置postfix).

您可以在此处找到所有需要的信息


Vai*_*sal 9

我建议对上面的答案进行小幅修正,它尝试使用 JPAQueryFactory。最好使用提供的工厂类。

public class MyRepositoryImpl implements MyRepositoryCustom {
@Autowired
private JPAQueryFactory factory;

@Override
public List<Tuple> findTuplesByMyArg(String myArg) {
    JPAQuery query = factory.query();
    ...
}}

@Configuration
public class Config {

@Autowired
private EntityManager em;

@Bean
public JPAQueryFactory jpaQueryFactory() {
      return new JPAQueryFactory(em);
}

}
Run Code Online (Sandbox Code Playgroud)