业务场景,数据源可能是mysql也可能是impala。在mapper接口层级方法都是一样的。所以抽象出来一个父接口,再分别用mysql包下面的一个mapper和一个impala包mapper接口分别继承它。注意这俩mapper的beanId要区分开。
使用:首先有两个bean在不同的包下面,但是这俩类名是一样的。
然后使用set注入,使用全局变量参数区分使用那种mapper实现。
1.mapper接口层:伪代码如下:
interface parentMapper{
method1();
methhod2();
}
//下面连个接口类名相同 但是所在的包不同
@Repository("mysqlMapper")
interface mapper extend parentMapper{
}
@Repository("impalaMapper")
interface mapper extend parentMapper{
}
service层实现:
public Service3{
//声明一个成员变量
Mapper mapperParent;
//使用set注入mappper
@Autowired
public void setmapper(@Qualifier("mysqlMapper") ParentMapper mysqlMapper,@Qualifier(impalaMapper) ParentMapper impalaMapper){
if(){
mapperParent=mysqlMapper
}else{
mapperParent=impalaMapper
}
}
}
Spring框架下的MySQL与Impala数据源mapper接口抽象与区分策略,
1万+

被折叠的 条评论
为什么被折叠?



