【第九章】 springboot + mybatis + 多数据源 (AOP实现)

在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改

1、ShopDao

package com.xxx.firstboot.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xxx.firstboot.domain.Shop;
import com.xxx.firstboot.mapper.ShopMapper;

@Repository
public class ShopDao {
    @Autowired
    private ShopMapper mapper;

    /**
     * 获取shop
     */
    public Shop getShop(int id) {
        return mapper.getShop(id);
    }
}
View Code

说明:只是去掉了设置数据源key的那一句代码

 

2、DataSourceAspect

package com.xxx.firstboot.common.datasource;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import com.xxx.firstboot.dao.ShopDao;

@Aspect
@Component
public class DataSourceAspect {
    
    @Before("execution(* com.xxx.firstboot.dao.*.*(..))")
    public void setDataSourceKey(JoinPoint point){
        //连接点所属的类实例是ShopDao
        if(point.getTarget() instanceof ShopDao){
            DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb2);
        }else{//连接点所属的类实例是UserDao(当然,这一步也可以不写,因为defaultTargertDataSource就是该类所用的mytestdb)
            DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb);
        }
    }
    
//    @Around("execution(* com.xxx.firstboot.dao.*.*(..))")
//    public Object setDataSourceKeyByAround(ProceedingJoinPoint point) throws Throwable{
//        if(point.getTarget() instanceof ShopDao){
//            DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb2);
//        }else{//连接点所属的类实例是UserDao(当然,这一步也可以不写,因为defaultTargertDataSource就是该类所用的mytestdb)
//            DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb);
//        }
//        return point.proceed();//执行目标方法
//    }
    
}
View Code

说明:列出了两种切面方法,在这里推荐使用前者,原因:

  • @Around:需要写执行目标方法的那一行代码,而这一行代码可能会抛异常,还需要抛出或捕获

对于切点表达式,可以抽取出来,进行重复利用。如上代码可以改为如下:

package com.xxx.firstboot.common.datasource;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.xxx.firstboot.dao.ShopDao;

@Aspect
@Component
public class DataSourceAspect {

    /**
     * 使用空方法定义切点表达式
     */
    @Pointcut("execution(* com.xxx.firstboot.dao.*.*(..))")
    public void declareJointPointExpression() {
    }

    /**
     * 使用定义切点表达式的方法进行切点表达式的引入
     */
    @Before("declareJointPointExpression()")
    public void setDataSourceKey(JoinPoint point) {
        // 连接点所属的类实例是ShopDao
        if (point.getTarget() instanceof ShopDao) {
            DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb2);
        } else {// 连接点所属的类实例是UserDao(当然,这一步也可以不写,因为defaultTargertDataSource就是该类所用的mytestdb)
            DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb);
        }
    }

}
View Code

注意:该切点表达式也可以用在其他切面类中,引入的时候使用"全类名.切点方法名()",例:@Before("com.xxx.firstboot.common.datasource.DataSourceAspect.declareJointPointExpression()")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值