本文介绍了Mybatis期望selectOne()返回一个结果(或为null),但发现:190的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库检索值,但是我无法获取所有值.我正在获取TooManyResultsException.

I am trying to retrieve values from DB, but I can't able to get all the values. I am getting TooManyResultsException.

MapperInterface

这是我正在调用的映射器界面.

This is the mapper interface which I am invoking.

public interface ITranslatorDAO
{
    Map<String, Map<String, String>> translate();
}

mapper.xml

这部分是我针对数据库运行的SQL,它有190行.我想检索所有行,但它抛出异常,如下所述.

This part is the SQL which i am running against DB and it has 190 rows.i wanted to retrieve all rows but it is throwing exception as i mentioned below.

<select id="translate"  resultType="map">
    SELECT
        section,
        data,
        translation
    FROM
        web_data..wd_ofx_translate
    ORDER BY
        section,
        data,
        translation
</select>

异常回溯

Exception in thread "main" org.mybatis.spring.MyBatisSystemException:
    nested exception is org.apache.ibatis.exceptions.TooManyResultsException:
    Expected one result (or null) to be returned by selectOne(), but found: 190

推荐答案

您应该添加@MapKey来告诉mybatis您想要表中的哪一列作为地图的键,例如使用section column作为地图的键:

you should add @MapKey to tell mybatis which column in table you want as the key of the map, such as use section column as key of the map :

public interface ITranslatorDAO{
        @MapKey("section")
        Map<String, Map<String, String>> translate();
}

这篇关于Mybatis期望selectOne()返回一个结果(或为null),但发现:190的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:27