jpa oracle mysql,oracle+jpa和mysql+mybatis的混合多数据源配置例子
最近在學習的時候看到了多數(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的混合多数据源配置例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle 表空间热备份,oracle
- 下一篇: linux cmake编译源码,linu