我发现这个问题之前已经被问过。但答案和文档都不适合我。我关注this文档。
我想向存储库添加 2 个自定义界面。我的代码一定做错了什么。
编译器给出错误:
Error:(18, 8) java: com.ma.home.service.BuyerPartyDetailsServiceImpl is not abstract and does not override abstract method deleteAll() in org.springframework.data.repository.CrudRepository.
自定义存储库:
@Repository
public interface CustomBuyerPartyDeatilsDAO {
@Query("SELECT b FROM BuyerPartyDetails buyer WHERE LOWER(buyer.xmlFileName) = fileName")
public List<BuyerPartyDetails> listByfileName(String fileName);
@Query("SELECT b FROM BuyerPartyDetails buyer WHERE LOWER(buyer.buyerOrganisationName) = name")
public BuyerPartyDetails getByBuyerPartyByName(String name);
}
存储库:
@Repository
public interface BuyerPartyDetailsDAO2 extends
CrudRepository<BuyerPartyDetails, Long>, CustomBuyerPartyDeatilsDAO {
public List<BuyerPartyDetails> listByfileName(String fileName);
public BuyerPartyDetails getByBuyerPartyByName(String name);
}
服务:
@Service
public class BuyerPartyDetailsServiceImpl implements
BuyerPartyDetailsDAO2,
CustomBuyerPartyDeatilsDAO {
@Autowired
private BuyerPartyDetailsDAO2 buyerPartyDetailsDAO2;
public BuyerPartyDetailsServiceImpl() {
}
// more implementation removed.
.......
.......
}
如果我提供 deleteAll()
的实现,它一一询问所有CrudRepository
方法如 save()
, saveAll()
, findById()
, findAll()
等
那么如何使用CrudRepository
已经实现的方法?
最佳答案
解决此错误
com.ma.home.service.BuyerPartyDetailsServiceImpl is not abstract and does not override abstract method deleteAll() in org.springframework.data.repository.CrudRepository
您必须重写所有抽象方法并为它们提供实现才能使其正常工作。
如果不需要全部实现,那么您必须将 DAO 接口(interface)注入(inject)到服务中,并让服务实现自己的接口(interface)。
Spring 将通过提供您应该在应用程序上下文中配置的接口(interface)的适当实现来完成剩下的工作。
关于java - 如何在Spring boot 2.0中实现CrudRepository的自定义方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49365278/