日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

jpa oracle mysql,oracle+jpa和mysql+mybatis的混合多数据源配置例子

發(fā)布時間:2025/3/21 数据库 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jpa oracle mysql,oracle+jpa和mysql+mybatis的混合多数据源配置例子 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

最近在學習的時候看到了多數(shù)據(jù)源這一章,回想以前做的所有項目用到了MySQL,Oracle,Mybatis,SpringJpa等等。

這里不評論各自的優(yōu)缺點,也不推薦該使用哪一種。大部分的文章都是單一技術配置多個庫,這幾樣混合使用的比較少。

經(jīng)過多次嘗試和資料參考后終于能出來結果了,這里記錄一下。

主體技術框架如下:

spring boot 2.0,thymeleaf,Oracle 12c + spring jpa,MySQL + mybatis。

主要的配置文件

1.MySqlDataSourceConfig

package com.example.demo.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;

import org.mybatis.spring.SqlSessionFactoryBean;

import org.mybatis.spring.SqlSessionTemplate;

import org.mybatis.spring.annotation.MapperScan;

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;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import org.springframework.transaction.annotation.EnableTransactionManagement;

/**

* @author qsky on 2018/7/2

*/

@Configuration

@MapperScan(basePackages = "com.example.demo.mapper", sqlSessionFactoryRef = "mySqlSessionFactory")

@EnableTransactionManagement

public class MySqlDataSourceConfig {

@Bean(name = "mySqlDataSource")

@Qualifier("mySqlDataSource")

@Primary

@ConfigurationProperties(prefix = "spring.datasource.hikari.mysql")

public DataSource mySqlDataSource() {

return DataSourceBuilder.create().build();

}

@Bean

@Primary

public DataSourceTransactionManager masterManager() {

return new DataSourceTransactionManager(mySqlDataSource());

}

@Bean

@Primary

public SqlSessionFactory mySqlSessionFactory() throws Exception {

final SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

bean.setDataSource(mySqlDataSource());

bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/*.xml"));

return bean.getObject();

}

@Bean

@Primary

public SqlSessionTemplate mySqlSessionTemplate() throws Exception {

return new SqlSessionTemplate(mySqlSessionFactory());

}

}

1.1 mapper包掃描路徑:com.example.demo.mapper

1.2 mapper xml文件路徑:classpath:mybatis/mapper/*.xml

2.OracleDataSourceConfig

package com.example.demo.config;

import java.util.Map;

import javax.annotation.Resource;

import javax.persistence.EntityManager;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;

import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.boot.jdbc.DataSourceBuilder;

import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import org.springframework.orm.jpa.JpaTransactionManager;

import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;

import org.springframework.transaction.PlatformTransactionManager;

import org.springframework.transaction.annotation.EnableTransactionManagement;

/**

* @author qsky on 2018/7/2

*/

@Configuration

@EnableTransactionManagement

@EnableJpaRepositories(entityManagerFactoryRef = "oracleEntityManagerFactory", transactionManagerRef = "oracleTransactionManager", basePackages = {

"com.example.demo.repository"})

public class OracleDataSourceConfig {

@Bean(name = "oracleDataSource")

@Qualifier("oracleDataSource")

@ConfigurationProperties(prefix = "spring.datasource.hikari.oracle")

public DataSource oracleDataSource() {

return DataSourceBuilder.create().build();

}

@Resource

private JpaProperties jpaProperties;

@Bean(name = "entityManager")

public EntityManager entityManager(EntityManagerFactoryBuilder builder) {

return oracleEntityManagerFactory(builder).getObject().createEntityManager();

}

/**

* 設置實體類所在位置

*/

@Bean(name = "oracleEntityManagerFactory")

public LocalContainerEntityManagerFactoryBean oracleEntityManagerFactory(EntityManagerFactoryBuilder builder) {

return builder.dataSource(oracleDataSource())

.packages("com.example.demo.entity")

.persistenceUnit("oraclePersistenceUnit")

.properties(getProperties())

.build();

}

private Map getProperties() {

return jpaProperties.getHibernateProperties(new HibernateSettings());

}

@Bean(name = "oracleTransactionManager")

public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {

return new JpaTransactionManager(oracleEntityManagerFactory(builder).getObject());

}

}

2.1?repository包路徑:com.example.demo.repository

2.2 實體類包路徑:com.example.demo.entity

3.application.properties

spring.thymeleaf.prefix=classpath:/templates/

spring.thymeleaf.suffix=.html

mybatis.type-aliases-package=com.example.demo.model

mybatis.config-locations=classpath:mybatis/mybatis-config.xml

mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

spring.datasource.hikari.mysql.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.hikari.mysql.jdbc-url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8

spring.datasource.hikari.mysql.username=root

spring.datasource.hikari.mysql.password=123456

spring.datasource.hikari.oracle.jdbc-url=jdbc:oracle:thin:@localhost:1521:orcl

spring.datasource.hikari.oracle.username=c##dev

spring.datasource.hikari.oracle.password=123456

spring.jpa.show-sql=true

#數(shù)據(jù)庫方言設置,避免出現(xiàn)limit查詢給oracle

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle12cDialect

注意配置前綴要帶上hikari

編寫repository和mapper時無需考慮使用哪個數(shù)據(jù)源,像正常一樣使用,因為上面在配置時已經(jīng)分包了。

具體demo代碼見GitHub:混合多數(shù)據(jù)源demo

《新程序員》:云原生和全面數(shù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的jpa oracle mysql,oracle+jpa和mysql+mybatis的混合多数据源配置例子的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。