每天一个注解之@DataSource、 @DS

news2024/10/6 1:37:38

在Java中,@DataSource 注解通常用于标记数据源(DataSource)相关的信息。数据源是一个用于获取数据库连接的对象,它通常用于与数据库进行交互。@DataSource 注解的详细说明可能会因不同的框架或库而有所不同,但通常用于配置数据源的一些属性,例如数据库的连接URL、用户名、密码等。

方案一

不同的包具体实现是不一样的,举例这个注解,主要是为了引出多数据源的配置方法。

依赖

       <dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
			<version>3.4.1</version>
		</dependency>

配置:

在这里插入图片描述

使用不同的数据源

@DS(“master”), master 就是配置的数据源名称。
在这里插入图片描述

说明

@DS 可以注解在方法上和类上。如果同时存在,方法注解优先于类上注解。强烈建议注解在 service 实现或 mapper 接口方法上。

@DS(“xxx”) 指定使用 xxx 这个数据源,xxx 可以为组名也可以为具体某个库的名称。如果是组名则切换时采用负载均衡算法切换。如果指定的组名或者库不存在,则自动使用默认数据源(主库)

如果没有 @DS,则使用默认数据源(主库)

如果设置了 @DS 但没有指定某个组或者库,则根据 DynamicDataSourceStrategy 策略,选择一个从库。默认负载均衡策略。

方案二:借助com.alibaba.druid 自己实现多数据源

依赖

       <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>
spring:
  profiles: dev
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/pos?serverTimezone=UTC&useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8
    username: root
    password: 123456

#多数据源
biz:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/biz?serverTimezone=UTC&useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8
    username: root
    password: 123456

java config 类

package com.xncoding.pos.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.xncoding.pos.common.mutidatesource.DSEnum;
import com.xncoding.pos.common.mutidatesource.DynamicDataSource;
import com.xncoding.pos.config.properties.DruidProperties;
import com.xncoding.pos.config.properties.MutiDataSourceProperties;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.sql.SQLException;
import java.util.HashMap;

/**
 * MybatisPlus配置
 *
 * @author xiongneng
 * @since 2017/5/20 21:58
 */
@Configuration
@EnableTransactionManagement(order = 2)
@MapperScan(basePackages = {"com.xncoding.pos.common.dao.repository"})
public class MybatisPlusConfig {

    @Autowired
    DruidProperties druidProperties;

    @Autowired
    MutiDataSourceProperties mutiDataSourceProperties;

    /**
     * 核心数据源
     */
    private DruidDataSource coreDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        druidProperties.config(dataSource);
        return dataSource;
    }

    /**
     * 另一个数据源
     */
    private DruidDataSource bizDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        druidProperties.config(dataSource);
        mutiDataSourceProperties.config(dataSource);
        return dataSource;
    }

    /**
     * 单数据源连接池配置
     */
    @Bean
    @ConditionalOnProperty(prefix = "xncoding", name = "muti-datasource-open", havingValue = "false")
    public DruidDataSource singleDatasource() {
        return coreDataSource();
    }

    /**
     * 多数据源连接池配置
     */
    @Bean
    @ConditionalOnProperty(prefix = "xncoding", name = "muti-datasource-open", havingValue = "true")
    public DynamicDataSource mutiDataSource() {

        DruidDataSource coreDataSource = coreDataSource();
        DruidDataSource bizDataSource = bizDataSource();

        try {
            coreDataSource.init();
            bizDataSource.init();
        } catch (SQLException sql) {
            sql.printStackTrace();
        }

        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        HashMap<Object, Object> hashMap = new HashMap<>();
        hashMap.put("dataSourceCore", coreDataSource);
        hashMap.put("dataSourceBiz", bizDataSource);
        dynamicDataSource.setTargetDataSources(hashMap);
        dynamicDataSource.setDefaultTargetDataSource(coreDataSource);
        return dynamicDataSource;
    }

    /**
     * mybatis-plus分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}


自定义一个注解:


/**
 * 多数据源标识
 *
 * @author xiongneng
 * @since 2017年3月5日 上午9:44:24
 */
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface DataSource {

    String name() default "";
}

aop 捕获注解,切换数据源:

package com.xncoding.pos.common.aop;

import com.xncoding.pos.common.annotion.DataSource;
import com.xncoding.pos.common.mutidatesource.DSEnum;
import com.xncoding.pos.common.mutidatesource.DataSourceContextHolder;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 多数据源切换的aop
 *
 * @author xiongneng
 * @since 2017年3月5日 上午10:22:16
 */
