springboot整合mybatis和springdataJpa多数据源查询数据配置
大家好,因为近期项目中有用到操作多个数据库的时候,我项目中持久层框架是mybatis和sprngdataJpa所以在使用到这两个框架的时候,我这里配置了两套操作多个数据库的配置分别是mybatis和jpa。
1.mybatis配置
1.首先在yml文件中配置数据库连接
spring:
datasource:
test1:
jdbc-url: jdbc:mysql://localhost:3306/test1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf8
driverClassName: com.mysql.cj.jdbc.Driver
username: root
password: xxx
test2:
jdbc-url: jdbc:mysql://localhost:3306/test2?serverTimezone=CTT&useUnicode=true&characterEncoding=utf8
driverClassName: com.mysql.cj.jdbc.Driver
username: root
password: xxx
2.新建mybatis配置类两个
@Configuration
//com.dxl.springboot.mapper.test1表示在这个包下的所以mapper中的sql都使用当前的这个数据源去操作数据库
//sqlSessionFactoryRef :起一个当前这个数据源的SqlSessionFactory名字
@MapperScan(basePackages = "com.alice.springboot.mapper.test1", sqlSessionFactoryRef = "dataSource-1-SqlSessionFactory")
public class MybatisDataSourceConfig1 {
// 将这个对象放入Spring容器中
@Bean(name = "dataSource-1")
// 表示这个数据源是默认数据源
@Primary
// 读取application.properties中的配置参数映射成为一个对象
// prefix表示参数的前缀
@ConfigurationProperties(prefix = "spring.datasource.test1")
public DataSource getDateSource1()
{
return DataSourceBuilder.create().build();
}
@Bean(name = "dataSource-1-SqlSessionFactory")
@Primary
public SqlSessionFactory test1SqlSessionFactory(@Qualifier("dataSource-1") DataSource datasource)
throws Exception
{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
// 设置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/test1/*.xml"));
return bean.getObject();
}
@Configuration
//com.dxl.springboot.mapper.test2表示在这个包下的所以mapper中的sql都使用当前的这个数据源去操作数据库
//sqlSessionFactoryRef :起一个当前这个数据源的SqlSessionFactory名字
@MapperScan(basePackages = "com.alice.springboot.mapper.test2", sqlSessionFactoryRef = "dataSource-2-SqlSessionFactory")
public class MybatisDataSourceConfig2 {
// 将这个对象放入Spring容器中
@Bean(name = "dataSource-2")
// prefix表示参数的前缀
@ConfigurationProperties(prefix = "spring.datasource.test2")
public DataSource getDateSource2()
{
return DataSourceBuilder.create().build();
}
@Bean(name = "dataSource-2-SqlSessionFactory")
public SqlSessionFactory test2SqlSessionFactory(@Qualifier("dataSource-2") DataSource datasource)
throws Exception
{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
// 设置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/test2/*.xml"));
return bean.getObject();
}
2.Jpa配置
1.首先在yml文件中配置数据库连接
spring:
datasource:
test1:
jdbc-url: jdbc:mysql://localhost:3306/test1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf8
driverClassName: com.mysql.cj.jdbc.Driver
username: root
password: xxx
test2:
jdbc-url: jdbc:mysql://localhost:3306/test2?serverTimezone=CTT&useUnicode=true&characterEncoding=utf8
driverClassName: com.mysql.cj.jdbc.Driver
username: root
password: xxx
2.创建两个jap的配置类
basePackages = {“com.springboot.jpa.dxl.dao1”}) 这个配置改成自己的dao包名字
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "JpaDataSourceEntityManagerFactory-1", transactionManagerRef = "JpaDataSourceTransactionManager-1", basePackages = {"com.springboot.jpa.dxl.dao1"})
public class JpaDataSourceConfig1 {
@Primary
@Bean(name = "jpaDataSource-1")
@ConfigurationProperties(prefix = "spring.datasource.test1")
public DataSource customerDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "JpaDataSourceEntityManagerFactory-1")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("jpaDataSource-1") DataSource dataSource) {
//packages("com.springboot.jpa.dxl.pojo") 这个配置改成自己的实体类包路径
//sistenceUnit("persistenceUnit-1") 起个名字不能重复
return builder.dataSource(dataSource).packages("com.springboot.jpa.dxl.pojo").persistenceUnit("persistenceUnit-1").build();
}
@Primary
@Bean(name = "JpaDataSourceTransactionManager-1")
public PlatformTransactionManager customerTransactionManager(@Qualifier("JpaDataSourceTransactionManager-1") EntityManagerFactory customerEntityManagerFactory) {
return new JpaTransactionManager(customerEntityManagerFactory);
}
}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "JpaDataSourceEntityManagerFactory-2", transactionManagerRef = "JpaDataSourceTransactionManager-2", basePackages = {"com.springboot.jpa.dxl.dao2"})
public class JpaDataSourceConfig2 {
@Primary
@Bean(name = "jpaDataSource-2")
@ConfigurationProperties(prefix = "spring.datasource.test2")
public DataSource customerDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "JpaDataSourceEntityManagerFactory-2")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("jpaDataSource-2") DataSource dataSource) {
//packages("com.springboot.jpa.dxl.pojo") 这个配置改成自己的实体类包路径
//sistenceUnit("persistenceUnit-2") 起个名字不能重复
return builder.dataSource(dataSource).packages("com.springboot.jpa.dxl.pojo").persistenceUnit("persistenceUnit-2").build();
}
@Primary
@Bean(name = "JpaDataSourceTransactionManager-1")
public PlatformTransactionManager customerTransactionManager(@Qualifier("JpaDataSourceTransactionManager-2") EntityManagerFactory customerEntityManagerFactory) {
return new JpaTransactionManager(customerEntityManagerFactory);
}
}
好了经过上面的配置项目中就可以使用多数据源进行对数据库的操作了,操作的时候如果想用哪个数据源就把对应的dao中的那个接口注入到代码中就可以使用了。
有什么不懂的小伙伴可以留言,记得点赞哦~