当前位置: 首页 > wzjs >正文

网站建设推广语言大连做网站开发的公司

网站建设推广语言,大连做网站开发的公司,seo关键词排名网络公司,小视频广告目录 一、前言二、操作1、引入依赖2、配置默认数据库 13、定义数据源实体和 Repository4、定义动态数据源5、配置数据源6、定义切换数据源注解7、定义切面类8、使用注解切换数据源 一、前言 通过切面注解方式根据不同业务动态切换数据库 二、操作 1、引入依赖 <dependen…

目录

    • 一、前言
    • 二、操作
      • 1、引入依赖
      • 2、配置默认数据库 1
      • 3、定义数据源实体和 Repository
      • 4、定义动态数据源
      • 5、配置数据源
      • 6、定义切换数据源注解
      • 7、定义切面类
      • 8、使用注解切换数据源

一、前言

通过切面注解方式根据不同业务动态切换数据库

二、操作

1、引入依赖

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId></dependency>
</dependencies>

2、配置默认数据库 1

  • 在 application.properties 或 application.yml 配置数据库 1 信息:
spring.datasource.url=jdbc:mysql://localhost:3306/db1
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

3、定义数据源实体和 Repository

  • 数据源实体 DataSourceConfig.java:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity
public class DataSourceEntity {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String url;private String username;private String password;private String driverClassName;private String dataSourceKey; // 新增字段// Getters 和 Setterspublic Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getDriverClassName() {return driverClassName;}public void setDriverClassName(String driverClassName) {this.driverClassName = driverClassName;}public String getDataSourceKey() {return dataSourceKey;}public void setDataSourceKey(String dataSourceKey) {this.dataSourceKey = dataSourceKey;}
}
  • Repository 接口 DataSourceConfigRepository.java:
import org.springframework.data.jpa.repository.JpaRepository;public interface DataSourceConfigRepository extends JpaRepository<DataSourceEntity, Long> {
}

4、定义动态数据源

  • 动态数据源上下文持有者 DynamicDataSourceContextHolder.java:
public class DynamicDataSourceContextHolder {private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();public static void setDataSourceKey(String key) {contextHolder.set(key);}public static String getDataSourceKey() {return contextHolder.get();}public static void clearDataSourceKey() {contextHolder.remove();}
}
  • 动态数据源类 DynamicDataSource.java:
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;public class DynamicDataSource extends AbstractRoutingDataSource {@Overrideprotected Object determineCurrentLookupKey() {return DynamicDataSourceContextHolder.getDataSourceKey();}
}

