yml配置:
spring:
application:
datasource:
first:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://172.16.3.48:3306/woer_pro1006?characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=UTC&serverTimezone=Asia/Shanghai
username: woer
password: SAb2b@2016
second:
driver-class-name: oracle.jdbc.OracleDriver
jdbc-url: jdbc:oracle:thin:@172.16.3.57:1521:PROD
username: apps
password: appwoer
jpa:
show-sql: true
hibernate:
ddl-auto: none
first-dialect: org.hibernate.dialect.MySQL5InnoDBDialect
second-dialect: org.hibernate.dialect.Oracle9iDialect #这里需要注意,当前数据库方言适用与oracle11,如果版本不一致,建议搜索合适的版本使用
配置两种方言: first-dialect: org.hibernate.dialect.MySQL5InnoDBDialect second-dialect: org.hibernate.dialect.Oracle9iDialect
自定义DataSource配置:
package com.doung.connector.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
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.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef="entityManagerFactoryFirst",
transactionManagerRef="transactionManagerFirst",
basePackages= { "com.doung.connector.dao.first" }) //设置Repository所在位置
public class FirstDatasourceConfig {
@Autowired
@Qualifier("firstDataSource")
private DataSource firstDataSource;
@Autowired
private JpaProperties jpaProperties;
@Autowired
private HibernateProperties hibernateProperties;
@Value("${spring.jpa.hibernate.first-dialect}")
private String firstDialect;// 获取对应的数据库方言
/**
*java.sql.SQLException: 数字溢出,需要设置对应的数据库方言
*
*/
private Map<String, Object> getVendorProperties() {
Map<String,String> map = new HashMap<>();
map.put("hibernate.dialect",firstDialect);// 设置对应的数据库方言
jpaProperties.setProperties(map);
return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
}
@Primary
@Bean(name = "entityManagerFirst")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryFirst(builder).getObject().createEntityManager();
}
@Primary
@Bean(name = "entityManagerFactoryFirst")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryFirst (EntityManagerFactoryBuilder builder) {
return builder.dataSource(firstDataSource)
//设置实体类所在位置.扫描所有带有 @Entity 注解的类
.packages("com.doung.connector.entity.first") //设置entity所在位置
//Spring会将EntityManagerFactory注入到Repository之中.有了 EntityManagerFactory之后,
//Repository就能用它来创建 EntityManager 了,然后 EntityManager 就可以针对数据库执行操作
.persistenceUnit("firstPersistenceUnit")
//设置数据源属性
.properties(getVendorProperties())
.build();
}
@Primary
@Bean(name = "transactionManagerFirst")
public PlatformTransactionManager transactionManagerFirst(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryFirst(builder).getObject());
}
}
设置对应的数据库方言
这篇博客详细介绍了如何在Spring Boot应用中配置两个不同的数据源(MySQL和Oracle),包括各自的驱动类、URL、用户名和密码。同时,针对每个数据源设置了相应的Hibernate方言,以确保与数据库版本兼容。此外,还展示了自定义DataSource配置的方法,包括创建EntityManager和TransactionManager,以及在JPA中启用事务管理和Repository的设置。

被折叠的 条评论
为什么被折叠?