@Aspect
@Component
@ConditionalOnProperty(prefix = "xncoding", name = "muti-datasource-open", havingValue = "true")
public class MultiSourceExAop implements Ordered {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Pointcut(value = "@annotation(com.xncoding.pos.common.annotion.DataSource)")
    private void cut() {

    }

    @Around("cut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {

        Signature signature = point.getSignature();
        MethodSignature methodSignature = null;
        if (!(signature instanceof MethodSignature)) {
            throw new IllegalArgumentException("该注解只能用于方法");
        }
        methodSignature = (MethodSignature) signature;

        Object target = point.getTarget();
        Method currentMethod = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());

        DataSource datasource = currentMethod.getAnnotation(DataSource.class);
        if (datasource != null) {
            DataSourceContextHolder.setDataSourceType(datasource.name());
            log.debug("设置数据源为:" + datasource.name());
        } else {
            DataSourceContextHolder.setDataSourceType("dataSourceCore");
            log.debug("设置数据源为:dataSourceCore");
        }
        try {
            return point.proceed();
        } finally {
            log.debug("清空数据源信息!");
            DataSourceContextHolder.clearDataSourceType();
        }
    }


    /**
     * aop的顺序要早于spring的事务
     */
    @Override
    public int getOrder() {
        return 1;
    }

}

使用:

在这里插入图片描述

两种方法都能实现多数据源。此外还有很多的实现方法。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1575333.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

2024-04-07 作业

作业要求&#xff1a; 1> 思维导图 2> 自由发挥应用场景实现一个登录窗口界面。 【可以是QQ登录界面、也可以是自己发挥的登录界面】 要求&#xff1a;尽量每行代码都有注释 作业1&#xff1a; 作业2&#xff1a; 运行代码&#xff1a; #include "myqwidget.h&quo…

部署安装ElasticSearch、Kibana、IK

文章目录 1、部署单点es1.1、创建网络1.2、加载镜像1.3、运行 2、部署kibana2.1、部署2.2、DevTools 3、IK分词器3.1、在线安装3.2、离线安装1&#xff09;查看数据卷目录2&#xff09;解压缩分词器安装包3&#xff09;上传到es容器的插件数据卷中4&#xff09;重启容器5&#…

2024.4.7

1. 2列火车 #include<myhead.h>pthread_mutex_t m1; pthread_mutex_t m2;void* run(void* arg) {while(1){pthread_mutex_lock(&m1);printf("火车B进入\n");printf("A请等待\n");pthread_mutex_unlock(&m2);sleep(2);} }int main(in…

火山方舟大模型服务平台调用Demo测试(豆包)

豆包得后台大模型支持为字节得火山方舟&#xff0c;所以想使用豆包的API&#xff0c;直接从这里就可以。 一、首先注册账号&#xff1a; 火山引擎-云上增长新动力 注册完成之后&#xff0c;控制台-账户-API访问密钥 二、找到API测试用例&#xff1a; Skylark-chat API调用…

白盒测试-语句覆盖

​ 语句覆盖法是指设计适当数量的测试用例&#xff0c;使被测程序中的每条语句至少被执行一次。语句覆盖率的计算方法为&#xff1a; ​ 至少被执行一次的语句数量 / 程序中可执行的语句总数。 案例 ​ 为了清晰地比较几种逻辑覆盖法设计测试用例的异同&#xff0c;逻辑覆盖…

LeetCode热题100:哈希

1.两数之和 题目链接&#xff1a;两数之和 题目描述&#xff1a;给定一个整数数组 nums 和一个整数目标值 target&#xff0c;请你在该数组中找出 和为目标值 target 的那 两个 整数&#xff0c;并返回它们的数组下标。 你可以假设每种输入只会对应一个答案。但是&#xff0c;数…

11.2 浏览器调试常用技巧

目录 1、开发者工具介绍 2、查看节点事件 3、断点调试 4、观察调用栈 5、恢复 JavaScript 执行 6、Ajax 断点 7、改写 JavaScript 文件 1、开发者工具介绍 由于需要学习 JavaScript 逆向&#xff0c;所以此文主要介绍与 JavaScript 逆向有帮助的功能。 以下链接为例&a…

国内:深圳交通流量数据集

数据来源&#xff1a;深圳政府数据开放平台&#xff08;深圳市政府数据开放平台&#xff09;&#xff0c;这个官网上还有其他类数据集&#xff0c;值得收藏&#xff01;&#xff01;&#xff01; 数据集介绍&#xff1a;宝安区-G4高速西乡大道入口车流量统计 第一行每列的标题…

记一次Debug与Release版程序输出不一致的问题解决

