报错信息:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [D:\JAVAWeb-zj\ebuy\out\artifacts\ebuy_web_war_exploded\WEB-INF\classes\mapper\AdminMapper.xml]'; nested exception is java.lang.IllegalArgumentException: Mapped Statements collection already contains value for org.example.dao.AdminDao.save

错误原因:

这个错误通常发生在 MyBatis 配置文件中,存在 重复定义的映射语句(Mapped Statement)

解决方法:

  1. 检查是否存在重复的映射语句定义。 - 仔细检查所有 Mapper 接口和 Mapper XML 文件,确认是否在不同的 Mapper 中定义了相同 id 的映射语句。 - 例如,在上面的错误信息中,出现了 Mapped Statements collection already contains value for org.example.dao.AdminDao.save,说明在某个 Mapper 中已经存在 idsave 的映射语句了。

  2. 修改重复的映射语句名称或使用不同的命名空间。 - 修改名称: 选择一个重复的映射语句,修改其 id 属性,使其与其他映射语句的 id 不同。 - 使用不同的命名空间: 在 Mapper XML 文件中,使用不同的 namespace 属性来区分不同的 Mapper 接口。例如,可以在 AdminMapper.xml 文件中添加一个不同的命名空间,例如:<mapper namespace='org.example.dao.AdminMapper2'>。然后,在代码中使用对应的命名空间来调用该 Mapper 接口的方法。

示例代码:

applicationContext.xmlxml

<!-- 自动扫描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 name='url' value='jdbc:mysql://localhost:3306/ebuy_rj?characterEncoding=UTF-8'/>    </bean>

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

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

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

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

AdminMapper.xmlxml

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

</mappe

MyBatis 映射语句重复错误:'Mapped Statements collection already contains value for ...'

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

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