简体   繁体   English

如何在 crud 存储库的实现中编码查询 function

[英]How to code the query function in the implementation of a crud repository

So typically, I have just had a single interface that implements a CrudRepository and put my @Query in there.所以通常情况下,我只有一个实现 CrudRepository 的接口并将我的 @Query 放在那里。 However, for this I have a DAO class that is the repo and a DAOImpl that implements that repo.但是,为此我有一个 DAO class 是存储库和一个实现该存储库的 DAOImpl。 So, I have the implement the method I just want to use a query for and return that query as a List of strings.因此,我实现了我只想使用查询并将该查询作为字符串列表返回的方法。 My question is how do I code the part in the implementation to just pass that List of Strings through?我的问题是如何对实现中的部分进行编码以仅通过该字符串列表?

public interface DealerDAO {
    /**
     * @return List of Marketing Orgs
     */
    public Map<String, MarketOrg> getMarketStructure();

    @Query(
            value="SELECT DISTINCT ...",
            nativeQuery = true)
    List<String> findDealerCode(String marketOrg, String region, String district);
}
@Service
public class DealerDAOImpl implements DealerDAO {
    @PersistenceContext
    private EntityManager em;


    public Map<String, MarketOrg> getMarketStructure() {
        HashMap<String, MarketOrg> mktOrgs = new HashMap<String, MarketOrg>();
        Query q = em.createNativeQuery("SELECT DISTINCT ....");
        List<Object[]> marketEntities = q.getResultList();
        /* Extra code */

        return mktOrgs;
    }


    @Override
    public List<String> findDealerCode(String marketOrg, String region, String district) {
        //This should return something, not null.
        return null;
    }

So,I do not understand your need entirely but My guess is that you do not want to provide the implementation for the findDealerCode() method but still wants to add an implementation for getMarketStructure() to return a Map.所以,我不完全理解您的需求,但我的猜测是您不想提供findDealerCode()方法的实现,但仍想为getMarketStructure()添加一个实现以返回 Map。 If so, instead of providing an implementation for the DealerDAO interface, if you have java8 compatibility why don't you use a default method implementation in the interface itself.如果是这样,而不是为DealerDAO接口提供实现,如果您具有 java8 兼容性,为什么不在接口本身中使用默认方法实现。 That way you could still extend the CRUD repository, use the default spring implementation and still get what you want to achieve.Like:这样您仍然可以扩展 CRUD 存储库,使用默认的 spring 实现,并且仍然可以获得您想要实现的目标。例如:

public interface DealerDAO extends CrudRepository<Obj,Obj> {
    /**
     * @return List of Marketing Orgs
     */
    default public Map<String, MarketOrg> getMarketStructure() {
        HashMap<String, MarketOrg> mktOrgs = new HashMap<String, MarketOrg>();

        /* Extra code */

        return mktOrgs;
    }

    @Query(
            value="SELECT DISTINCT ...",
            nativeQuery = true)
    List<String> findDealerCode(String marketOrg, String region, String district);
}

but this means that the method cannot access the state of the instance.但这意味着该方法无法访问实例的 state。 Default methods should only be used to delegate calls to other repository methods .默认方法只能用于将调用委托给其他存储库方法 Default methods - by definition - cannot access any state of an instance (as an interface has none).默认方法 - 根据定义 - 无法访问实例的任何 state(因为接口没有)。 They only can delegate to other interface methods or call static ones of other classes.它们只能委托给其他接口方法或调用其他类的 static。

or in spring-boot way you could have separate interfaces for these methods, if you need to have the persistance context in the getMarketStructure() method.或者在 spring-boot 方式中,如果您需要在getMarketStructure()方法中具有持久性上下文,则可以为这些方法提供单独的接口。 Like:喜欢:

@Repository
public interface DealerDAO extends CrudRepository<Obj,Obj>, DealerRepositoryCustom {
    @Query(
            value="SELECT DISTINCT ...",
            nativeQuery = true)
    List<String> findDealerCode(String marketOrg, String region, String district);
}

public interface DealerRepositoryCustom {
   public Map<String, MarketOrg> getMarketStructure();
}

then you could have the implementation for the DealerRepositoryCustom interface in a class like:那么您可以在 class 中实现DealerRepositoryCustom接口,例如:

@Repository
@Transactional(readOnly = true) // if you don't plan on modifying
public class DealerCustomRepositoryImpl implements DealerRepositoryCustom {
    @PersistenceContext
    private EntityManager em;

    @Override
    public List<String> findDealerCode(String marketOrg, String region, String district) {
        //This should return something, not null.
        return null;
    }
}

For reference read .供参考阅读

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

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