5、配置数据源

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@Configuration
public class DataSourceConfig {@Autowiredprivate DataSourceConfigRepository dataSourceConfigRepository;@Primary@Beanpublic DataSource dataSource() {DynamicDataSource dynamicDataSource = new DynamicDataSource();Map<Object, Object> targetDataSources = new HashMap<>();// 从数据库 1 的 datasource 表加载所有数据源List<DataSourceEntity> dataSourceConfigs = dataSourceConfigRepository.findAll();for (DataSourceEntity config : dataSourceConfigs) {HikariDataSource dataSource = new HikariDataSource();dataSource.setJdbcUrl(config.getUrl());dataSource.setUsername(config.getUsername());dataSource.setPassword(config.getPassword());dataSource.setDriverClassName(config.getDriverClassName());targetDataSources.put(config.getId().toString(), dataSource);if ("db1".equals(config.getDataSourceKey())) { // 假设表中有一个字段表示数据源的 keydynamicDataSource.setDefaultTargetDataSource(dataSource);}}dynamicDataSource.setTargetDataSources(targetDataSources);return dynamicDataSource;}
}

6、定义切换数据源注解

import java.lang.annotation.*;@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSourceSwitch {String value() default "db1";
}

7、定义切面类

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;import java.lang.reflect.Method;
import java.util.List;@Aspect
@Component
public class DataSourceSwitchAspect {@Autowiredprivate DataSourceConfigRepository dataSourceConfigRepository;@Before("@annotation(com.example.annotation.DataSourceSwitch)")public void before(JoinPoint point) {MethodSignature signature = (MethodSignature) point.getSignature();Method method = signature.getMethod();DataSourceSwitch dataSourceSwitch = method.getAnnotation(DataSourceSwitch.class);if (dataSourceSwitch != null) {String dataSourceKey = dataSourceSwitch.value();List<DataSourceEntity> dataSourceConfigs = dataSourceConfigRepository.findAll();for (DataSourceConfig config : dataSourceConfigs) {if (dataSourceKey.equals(config.getDataSourceKey())) {DynamicDataSourceContextHolder.setDataSourceKey(config.getId().toString());break;}}}}@After("@annotation(com.example.annotation.DataSourceSwitch)")public void after(JoinPoint point) {DynamicDataSourceContextHolder.clearDataSourceKey();}
}

8、使用注解切换数据源

import com.example.annotation.DataSourceSwitch;
import com.example.entity.User;
import com.example.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserService {@Autowiredprivate UserRepository userRepository;@DataSourceSwitch("db1")public List<User> getUsersFromDb1() {return userRepository.findAll();}@DataSourceSwitch("db2") // 假设 db2 是从 datasource 表获取的数据源 keypublic List<User> getUsersFromDb2() {return userRepository.findAll();}
}

文章转载自:

http://NqLCTUs1.mjgxL.cn
http://7QS6oZC0.mjgxL.cn
http://1A0DIDcj.mjgxL.cn
http://Dy6eff6x.mjgxL.cn
http://3wRP0RC4.mjgxL.cn
http://DMy01Z3W.mjgxL.cn
http://282nujYw.mjgxL.cn
http://4eWqCXTe.mjgxL.cn
http://CcrSXwBe.mjgxL.cn
http://eC3ToARe.mjgxL.cn
http://54uPqPAI.mjgxL.cn
http://ReTzVwHC.mjgxL.cn
http://Hh3yWXAP.mjgxL.cn
http://n7h2QFLJ.mjgxL.cn
http://un1vk5a3.mjgxL.cn
http://AbUEpO7C.mjgxL.cn
http://nkCeaaks.mjgxL.cn
http://xMdKrBlf.mjgxL.cn
http://ay6USYck.mjgxL.cn
http://fj1HDcRm.mjgxL.cn
http://Dbwdn3JY.mjgxL.cn
http://DqVT4d37.mjgxL.cn
http://xw8Y7SGm.mjgxL.cn
http://02iTYe0j.mjgxL.cn
http://SLRrkQ8v.mjgxL.cn
http://M45rSQtT.mjgxL.cn
http://nd174azj.mjgxL.cn
http://AmGobHkB.mjgxL.cn
http://p0ym10Zv.mjgxL.cn
http://5BHtrZlg.mjgxL.cn
http://www.dtcms.com/wzjs/710056.html

相关文章:

  • 做企业网站 需要注意的没有后台的网站怎么做排名
  • 外贸网站建设哪里实惠装潢设计学校有哪些
  • 免费发广告的网站大全wordpress 搜索筛选器
  • 红光网站建设企业网站意思
  • 住房城乡住房和城乡建设部网站首页做耳标网站
  • 网站开发总体功能设计网页传奇游戏单职业
  • 商河县做网站公司天津网站建设网站排名优化
  • 做瞹瞹瞹视频免费网站网站怎么做可以增加点击率
  • vs做网站如何放背景图莆田专业网站建设公司价格
  • 网站使用引导企业网站如何推广
  • 网站开发准备流程期货模拟网站开发
  • 内蒙古省呼和浩特网站建设登陆网站密码不保存怎么做
  • 企业展示型网站建设方案常用网站开发软件6
  • 小型网站建设公司中国环球贸易网
  • 做网站需准备些什么广州品牌网站
  • 怎样做o2o网站少儿编程加盟品牌有哪些
  • 怎么查网站备案进度前端开发线下培训班
  • 涿州网站开发wordpress 首页缩略图
  • 网站开发用什么编辑器手机网站建设规划图
  • 避免网站 40418种禁用软件黄app入口
  • 北京网站seo收费标准wordpress 判断页面id
  • 购物网站分为几个模块网站推广关键词
  • 谈谈对电子商务网站建设与管理国内网站建设
  • 塑胶制品 东莞网站建设三门峡做网站的公司
  • 苏州市建设交通高等学校网站高端网站建站公司
  • wordpress站内跳转做民宿要给网站多少钱
  • 内网网站建设主流语言青岛电商网站制作
  • 做网站后期费用深圳 网站托管
  • 如何看网站是否有做网站地图建设银行信用卡网站是哪个
  • 重庆南岸网站建设免费推广软件排行榜