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

springboot + (mysql/pgsql) + jpa 多数据源(不同类数据源)

 配置文件:

spring:datasource:primary:jdbc-url: jdbc:mysql://host:3306/数据库?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNullusername: 账号password: 密码driver-class-name: com.mysql.cj.jdbc.Driversecondary:jdbc-url: jdbc:postgresql://host:3306/数据库username: 账号password: 密码driver-class-name: org.postgresql.Driverjpa:hibernate:primary-dialect: org.hibernate.dialect.MySQL5InnoDBDialectsecondary-dialect: org.hibernate.spatial.dialect.postgis.PostgisDialectddl-auto: updateshow-sql: trueproperties:hibernate:enable_lazy_load_no_trans: true #禁用懒加载temp:use_jdbc_metadata_defaults: falsehbm2ddl: auto: none

datasourceconfig:

import javax.sql.DataSource;import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;@Configuration
public class DataSourceConfig {@Bean(name = "primaryDataSource")@Qualifier("primaryDataSource")@ConfigurationProperties(prefix = "spring.datasource.primary")@Primarypublic DataSource primaryDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "secondaryDataSource")@Qualifier("secondaryDataSource")@ConfigurationProperties(prefix = "spring.datasource.secondary")public DataSource secondaryDataSource() {return DataSourceBuilder.create().build();}}

数据源一:

import java.util.Map;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;/*** 数据源一*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPrimary",transactionManagerRef = "transactionManagerPrimary",basePackages = {"com.yuruo.reco.entity.repository","com.yuruo.reco.entity.mysql.repository"}) //设置Repository所在位置
public class PrimaryConfig {@Autowired@Qualifier("primaryDataSource")private DataSource primaryDataSource;@Autowiredprivate JpaProperties jpaProperties;@Primary@Bean(name = "entityManagerPrimary")public EntityManager entityManager() {return entityManagerFactoryPrimary().getObject().createEntityManager();}@Primary@Bean(name = "entityManagerFactoryPrimary")public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary() {//定义数据库类型和连接方言等主要配置(不写两个数据库方言一样会报错)HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();jpaVendorAdapter.setGenerateDdl(true);jpaVendorAdapter.setDatabase(Database.MYSQL);jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");LocalContainerEntityManagerFactoryBean builder = new LocalContainerEntityManagerFactoryBean();builder.setDataSource(primaryDataSource);builder.setPackagesToScan("com.yuruo.reco.entity","com.yuruo.reco.entity.mysql");builder.setJpaVendorAdapter(jpaVendorAdapter);builder.setPersistenceUnitName("primaryPersistenceUnit");builder.setJpaPropertyMap(getVendorProperties());return builder;}private Map<String, String> getVendorProperties() {return jpaProperties.getProperties();}@Primary@Bean(name = "transactionManagerPrimary")public JpaTransactionManager transactionManagerPrimary() {return new JpaTransactionManager(entityManagerFactoryPrimary().getObject());}
}

数据源二:


import java.util.Map;import javax.persistence.EntityManager;
import javax.sql.DataSource;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;/*** 数据源二*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactorySecondary", transactionManagerRef = "transactionManagerSecondary", basePackages = {"com.yuruo.reco.hologres.repository" }) // 设置Repository所在位置,两个数据库对应的repository和实体类需要不同的路径
public class SecondaryConfig {@Autowired@Qualifier("secondaryDataSource")private DataSource secondaryDataSource;@Bean(name = "entityManagerSecondary")public EntityManager entityManager() {return entityManagerFactorySecondary().getObject().createEntityManager();}/*** 将配置文件中对应的配置信息注册到jpa中进行管理* * @return*/@Bean(name = "entityManagerFactorySecondary")public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary() {HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();jpaVendorAdapter.setGenerateDdl(true);jpaVendorAdapter.setDatabase(Database.POSTGRESQL);jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQLDialect");LocalContainerEntityManagerFactoryBean builder = new LocalContainerEntityManagerFactoryBean();builder.setDataSource(secondaryDataSource);builder.setPackagesToScan("com.yuruo.reco.hologres.entity");builder.setJpaVendorAdapter(jpaVendorAdapter);builder.setPersistenceUnitName("secondaryPersistenceUnit");builder.setJpaPropertyMap(getVendorProperties());return builder;}@Autowiredprivate JpaProperties jpaProperties;private Map<String, String> getVendorProperties() {return jpaProperties.getProperties();}// 用来作为数据库事务回滚的限定词// @Transactional(rollbackFor = OAPMException.class, value =// "transactionManagerSecondary")// 事务管理器@Bean(name = "transactionManagerSecondary")public JpaTransactionManager transactionManagerSecondary() {return new JpaTransactionManager(entityManagerFactorySecondary().getObject());}
}

http://www.lryc.cn/news/105333.html

相关文章:

  • 【Golang】Golang进阶系列教程--Go 语言 context 都能做什么?
  • 画图干货!14种uml图类型及示例
  • 计算机视觉实验:人脸识别系统设计
  • 振弦采集仪完整链条的岩土工程隧道安全监测
  • NLP实战9:Transformer实战-单词预测
  • 使用Vue.js和Rust构建高性能的物联网应用
  • idea调节文字大小、日志颜色、git改动信息
  • 避免大龄程序员边缘化:如何在技术行业中保持竞争力
  • Jenkins工具系列 —— 启动 Jenkins 服务报错
  • 华为数通HCIA-实验环境ensp简介
  • SK5代理与IP代理:网络安全中的爬虫利器
  • 实战:Prometheus+Grafana监控Linux服务器及Springboot项目
  • [用go实现解释器]笔记1-词法分析
  • 在 spark-sql / spark-shell / hive / beeline 中粘贴 sql、程序脚本时的常见错误
  • 关于视频汇聚融合EasyCVR平台多视频播放协议的概述
  • 三星书画联展:三位艺术家开启国风艺术之旅
  • 在腾讯云服务器OpenCLoudOS系统中安装nginx(有图详解)
  • 大数据课程E5——Flume的Selector
  • 在线查看浏览器
  • 谷粒商城第七天-商品服务之分类管理下的分类的拖拽功能的实现
  • 解决单节点es索引yellow
  • Java虚拟机在类加载阶段都做了些什么,才使得我们可以运行Java程序
  • 华为认证 | 学HCIE,想培训需要注意啥?
  • 这所211考数一英二,学硕降分33分,十分罕见!
  • 关于BQ27427的配置问题
  • 试卷还原成空白卷怎么做?分享个简单的方法
  • 查看学校名称中含北京的用户
  • 快速开发人脸识别系统Java版本
  • Reinforcement Learning with Code 【Code 1. Tabular Q-learning】
  • 解决:Uncaught (in promise) SyntaxError: “[object Object]“ is not valid JSON 问题的过程