多数据源扫错包问题

问题描述:配置多数据源之后一直扫错包,一个数据源总是扫到另外的mapper里面

知识拓展: @Repository注解和@Mapper注解区别

@Reponsitory注解
@Reponsitory使用后,在启动类上需要添加@MapperScan("xxx.xxx.xxx.mapper")注解

@Mapper注解
@Mapper注解使用后相当于@Reponsitory加@MapperScan注解,会自动进行配置加载

1.解决方法一
多数据源不要在Application启动类写@MapperScan() <此处不扫描是因为要分主副连接扫描什么的太麻烦所以干脆此处不用>

在相应的DataScoureCofig 里面使用@MapperScan()
并且@Mapper不要使用 改用@Repository
2.解决方法二
接口不用写任何注解
在 DatasourceConfig 上写MapperScan(basePackages = ““com.swcp.xxx.xxx.xxx””)

在SqlSessionFactory工厂注入地址

		sessionFactory.setMapperLocations(
				new PathMatchingResourcePatternResolver().getResources("classpath*:com/swcp/xxx/xxx/xxx/*.xml"));

@Bean(name = "mysqlSqlSessionFactory")
	public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqldatasource") DataSource dataSource)
			throws Exception {
		final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
		sessionFactory.setDataSource(dataSource);
		MybatisConfiguration configuration = new MybatisConfiguration();
		configuration.setCallSettersOnNulls(true);
		configuration.setMapUnderscoreToCamelCase(true);
		configuration.setDefaultEnumTypeHandler(EnumOrdinalTypeHandler.class);
		sessionFactory.setConfiguration(configuration);
//        sqlSessionFactoryBean.setMapperLocations(resolveMapperLocations());
		sessionFactory.setPlugins(new Interceptor[]{new PaginationInterceptor().setLimit(-1)});
		sessionFactory.setMapperLocations(
				new PathMatchingResourcePatternResolver().getResources("classpath*:com/swcp/xxx/xxx/xxx/*.xml"));
		return sessionFactory.getObject();
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值