运维开发网

Java MyBatis – Mapped Statements集合已包含值

运维开发网 https://www.qedev.com 2020-04-08 13:50 出处:网络 作者:运维开发网整理
在我的服务器启动时注册mapper类时,我抛出了以下错误消息, [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
在我的服务器启动时注册mapper类时,我抛出了以下错误消息,

[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)

我为我的mapper接口使用了注释,并且没有xml配置.

下面是我的UserMapper界面,

public interface UserMapper {

  @Select("SELECT  * FROM customer")
  List<User> getAllUsers();

  @Select("SELECT * FROM customer where userId = #{userId} ")
  List<User> getAllUsers(Long userId);

}
我找到了错误消息的原因.如果您具有相同的方法名称,则mybatis会抛出Mapped Statements集合已包含值错误消息.因此解决方案是为不同的映射器语句设置不同的方法名称,即使方法签名不同也是如此.

所以在我的mapper接口中,方法名称第二个getAllUsers()名称应该是getUserById();.如果在任何mapper.xml文件中具有相同的方法名称,则会引发相同的错误.因此,必须为mybatis的不同sql语句提供唯一的方法名称或映射器命名空间,以便在运行时映射它.

0

精彩评论

暂无评论...
验证码 换一张
取 消