基于注解的springboot+mybatis的多数据源组件的实现,你会吗

4
5 @Bean(name = “oracleDataSource”)
6 @ConfigurationProperties(prefix = “spring.secondary”)
7 public DataSource oracleDruid(){
8 return new DruidDataSource();
9 }
10
11 @Bean(name = “oracleSqlSessionFactory”)
12 public SqlSessionFactory oracleSqlSessionFactory(@Qualifier(“oracleDataSource”) DataSource dataSource) throws Exception {
13 SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
14 bean.setDataSource(dataSource);
15 bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(“classpath:oracle/mapper/*.xml”));
16 return bean.getObject();
17 }
18
19 @Bean(“oracleSqlSessionTemplate”)
20 public SqlSessionTemplate oracleSqlSessionTemplate(@Qualifier(“oracleSqlSessionFactory”) SqlSessionFactory sessionFactory) {
21 return new SqlSessionTemplate(sessionFactory);
22 }
23 }

这样,就实现了一个工程下使用多个数据源的功能,对于这种实现方式,其实也足够简单了,但是如果我们的数据库实例有很多,并且每个实例都主从配置,那这里维护起来难免会导致包名过多,不够灵活。

现在考虑实现一种对业务侵入足够小,并且能够在mapper方法粒度上去支持指定数据源的方案,那自然而然想到了可以通过注解来实现,首先,自定义一个注解@DBKey:

1 @Retention(RetentionPolicy.RUNTIME)
2 @Target({ElementType.METHOD, ElementType.TYPE})
3 public @interface DBKey {
4
5 String DEFAULT = “default”; // 默认数据库节点
6
7 String value() default DEFAULT;
8 }

思路和上面基于springboot原生的配置的类似,首先定义一个默认的数据库节点,当mapper接口方法/类没有指定任何注解的时候,默认走这个节点,注解支持传入value参数表示选择的数据源节点名称。至于注解的实现逻辑,可以通过反射来获取mapper接口方法/类的注解值,然后指定特定的数据源。

那在什么时候执行这个操作获取呢?可以考虑使用spring AOP织入mapper层,在切入点执行具体mapper方法之前,将对应的数据源配置放入threaLocal中,有了这个逻辑,立即动手实现:

首先,定义一个db配置的上下文对象。维护所有的数据源key实例,以及当前线程使用的数据源key:

1 public class DBContextHolder {
2
3 private static final ThreadLocal DB_KEY_CONTEXT = new ThreadLocal<>();
4
5 //在app启动时就加载全部数据源,不需要考虑并发
6 private static Set allDBKeys = new HashSet<>();
7
8 public static String getDBKey() {
9 return DB_KEY_CONTEXT.get();
10 }
11
12 public static void setDBKey(String dbKey) {
13 //key必须在配置中
14 if (containKey(dbKey)) {
15 DB_KEY_CONTEXT.set(dbKey);
16 } else {
17 throw new KeyNotFoundException(“datasource[” + dbKey + “] not found!”);
18 }
19 }
20
21 public static void addDBKey(String dbKey) {
22 allDBKeys.add(dbKey);
23 }
24
25 public static boolean containKey(String dbKey) {
26 return allDBKeys.contains(dbKey);
27 }
28
29 public static void clear() {
30 DB_KEY_CONTEXT.remove();
31 }
32 }

然后,定义切点,在切点before方法中,根据当前mapper接口的@@DBKey注解来选取对应的数据源key:

1 @Aspect
2 @Order(Ordered.LOWEST_PRECEDENCE - 1)
3 public class DSAdvice implements BeforeAdvice {
4
5 @Pointcut(“execution(* com.xxx….repository..*(…))”)
6 public void daoMethod() {
7 }
8
9 @Before(“daoMethod()”)
10 public void beforeDao(JoinPoint point) {
11 try {
12 innerBefore(point, false);
13 } catch (Exception e) {
14 logger.error(“DefaultDSAdviceException”,
15 “Failed to set database key,please resolve it as soon as possible!”, e);
16 }
17 }
18
19 /**
20 * @param isClass 拦截类还是接口
21 */
22 public void innerBefore(JoinPoint point, boolean isClass) {
23 String methodName = point.getSignature().getName();
24
25 Class<?> clazz = getClass(point, isClass); 26 //使用默认数据源 27 String dbKey = DBKey.DEFAULT; 28 Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()).getMethod().getParameterTypes();
29 Method method = null;
30 try {
31 method = clazz.getMethod(methodName, parameterTypes);
32 } catch (NoSuchMethodException e) {
33 throw new RuntimeException("can’t find " + methodName + " in " + clazz.toString());
34 }
35 //方法上存在注解,使用方法定义的datasource
36 if (method.isAnnotationPresent(DBKey.class)) {
37 DBKey key = method.getAnnotation(DBKey.class);
38 dbKey = key.value();
39 } else {
40 //方法上不存在注解,使用类上定义的注解
41 clazz = method.getDeclaringClass();
42 if (clazz.isAnnotationPresent(DBKey.class)) {
43 DBKey key = clazz.getAnnotation(DBKey.class);
44 dbKey = key.value();
45 }
46 }
47 DBContextHolder.setDBKey(dbKey);
48 }
49
50
51 private Class<?> getClass(JoinPoint point, boolean isClass) { 52 Object target = point.getTarget(); 53 String methodName = point.getSignature().getName(); 54 55 Class<?> clazz = target.getClass();
56 if (!isClass) {
57 Class<?>[] clazzList = target.getClass().getInterfaces();
58
59 if (clazzList == null || clazzList.length == 0) {
60 throw new MutiDBException(“找不到mapper class,methodName =” + methodName);
61 }
62 clazz = clazzList[0];
63 }
64
65 return clazz;
66 }
67 }

既然在执行mapper之前,该mapper接口最终使用的数据源已经被放入threadLocal中,那么,只需要重写新的路由数据源接口逻辑即可:

1 public class RoutingDatasource extends AbstractRoutingDataSource {
2
3 @Override
4 protected Object determineCurrentLookupKey() {
5 String dbKey = DBContextHolder.getDBKey();
6 return dbKey;
7 }
8
9 @Override
10 public void setTargetDataSources(Map<Object, Object> targetDataSources) {
11 for (Object key : targetDataSources.keySet()) {
12 DBContextHolder.addDBKey(String.valueOf(key));
13 }
14 super.setTargetDataSources(targetDataSources);
15 super.afterPropertiesSet();
16 }
17 }

另外,我们在服务启动,配置mybatis的时候,将所有的db配置加载:

1 @Bean
2 @ConditionalOnMissingBean(DataSource.class)
3 @Autowired
4 public DataSource dataSource(MybatisProperties mybatisProperties) {
5 Map<Object, Object> dsMap = new HashMap<>(mybatisProperties.getNodes().size());
6 for (String nodeName : mybatisProperties.getNodes().keySet()) {
7 dsMap.put(nodeName, buildDataSource(nodeName, mybatisProperties));
8 DBContextHolder.addDBKey(nodeName);
9 }
10 RoutingDatasource dataSource = new RoutingDatasource();
11 dataSource.setTargetDataSources(dsMap);
12 if (null == dsMap.get(DBKey.DEFAULT)) {
13 throw new RuntimeException(
14 String.format(“Default DataSource [%s] not exists”, DBKey.DEFAULT));
15 }
16 dataSource.setDefaultTargetDataSource(dsMap.get(DBKey.DEFAULT));
17 return dataSource;
18 }

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数同学面临毕业设计项目选题时,很多人都会感到无从下手,尤其是对于计算机专业的学生来说,选择一个合适的题目尤为重要。因为毕业设计不仅是我们在大学四年学习的一个总结,更是展示自己能力的重要机会。

因此收集整理了一份《2024年计算机毕业设计项目大全》,初衷也很简单,就是希望能够帮助提高效率,同时减轻大家的负担。
img
img
img

既有Java、Web、PHP、也有C、小程序、Python等项目供你选择,真正体系化!

由于项目比较多,这里只是将部分目录截图出来,每个节点里面都包含素材文档、项目源码、讲解视频

如果你觉得这些内容对你有帮助,可以添加VX:vip1024c (备注项目大全获取)
img

1)]

既有Java、Web、PHP、也有C、小程序、Python等项目供你选择,真正体系化!

由于项目比较多,这里只是将部分目录截图出来,每个节点里面都包含素材文档、项目源码、讲解视频

如果你觉得这些内容对你有帮助,可以添加VX:vip1024c (备注项目大全获取)
[外链图片转存中…(img-ewX2ltIe-1712573600542)]

  • 24
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值