Spring整合mybatis过程中出现的一个问题:
报错信息如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘sqlSessionFactory’ defined in class path resource
Caused by: org.springframework.core.NestedIOException: Failed to parse mapping resource:
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for

按照狂神的教学视频看的spring-mybatis整合,在看完自己手动敲代码运行的时候报了上面的个错,根据报错信息反复去检查配置文件,大小写啊,文件路径啊,完整的对比了两三次还是检查不出来,然后网上各种查报错,也没查出来结果。然后又把Spring的配置去掉,单独用mybatis的配置,结果跑出来了,然后换上spring的配置,又出现了这个错误,简直崩溃。后来又去仔细的看了一下视频,发现在配置spring的sqlSessionFactory的时候配置了mapperLocations和mapperLocations,如下:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/lr/dao/*.xml"/>
</bean>
当时只知道configLocation是引用mybatis的配置文件,没大搞清楚mapperLocations是干嘛的。所以在这里配置了mapperLocations的同时又在mybatis的配置文件里配置了mappers如下:
<mappers>
<mapper resource="com/lr/dao/UserMapper.xml"/>
</mappers>
然后这两个配置冲突了,所以导致了错误。。。
最终把mybatis的配置文件里的mapper映射删掉或者删掉spring对映射的配置就可以解决这个错误。
解决Spring与MyBatis整合时因mapper配置重复导致的错误。
2384

被折叠的 条评论
为什么被折叠?



