简体   繁体   English

Spring 引导 mongo 存储库自定义实现不工作

[英]Spring boot mongo repository custom implementation not working

hi i followed this instruction, and i have custom method implementation like this:嗨,我遵循了这条指令,并且我有这样的自定义方法实现:

interface界面

interface ProfileMongoRepositoryCustom  {
  fun addCourse(id: String)
}

Implementation执行

class ProfileMongoRepositoryCustomImpl(
  @Autowired val mongoOperations: MongoOperations,

  ) : ProfileMongoRepositoryCustom {
  override fun addCourse(id: String) {
   // some implementation
  }
}

mongo repo蒙戈回购

@Repository
interface ProfileMongoRepository : MongoRepository<Profile, String>, ProfileMongoRepositoryCustom {
  fun findByEmail(email: String): Profile?
  fun findByPhoneNumber(phoneNumber: String): Profile?
}

when i run the program, it shows an error: No property addCourse found for type Profile!当我运行该程序时,它显示错误: No property addCourse found for type Profile!

it seems like the implementation is not read by the application because if i put the implementation in the same module and package with the interface it worked, my module structure is:应用程序似乎没有读取实现,因为如果我将实现放在同一个模块中,并将 package 与它工作的接口放在一起,我的模块结构是:

  • -- project-app (main class) -- 项目应用程序(主类)
  • -- project-repo (this is where mongo repo and custom interface belong) -- project-repo(这是 mongo repo 和自定义界面所属的地方)
  • -- project-repo-impl (this is where my custom implementaion belong) -- project-repo-impl(这是我的自定义实现所属的地方)

Basically just remove "Mongo" on your repository custom interface name基本上只需删除存储库自定义接口名称上的“Mongo”

Rename ProfileMongoRepositoryCustom to ProfileRepositoryCustomProfileMongoRepositoryCustom重命名为ProfileRepositoryCustom

also ProfileMongoRepositoryCustomImpl to ProfileRepositoryCustomImplProfileMongoRepositoryCustomImplProfileRepositoryCustomImpl

Reference from https://docs.spring.io/spring-data/jpa/docs/1.6.0.RELEASE/reference/html/repositories.html (section: Adding custom behavior to single repositories)参考自https://docs.spring.io/spring-data/jpa/docs/1.6.0.RELEASE/reference/html/repositories.html (部分:向单个存储库添加自定义行为)

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

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