java MyBatis - 映射语句集合已经包含值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37085803/
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
MyBatis - Mapped Statements collection already contains value for
提问by Lucky
I had the following error messages thrown when registering the mapper classes on my server startup,
在我的服务器启动时注册映射器类时,我抛出了以下错误消息,
[artifact:mvn] 2016-05-07 11:39:21,708 [ERROR] org.mybatis.spring.mapper.MapperFactoryBean - Error while adding the mapper 'interface com.sample.mappers.UserMapper' to configuration.
[artifact:mvn] java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.sample.mappers.UserMapper.getAllUsers
[artifact:mvn] at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:802)
[artifact:mvn] at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:774)
[artifact:mvn] at org.apache.ibatis.session.Configuration.addMappedStatement(Configuration.java:598)
[artifact:mvn] at org.apache.ibatis.builder.MapperBuilderAssistant.addMappedStatement(MapperBuilderAssistant.java:300)
[artifact:mvn] at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parseStatement(MapperAnnotationBuilder.java:313)
[artifact:mvn] at org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parse(MapperAnnotationBuilder.java:128)
[artifact:mvn] at org.apache.ibatis.binding.MapperRegistry.addMapper(MapperRegistry.java:72)
[artifact:mvn] at org.apache.ibatis.session.Configuration.addMapper(Configuration.java:671)
[artifact:mvn] at org.mybatis.spring.mapper.MapperFactoryBean.checkDaoConfig(MapperFactoryBean.java:81)
[artifact:mvn] at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
I used annotations for my mapper interfaces and there's no xml configuration.
我为我的映射器接口使用了注释,并且没有 xml 配置。
Below is my UserMapper interface,
下面是我的 UserMapper 界面,
public interface UserMapper {
@Select("SELECT * FROM customer")
List<User> getAllUsers();
@Select("SELECT * FROM customer where userId = #{userId} ")
List<User> getAllUsers(Long userId);
}
回答by Lucky
I found out the cause of the error message. If you have the same method name, then mybatis throws the Mapped Statements collection already contains value
error message. So the solution is to have different method names for different mapper statements even if the method signatures are different.
我找到了错误消息的原因。如果你有相同的方法名,那么mybatis 会抛出Mapped Statements collection already contains value
错误信息。因此,解决方案是即使方法签名不同,也为不同的映射器语句使用不同的方法名称。
So In my mapper interface the method names second getAllUsers()
name should be getUserById();
. The same error is thrown if you have the same method name in any of the mapper.xml
files. So it is mandatory to have unique method names or mapper namespace for different sql statements for mybatis to map this at runtime.
所以在我的映射器界面中,方法名称的第二个getAllUsers()
名称应该是getUserById();
. 如果您在任何mapper.xml
文件中具有相同的方法名称,则会引发相同的错误。所以对于不同的 sql 语句,mybatis 必须有唯一的方法名称或映射器命名空间,以便在运行时映射它。
回答by Rob
For some reason this error was thrown when I had a resultType set to a class that no longer existed. Once I updated the class, the problem was resolved. Not sure why this particular error was thrown, but perhaps check your resultTypes as well if you come across this.
出于某种原因,当我将 resultType 设置为不再存在的类时,会引发此错误。一旦我更新了课程,问题就解决了。不知道为什么抛出这个特定的错误,但如果你遇到这个,也许也检查你的 resultTypes 。
回答by hemantvsn
In my case, the occurance was due to resultMaps were added to the Configuration after addition of mapper.
就我而言,发生这种情况是由于在添加映射器后将 resultMaps 添加到配置中。
Eg:
例如:
Configuration configuration = new Configuration(environment);
configuration.addMappers("com.hemant.data.mapper");
configuration.addResultMap(getResultMapForRoles(configuration));
If we observe MyBatis source code, then on configuration.addMappers(..) ..... parse() is executed. org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parse() org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parseStatement(Method)
如果我们观察 MyBatis 源代码,那么在 configuration.addMappers(..) ..... parse() 上执行。 org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parse() org.apache.ibatis.builder.annotation.MapperAnnotationBuilder.parseStatement(方法)
for (Method method : methods) {
try {
// issue #237
if (!method.isBridge()) {
parseStatement(method);
}
} catch (IncompleteElementException e) {
configuration.addIncompleteMethod(new MethodResolver(this, method));
}
}
If there is an issue with parsing the statement (which occured in mycase as the statement had @ResultMap annotation, but resultMap wasn't provided to configuration.), the method is added in INCOMPLETE_METHODS in configuration which later raises the exception.
如果解析语句时出现问题(发生在 mycase 中,因为语句具有 @ResultMap 注释,但未将 resultMap 提供给配置。),该方法将添加到配置中的 INCOMPLETE_METHODS 中,稍后会引发异常。
@Select("select role_id, role_name, allowed_actions::bigint, denied_actions::bigint from acm_role")
@ResultMap("com.hemant.data.mapper.RoleMapper." + PermissionDBHook.ROLE_MAP)
public List<Role> getAllRoles();
On adding resultMap to configuration before mapper, solved it
在映射器之前将结果映射添加到配置中,解决了它
Configuration configuration = new Configuration(environment);
//Hemant - result map should be added before mappers
configuration.addResultMap(getResultMapForRoles(configuration));
configuration.addMappers("com.hemant.data.mapper");
回答by Hashken
This answer is not directly relevant to the question, but could be useful for others facing a similar error:
这个答案与问题没有直接关系,但可能对面临类似错误的其他人有用:
This error seems to be thrown when MyBatis finds multiple definitions for one of the methods in the mapper. In my case, I had both the Annotation and a definition in the XMLfor a method. Removing one of these resolved the issue.
当 MyBatis 在映射器中找到其中一种方法的多个定义时,似乎会抛出此错误。就我而言,我在 XML中有一个方法的注释和定义。删除其中之一解决了问题。
回答by cncDAni3
For me the problem was not with that query - it was just the first, so it shows that one. The real problem was with a type, that I resolved with a typeAlias. (I missed an annotation...)
对我来说,问题不在于那个查询——它只是第一个,所以它显示了那个。真正的问题出在类型上,我用 typeAlias 解决了这个问题。(我错过了一个注释...)
回答by glory zheng
Maybe your parameter type is wrong. like resultType or resultMap ,parameterType etc...
可能你的参数类型不对。像 resultType 或 resultMap ,parameterType 等...