Spring Boot Mongo存储库自定义实现不起作用
嗨,我关注了 这个< /a> 指令,我有这样的自定义方法实现:
接口
interface ProfileMongoRepositoryCustom {
fun addCourse(id: String)
}
实现
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?
}
当我运行程序时,它显示错误: 没有找到 Profile 类型的属性 addCourse!
应用程序似乎无法读取实现,因为如果我将实现放在同一个模块中,并与其工作的接口一起打包,我的模块结构是:
- - -project-app(主类)
- --project-repo(这是 mongo repo 和自定义接口所属的位置)
- --project-repo-impl(这是我的自定义实现所属的位置)
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!
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:
- -- project-app (main class)
- -- project-repo (this is where mongo repo and custom interface belong)
- -- project-repo-impl (this is where my custom implementaion belong)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上只需在您的存储库自定义接口名称上删除“ mongo”
名称
ProfileMongorePositoryCustom
fifilerePositoryCustom
也
ProfileMongorePositoryCustomimpl
toprofilerepositorycustomimpl
来自 https://docs.spring.io/spring-data/jpa/jpa/docs/1.6.0.release/reelease/reelease/reelease/Release/html/repositories.html/repositories.html e节:向单个存储库添加自定义行为)
Basically just remove "Mongo" on your repository custom interface name
Rename
ProfileMongoRepositoryCustom
toProfileRepositoryCustom
also
ProfileMongoRepositoryCustomImpl
toProfileRepositoryCustomImpl
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)