Spring Boot 集成 MyBatis 报错:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' ...

该错误通常发生在 Spring Boot 集成 MyBatis 时,错误信息中显示 Cannot resolve reference to bean 'sqlSessionFactory' ... 以及 Mapped Statements collection already contains value for ...,这表明在 MyBatis 映射文件中存在重复的 SQL 语句定义。

错误原因

该错误通常是因为在你的 MyBatis 映射文件中,已经存在了一个 id 为 'save' 的 SQL 语句,而你在扫描 mapper 接口时又重复定义了一个相同 id 的 SQL 语句。

解决方法

  1. 检查项目中是否存在重复的 mapper 接口或映射文件。 如果存在,可以删除其中一个。

  2. 检查是否有其他地方也引用了相同的 mapper 接口或映射文件。 如果有,可以将其删除或注释掉。

  3. 检查 Mapper.xml 文件中是否存在重复的 id 属性。 如果存在,请确保每个 id 属性都是唯一的。

示例代码

applicationContext.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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- 自动扫描Spring注解配置 -->
    <context:component-scan base-package="org.example">
        <!--不扫描@Controller,以免与spring mvc的扫描配置一样,导致重复扫描-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

    <!-- 1.mysql连接的基本配置,使用的是spring提供的 DriverManagerDataSource-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="username" value="root"></property>
        <property name="password" value="zhangjie123."></property>
        <property name="url" value="jdbc:mysql://localhost:3306/ebuy_rj?characterEncoding=UTF-8"></property>
    </bean>

    <!--
       2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源
       MyBatis定义数据源,同意加载配置
   -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--引入mybatis的配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <!--自动扫描mybatis的mapper.xml文件-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>

    <!--
       3. mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory
       basePackage:DAO接口所在包名,Spring会自动查找其下的类
   -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
        <!-- mapper接口扫描的包,即将此包下所有符合的mapper接口加载到SpringIOC容器管理 -->
        <property name="basePackage" value="org.example.dao"></property>
    </bean>

    <!--
 4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源
-->
    <bean id="txManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 5. 启用事务,使用声明式事务
     transaction-manager:引用上面定义的事务管理器
 -->
    <tx:annotation-driven transaction-manager="txManager" />
</beans>

AdminMapper.xml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.dao.AdminDao">
    <select id="countByUsernameAndPassword" resultType="int">
        select count(1) from admin where username=#{username} and password=#{password}
    </select>

    <select id="findByUsername" resultType="Admin">
        select * from admin where username=#{username}
    </select>

    <select id="findAll" resultType="Admin">
        select * from admin ORDER BY createTime asc
    </select>

    <delete id="deleteById">
        delete from admin where id=#{id}
    </delete>

    <insert id="save" >
        insert into admin(id,username,password,name,sex,unlocked,createTime) values(null,#{username},#{password},#{name},#{sex},#{unlocked},now())
    </insert>

    <select id="findById" resultType="Admin">
        select * from admin where id=#{id}
    </select>


</mapper>

总结

通过仔细检查项目代码,确保每个 Mapper.xml 文件中 id 属性的唯一性,并避免重复定义相同 id 的 SQL 语句,可以有效解决该问题。

Spring Boot 集成 MyBatis 报错:`org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' ...`

原文地址: https://www.cveoy.top/t/topic/pcRu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录