Etat HTTP 500 - 结果大小不正确:预期1,实际2

时间:2018-05-05 05:45:21

标签: java spring-mvc annotations jdbctemplate

道:

public Vocabulary getItem(int categoryvocabularyid) {
    String sql = "SELECT * FROM vocabulary WHERE categoryvocabularyid = ?";
    return jdbcTemplate.queryForObject(sql,new Object []{categoryvocabularyid}, new BeanPropertyRowMapper<Vocabulary>(Vocabulary.class));
}

控制器:

    @RequestMapping("/public/vocabulary/{id}")
public String readedit(@PathVariable("id") int categoryvocabularyid, ModelMap modelMap){
    modelMap.addAttribute("vocal", vocaburalysDao.getItem(categoryvocabularyid));
    return "public.index.vocabulary.detail";
}

我的观点:

<c:forEach items="${cateVocal }" var="cateVocal">
        <div class="box-item">
            <a href="/exam/56">
                <!-- <div class="img"
                    style="background: url(/upload/2016/05/9106.jpg); background-size: cover;">
                    <p>0/54</p>
                </div> -->
                <!-- <div class="meter">
                    <span style="width: 0%"></span>
                </div> -->
                <h4>Chủ Đề: ${cateVocal.categoryVocaburalyName }</h4>
            </a><a class="learn" href="${pageContext.request.contextPath}/public/vocabulary/${cateVocal.id }">Học ngay</a>

        </div>
        </c:forEach>

我收到了一个错误,我不知道是什么原因引起的, 细节错误

Etat HTTP 500 - Request processing failed; nested exception is org.springframework.dao.IncorrectResultSizeDataAccessException: Incorrect result size: expected 1, actual 2

请告诉我。

非常感谢大家

1 个答案:

答案 0 :(得分:0)

您从您的数据库中询问单个项目Vocabulary,但实际上它找到的不止一个,因此您会收到例外。

我看到你在categoryvocabularyid找到了行,所以这里有解决方案:

  1. 如果categoryvocabularyidVocabulary中是唯一的(我猜这不太可能),那么您必须将唯一索引放入数据库中的字段,然后删除重复的项目
  2. 如果没有,则必须将方法返回更改为List<Vocabulary> 并使用queryForList代替
相关问题