java Mybatis 期望 selectOne() 返回一个结果(或 null),但发现:190

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34666152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:10:55  来源:igfitidea点击:

Mybatis Expected one result (or null) to be returned by selectOne(), but found: 190

javamybatis

提问by silentlearner

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

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

MapperInterface

映射器接口

This is the mapper interface which I am invoking.

这是我正在调用的映射器接口。

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

mapper.xml

映射器.xml

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.

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

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

Exception traceback

异常回溯

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

回答by Persia

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 :

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

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

回答by JimmyJames

The selectOne() method is used when you want to run a query that selects onerow. If you want to select more than one row, use a different method.

当您想要运行选择一行的查询时,将使用 selectOne() 方法。如果要选择多行,请使用不同的方法。