【问题标题】:Is there a way to use a repository without applying repository base class?有没有办法在不应用存储库基类的情况下使用存储库?
【发布时间】:2019-09-15 17:01:16
【问题描述】:

我有一个带有 @EnableJpaRepositories(repositoryBaseClass = CommonRepository.class) 注释的 Spring Boot 应用程序。 几乎所有的存储库都需要实现一些使用repositoryBaseClass 完成的自定义逻辑。

有没有办法创建将被排除在repositoryBaseClass 机制之外的存储库?

【问题讨论】:

    标签: java spring-boot spring-data-jpa


    【解决方案1】:

    您可以编写一个不实现 Spring Data JPA Repository 接口之一的存储库。本质上,您会像没有 Spring Data JPA 那样编写存储库。

    public class YourRepo /* No Spring Data interface here! */ { 
    
        @Autowired
        private EntityManger entityManager;
    
        public add(Something entity) {
            entityManager.merge(entity);
        }
    }
    

    否则,如果您想使用不同的repositoryBaseClasses,则需要create different configurations(与@Configuration)。

    【讨论】:

    • 并且可能会调整组件扫描,因此ConfigurationrepositoryBaseClass = CommonRepository.class 不会选择特定的其他Repository
    • 嗯,是的,我没有想到不使用弹簧做某事:)
    猜你喜欢
    • 2016-01-15
    • 2019-01-26
    • 2011-08-31
    • 1970-01-01
    • 2019-06-02
    • 2010-11-11
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多