Mybatis一对多查询报错,出现的Expected one result (or null) to be returned by selectOne(), but found: 3问题

在Mybatis的一对多查询中,新手很容易出现Expected one result (or null) to be returned by selectOne(), but found: 3的问题。如下图:
Mybatis一对多查询报错,出现的Expected one result (or null) to be returned by selectOne(), but found: 3问题_第1张图片
这个报错大致意思是期望的返回结果为1个,但是实际返回结果为3个。出现这个问题的主要原因是UserMapper.xml映射配置文件时出现了差错。之前我的错误写法是这样的:

Mybatis一对多查询报错,出现的Expected one result (or null) to be returned by selectOne(), but found: 3问题_第2张图片

Mybatis一对多查询报错,出现的Expected one result (or null) to be returned by selectOne(), but found: 3问题_第3张图片
上图中步骤四在查询的时候用的返回值类型resulType是接口中抽象方法的返回的数据类型,这并没有在查询的时候使用到上面的映射文件,我们应该把步骤四中的resultType="lucky.entity.User"改成步骤三中的resultMap = “userAndOrderMap”。这样才算真正完成映射文件的配置。效果如下图:

Mybatis一对多查询报错,出现的Expected one result (or null) to be returned by selectOne(), but found: 3问题_第4张图片
哈哈,问题圆满解决啦~~~

Mybatis一对多查询报错,出现的Expected one result (or null) to be returned by selectOne(), but found: 3问题_第5张图片
配上一张Mybatis一对多表查询UserMapper.xml映射配置文件的关联关系图:

Mybatis一对多查询报错,出现的Expected one result (or null) to be returned by selectOne(), but found: 3问题_第6张图片

你可能感兴趣的:(bugger,Mybatis,Java,初学)