打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
数据库|使用DTO进行查询时错误注意

1 前言

在一次的springboot项目中,使用DTO对数据库的两张表进行查询时,启动项目,控制台就会报关于这个方法的错误,这是怎么回事呢?下面来看看

2 控制台报错

下面是当项目启动时控制台报出错误:


org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'quesstionController': Unsatisfied dependency expressed through field 'questionService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'questionServiceImpl': Unsatisfied dependency expressed through field 'questionRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'questionRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract org.springframework.data.domain.Page com.coolwen.experimentplatform.dao.QuestionRepository.findAndUname(org.springframework.data.domain.Pageable)!

如下面图2.1~图2.4,依次报错为Controller层,Impl实现类,model层,直到最后的查找方法。

图 2.1

图 2.2

图 2.3

图 2.4

3 问题原因

出现这个问题是什么原因呢?先来看下DTO,将两张表里所需的字段写出来,没什么问题。

图 3.1

接着是查找方法和查询语句,看起来也没什么问题。

图 3.2

但是经过检查测试,发现在model层里,给字段取了别名,而不是与数据库一致的名字,与查询语句写的名字不一样,这就导致出现了错误。

图 3.3

4 问题解决

最后修改一下查询语句里的别名,就可以正常启动了:

图4.1

图 4.2


END

主  编   |   张祯悦

责  编   |   黄晓锋

 where2go 团队


微信号:算法与编程之美          

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
日志框架Nlog之异步记录
bean、javabean、entity、dto、ejb、pojo都是些什么鬼
Hibernate4实战 之第七部分:最佳实践
使用MyEclipse部署SSH框架
关于Spring定时操作的例子
访问控制列表(ACL)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服