Mapper、Service类都测试了都没问题,但是加上以下controller后测试httpclient得到的结果就不正确,只有一个
来源:2-8 加载图书短评数据
stonesun1314
2022-12-27 11:47:05
相关代码:
package com.imooc.reader.controller; import com.imooc.reader.service.EvaluationService; import com.imooc.reader.utils.ResponseUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; import java.util.Map; @RestController @RequestMapping("/api/evaluation") public class EvaluationController { @Resource private EvaluationService evaluationService; @GetMapping("/list") public ResponseUtils list(Long bookId){ ResponseUtils resp = null; try { List<Map> maps = evaluationService.selectByBookId(bookId); resp = new ResponseUtils().put("list",maps); }catch (Exception e){ e.printStackTrace(); resp = new ResponseUtils(e.getClass().getSimpleName(), e.getMessage()); } return resp; } }
Controller代码也是从老师提供的代码粘贴的,还是一样的结果,查看数据查询的结果,Response插入数据也正确,但是return返回的json只有一条数据。
这数据一直正常但是httpClient返回的数据只有一条,浏览器请求也是相同的结果
什么原因呀?
1回答
好帮手慕小小
2022-12-27
同学你好,建议同学按照如下几个思路进行排查尝试:
1、先查看确认数据库中book_id为1的数据有多少条呢?如果只是一条,那么查看显示只有一条数据是正常的。
2、查询数据如果是多条的情况下,同学可以对比课程代码检查自己的sql语句是否正确。
3、如果上述俩点都可以的话,猜测可能是因同学Mybatis版本较高,出现类型转换异常,从而导致无法获取到数据。则建议同学参考2-9小节(https://class.imooc.com/lesson/2274#mid=56847)进行解决试一下。
4、有可能是缓存导致的,建议同学删除out、target目录,然后重构项目试一下。
祝学习愉快~
相似问题