按照你的方法我修改之后,我依然报错了:Description: file [E:\idea\workplace\mybatis_scoring_system\target\classes\com\cvte\mapper\UserMapper.class] required a single bean, but 2 were found: - sqlSessionFactoryBean: defined by method 'sqlSessionFactoryBean' in class path resource [com/cvte/config/MyBatisConfig.class] - sqlSessionFactory: defined by method 'sqlSessionFactory' in class path resource [com/cvte/config/MybatisInterceptorConfig.class] Action: Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
时间: 2024-03-06 15:46:48 浏览: 299
node.js使用npm 安装插件时提示install Error: ENOENT报错的解决方法
5星 · 资源好评率100%
这个错误提示是因为你在配置 MyBatis 的时候,定义了两个 SqlSessionFactory,导致 Spring 无法确定要使用哪个 SqlSessionFactory。
你可以在 MyBatis 的配置类中,使用 @Primary 注解来标记一个 SqlSessionFactory,表示它是首选的 SqlSessionFactory。例如:
```
@Configuration
@MapperScan("com.cvte.mapper")
public class MyBatisConfig {
@Bean
@Primary
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
return sqlSessionFactoryBean;
}
}
```
这样,就可以将 sqlSessionFactoryBean 标记为首选的 SqlSessionFactory,其他地方如果需要使用 SqlSessionFactory,就可以使用 @Autowired 注解来获取 sqlSessionFactoryBean 对象了。
阅读全文