问题叙述&#xff1a; 在x86平台下无论Debug还是Release都没问题&#xff0c;而在arm平台下Debug版本程序无问题&#xff0c;Release版本程序&#xff08;-O3编译&#xff09;发现输出值不正确&#xff0c;怀疑值被篡改&#xff0c;于是在调用前后分别使用printf打印出参数值&…

vitepress系列-04-规整sideBar左侧菜单导航

规整左侧菜单导航 新建navConfig.ts 文件用来管理左侧导航菜单&#xff1a; 将于其他的配置分开&#xff0c;避免config.mts太大 在config目录下&#xff0c;新建 sidebarModules文件目录用来左侧导航菜单 按模块进行分类&#xff1a; 在config下新建sidebarConfig.ts文件&…

3dmax经常染失败?优化方法提升染质量!

在三维建模和渲染的过程中&#xff0c;优化模型和场景的效率是至关重要的。以下是一些提升效率的方法&#xff1a; 模型简化&#xff1a;在创建模型时&#xff0c;应尽量减少使用的命令和修改器的数量。这是因为命令和修改器越多&#xff0c;消耗的内存和CPU资源也就越多&…

vitepress系列-05-其他优化设置

其他优化设置 设置底部上一页和下一页 设置&#xff1a; import { defineConfig } from vitepress// https://vitepress.dev/reference/site-config export default defineConfig({lang: en-US,title: "东东爱编码的技术博客",description: "记录日常学习点点…

Cute Background FX

Cute Background FX是环境背景粒子系统的集合。非常适合作为菜单的背景。 该包包括: -20个独特预制件+20个URP预制件 -5种独特的环境设计 -15种纹理 -2个自定义着色器+2个URP着色器 -共59项独特资产 -一个演示场景,您可以在其中概述所有内容。 所有纹理都是512x512分辨率的P…

布隆过滤器详解及java实现

什么是布隆过滤器&#xff1f; 布隆过滤器&#xff08;Bloom Filter&#xff09;是一种数据结构&#xff0c;用于判断一个元素是否属于一个集合。它的特点是高效地判断一个元素是否可能存在于集合中&#xff0c;但是存在一定的误判率。 布隆过滤器的基本原理是使用一个位数组…

原型变量、原子操作、原子性、内存序

一、原子变量、原子操作 锁竞争&#xff1a;互斥锁、条件变量、原子变量、信号量、读写锁、自旋锁。在高性能基础组件优化的时候&#xff0c;为了进一步提高并发性能&#xff0c;可以使用原子变量。性能&#xff1a;原子变量 > 自旋锁 > 互斥锁。 操作临界资源的时间较长…

第十讲 Query Execution Part 1

1 处理模型【Processing Model】 DBMS 的处理模型【Processing Model】定义了系统如何执行【execute】查询计划【Query Plan】。 针对不同的工作负载进行不同的权衡。 方法1&#xff1a;迭代器模型【Iterator Model】 方法2&#xff1a;物化模型【Materialization Model】 方…

linux虚拟机上安装,使用以及远程连接mysql

1. 安装mysql 5.7 1) 首先更新软件源 sudo apt-get update 2) 安装MySQL数据库软件 ​ sudo apt-get install mysql-server 3) 安装MySQL数据库管理软件​ sudo apt-get install mysql-client 4) 安装MySQL数据库客户端&#xff0c;用户访问数据库 sudo apt-get install…

JAVA—抽象—定义抽象类Converter及其子类WeightConverter

同样&#xff0c;我们由这道题引出抽象类&#xff0c;抽象方法这个概念。 按下面要求定义类Converter及其子类WeightConverter 定义抽象类&#xff1a;Converter&#xff1a; 定义一个抽象类Converter&#xff0c;表示换算器&#xff0c;其定义的如下&#xff1a; 一个私有…

HarmonyOS实战开发DLP-如何实现一个安全类App。

介绍 本示例是一个安全类App&#xff0c;使用ohos.dlpPermission 接口展示了在eTS中普通文件加密受限的过程。 效果预览 使用说明: 1.启动应用后点击“”按钮可以添加一个普通文件; 2.长按点击加密按钮&#xff0c;出现加密权限弹窗&#xff0c;选择需要设置的权限并点击确定…

【C语言】_文件类型,结束判定与文件缓冲区

目录 1. 文本文件和二进制文件 2. 文件读取结束的判定 3. 文件缓冲区 1. 文本文件和二进制文件 根据数据的组织形式&#xff0c;数据文件被称为文本文件或二进制文件&#xff1b; 数据在内存中以二进制的形式存储&#xff0c;如果不加转换地输出到外存&#xff0c;就是二进…