数据库|使用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 团队


   

微信号:算法与编程之美          

长按识别二维码关注我们!

温馨提示:点击页面右下角“写留言”发表评论,期待您的参与!期待您的转发!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

算法与编程之美

欢迎关注『算法与编程之美』

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值