最近群里有人贴出一个问题:Mapped Statements collection does not contain value for student.insert

如下图所示:

Mapped-Statements-collection.png

关于这个错误的分析是这样的:

(1)其实,最令人困惑的是"Mapped Statements collection",它是个map的名字,这个map存放各种xml或者注解中被映射的sql。这个map的key就是id,例如 student.insert。value指的是被映射的sql语句。上述错误的原因是找不到被映射的sql。

(2)Mapped Statements collection指的是map的名字,MyBatis的源码是这样的:

Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection");

MappedStatement对象对应mapper.xml配置文件中的一个select/update/insert/delete节点,描述的就是一条sql语句。

扩展一下:

Mapped Statements collection 的另外一个问题:Mapped Statements collection already contains value for...

MyBatis开发过程中经常遇到 Mapped Statements collection already contains value for xxx 这种错误,字面意思是说mapper中存在id重复的值,比如说同一个xml文件中有两个id为xxx的方法。这种情况经常是copy已有代码的时候忘了改id导致的。另外,还有可能是方法重载导致的,曾经有个网友就遇到这样的问题:

mybatis.jpg

因为mapper里面出现了重载方法,mybatis规定mapper接口里面不能出现重载方法。

标签: none

[网站公告]-[2024年兼职介绍]


仅有一条评论

  1. helloworld helloworld

添加新评论