在使用Mybatis跟数据库进行交互时出现如下错误:
注意这两句话:Mapped Statements collection already contains value(映射语句集合已包含值),please check com/zjx/dao/BookMapper.xml
然后我们去看BookMapper
<mapper namespace="com.zjx.dao.BookMapper">
<insert id="addBook" parameterType="Book">
insert into books(bookName, bookCounts, detail)
values (#{bookName}, #{bookCounts}, #{detail})
</insert>
<delete id="deleteByID" parameterType="_int">
DELETE FROM books WHERE bookID = #{id};
</delete>
<update id="updateBy" parameterType="Book">
update books
<set>
<if test="bookName != null">
set bookName = #{book.bookName}
</if>
<if test="bookCounts != null">
set bookCounts = #{book.bookCounts}
</if>
<if test="detail != null">
set detail = #{book.detail}
</if>
</set>
where bookID = #{book.bookID}
</update>
<select id="queryForOneByID" resultType="Book">
select * from books where bookID = #{id}
</select>
<select id="queryForAll" resultType="Book">
select * from books
</select>
</mapper>
这里面根本没有重复的属性呀
于是乎,百度到我的applicationContext.xml中既配置了configLocation,又配置了mapperLocations,如下:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入连接池
给 sqlsessionFactory 的属性 dataSource 赋值
ref="引用该 spring容器 中的另一个 bean的id"
-->
<property name="dataSource" ref="dataSource"/>
<!--指定mybatis的全局配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--指定mybatis,mapper文件的位置-->
<property name="mapperLocations" value="classpath:com/zjx/dao/*.xml"/>
</bean>
然后,我把mapperLocations去掉,tomcat就成功跑起来