Mapped Statements collection already contains value for xxxx
翻译为:映射语句集合已经包含xxxx的值
前提:我使用mybatis-Spring进行整合。其中mybatis-config.xml是mybatis默认的配置文件。
spring-dao.xml是mybatis-spring的配置文件。
下面是mybatis-config.xml代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias type="com.kuang.pojo.User" alias="User"/>
</typeAliases>
<!--
<environments default="test">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis? useSSL=true&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="root123"/>
</dataSource>
</environment>
</environments>
-->
<!--
遇到Mapped Statements collection already contains value for,原因是因为:
我在mybatis-spring的配置文件中 spring-dao.xml 中进行sqlSessionFactory配置时候,
引入了 mapperLocations 这个property 参数,已经绑定了UserMapper接口。
然后,下面代码又重新绑定了一遍。所以导致错误
-->
<mappers>
<mapper class="com.kuang.mapper.UserMapper"/>
</mappers>
</configuration>
下面是spring-dao.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis? useSSL=true&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="root123"/>
</bean>
<!--
这里使用mapperLocations绑定了接口,如果在mybatis的配置文件mybatis-config.xml再次绑定的话,就会报错
Mapped Statements collection already contains value for xxxx
-->
<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/kuang/mapper/*.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<bean id="userMapper" class="com.kuang.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
在idea中,遇到Mapped Statements collection already contains value for,原因是因为:
我在mybatis-spring的配置文件中 spring-dao.xml 中进行sqlSessionFactory配置时候, 引入了 mapperLocations 这个property 参数,已经绑定了UserMapper接口。 然后,下面代码又重新绑定了一遍。所以导致错误
<mappers> <mapper class="com.kuang.mapper.UserMapper"/> </mappers>
这里使用mapperLocations绑定了接口,如果在mybatis的配置文件mybatis-config.xml再次绑定的话,就会报错 Mapped Statements collection already contains value for xxxx
<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/kuang/mapper/*.xml"/> </bean>