|
7 | 7 | import org.springframework.stereotype.Component;
|
8 | 8 |
|
9 | 9 | /**
|
10 |
| - * 通过 aop 切换datasource |
| 10 | + * |
| 11 | + * <a href = "https://my.oschina.net/guangshan/blog/1808373">在Spring中使用MyBatis的Mapper接口自动生成时,用一个自定义的注解标记在Mapper接口的方法中,<br/> |
| 12 | + * 再利用@Aspect定义一个切面,拦截这个mapper中的增删改查注解实现数据源切换。<br/> |
| 13 | + * 在Spring Boot 1.X(Spring Framework 4.x)中,并不能生效,<br/> |
| 14 | + * 而在Spring Boot 2.X(Spring Framework 5.X)中却能生效。</a><br/> |
| 15 | + * |
| 16 | + * 通过 aop 切换 datasource |
11 | 17 | */
|
12 | 18 | @Aspect
|
13 | 19 | @Component
|
14 | 20 | public class DataSourceAopAspect implements PriorityOrdered {
|
15 | 21 |
|
16 |
| - @Before(value = "execution(* com.vincent.dao.UserDao.get*(..))") |
| 22 | + /** |
| 23 | + * {@link org.apache.ibatis.annotations.Select} 切入点 |
| 24 | + */ |
| 25 | + private static final String SELECT_POINTCUT = "@annotation(org.apache.ibatis.annotations.Select)"; |
| 26 | + |
| 27 | + /** |
| 28 | + * {@link org.apache.ibatis.annotations.SelectProvider} 切入点 |
| 29 | + */ |
| 30 | + private static final String SELECTPROVIDER_POINTCUT = "@annotation(org.apache.ibatis.annotations.SelectProvider)"; |
| 31 | + |
| 32 | + /** |
| 33 | + * {@link org.apache.ibatis.annotations.Insert} 切入点 |
| 34 | + */ |
| 35 | + private static final String INSERT_POINTCUT = "@annotation(org.apache.ibatis.annotations.Insert)"; |
| 36 | + |
| 37 | + /** |
| 38 | + * {@link org.apache.ibatis.annotations.InsertProvider} 切入点 |
| 39 | + */ |
| 40 | + private static final String INSERTPROVIDER_POINTCUT = "@annotation(org.apache.ibatis.annotations.InsertProvider)"; |
| 41 | + |
| 42 | + /** |
| 43 | + * {@link org.apache.ibatis.annotations.Update} 切入点 |
| 44 | + */ |
| 45 | + private static final String UPDATE_POINTCUT = "@annotation(org.apache.ibatis.annotations.Update)"; |
| 46 | + |
| 47 | + /** |
| 48 | + * {@link org.apache.ibatis.annotations.UpdateProvider} 切入点 |
| 49 | + */ |
| 50 | + private static final String UPDATEPROVIDER_POINTCUT = "@annotation(org.apache.ibatis.annotations.UpdateProvider)"; |
| 51 | + |
| 52 | + /** |
| 53 | + * {@link org.apache.ibatis.annotations.Delete} 切入点 |
| 54 | + */ |
| 55 | + private static final String DELETE_POINTCUT = "@annotation(org.apache.ibatis.annotations.Delete)"; |
| 56 | + |
| 57 | + /** |
| 58 | + * {@link org.apache.ibatis.annotations.DeleteProvider} 切入点 |
| 59 | + */ |
| 60 | + private static final String DeletePROVIDER_POINTCUT = "@annotation(org.apache.ibatis.annotations.DeleteProvider)"; |
| 61 | + |
| 62 | + /** |
| 63 | + * 切换到读库 |
| 64 | + * @param point |
| 65 | + */ |
| 66 | + @Before(value = SELECT_POINTCUT + " || " + SELECTPROVIDER_POINTCUT) |
17 | 67 | public void setReadDataSource(JoinPoint point) {
|
18 | 68 | DataSourceContextHolder.setRead();
|
19 | 69 | }
|
20 | 70 |
|
21 |
| - @Before(value = "execution(* com.vincent.dao.UserDao.add*(..))") |
| 71 | + /** |
| 72 | + * 切换到写库 |
| 73 | + * @param point |
| 74 | + */ |
| 75 | + @Before(value = INSERT_POINTCUT + " || " + INSERTPROVIDER_POINTCUT + " || " + UPDATE_POINTCUT + " || " + UPDATEPROVIDER_POINTCUT |
| 76 | + + " || " + DELETE_POINTCUT + " || " + DeletePROVIDER_POINTCUT) |
22 | 77 | public void setWriteDataSource(JoinPoint point) {
|
23 | 78 | DataSourceContextHolder.setWrite();
|
24 | 79 | }
|
|
0 commit comments