首页 > 网站 > WEB开发 > 正文

spring自动管理多数据库

2024-04-27 15:10:44
字体:
来源:转载
供稿:网友
package data.bi.enable.datasource;import org.aspectj.lang.PRoceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.springframework.stereotype.Component;import data.bi.core.DynamicDataSource;import data.bi.dao.BIAutoDao;import data.bi.dao.BIDataDao;import data.bi.dao.BIIndexDao;import data.bi.dao.BISocialDao;import data.bi.dao.BITestDao;import data.bi.dao.BIUserDao;import data.bi.dao.BIYokaDao;/** * spring自动管理多数据库 * @author Administrator * */@Component@Aspectpublic class MultipleDataSourceAspectAdvice { /** * 1.注意包名,data.online.dao是操作数据库接口所在包名 * 2.注意instanceof 后面跟的是接口的名称从而判定调用哪个数据库 * @param jp * @return * @throws Throwable */ @Around("execution(* data.bi.dao.*.*(..))") public Object doAround(ProceedingJoinPoint jp) throws Throwable { if (jp.getTarget() instanceof BIDataDao) { DynamicDataSource.setDataSourceKey("dataSource_top100"); } else if (jp.getTarget() instanceof BIUserDao) { DynamicDataSource.setDataSourceKey("dataSource_user"); } else if (jp.getTarget() instanceof BISocialDao){ DynamicDataSource.setDataSourceKey("dataSource_social"); } else if (jp.getTarget() instanceof BITestDao){ DynamicDataSource.setDataSourceKey("dataSource_test"); } else if (jp.getTarget() instanceof BIYokaDao){ DynamicDataSource.setDataSourceKey("dataSource_beautyl"); } else if (jp.getTarget() instanceof BIAutoDao){ DynamicDataSource.setDataSourceKey("dataSource_16_auto"); } else if (jp.getTarget() instanceof BIIndexDao){ DynamicDataSource.setDataSourceKey("dataSource_test"); } return jp.proceed(); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表