springboot2配置hikari多数据源(postgres版)

1、pom.xml文件引入(其他数据库也可以借鉴,将依赖和properties中的驱动等数据修改测试即可)

postgresql依赖

    org.postgresql
    postgresql

Hikari依赖(这两个依赖根据springboot版本自行加上版本)

    com.zaxxer
    HikariCP

2、数据库配置
application-hikari.properties文件加入数据库信息,需要在application.properties中引入,格式spring.profiles.active=hikari,在两个数据库中随便建立两张表就可以模拟,一个指向主数据库,一个指向从数据库

#数据源类型
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
#主库
spring.datasource.master.username=postgres
spring.datasource.master.password=123456
spring.datasource.master.jdbc-url=jdbc:postgresql://localhost:5432/mytest01?useUnicode=true&characterEncoding=utf8
spring.datasource.master.driver-class-name=org.postgresql.Driver
#从库
spring.datasource.slave.username=postgres
spring.datasource.slave.password=123456
spring.datasource.slave.jdbc-url=jdbc:postgresql://localhost:5432/mytest?useUnicode=true&characterEncoding=utf8
#从库是否启用
spring.datasource.slave.enabled=true
spring.datasource.slave.driver-class-name=org.postgresql.Driver

关于spring.datasource.slave.enabled=true,如果设置为true,会出现两个数据源


image.png

3、代码配置
主库代码

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 javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.hj.project.mapper", sqlSessionTemplateRef = "masterSqlSessionTemplate")
public class MasterConfig {

    @Bean(name = "masterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    @Primary
    public DataSource masterDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "masterSqlSessionFactory")
    @Primary
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //设置master数据源mapper文件路径
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "masterTransactionManager")
    @Primary
    public DataSourceTransactionManager masterTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "masterSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

从库代码


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.autoconfigure.condition.ConditionalOnProperty;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.hj.quartz.mapper", sqlSessionTemplateRef = "slaveSqlSessionTemplate")
//注解@ConditionalOnProperty,这个注解能够控制某个configuration是否生效,这里的配置不要动,修改在properties文件中,往上看
@ConditionalOnProperty(prefix = "spring.datasource.slave", name = "enabled", havingValue = "true")
public class SlaveConfig {

    @Bean(name = "slaveDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.slave")
    
    public DataSource slaveDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "slaveSqlSessionFactory")
    public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //设置slave数据源mapper文件路径
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "slaveTransactionManager")
    public DataSourceTransactionManager slaveTransactionManager(@Qualifier("slaveDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "slaveSqlSessionTemplate")
    public SqlSessionTemplate slaveSqlSessionTemplate(@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

4、启动类设置

//exclude = DataSourceAutoConfiguration.class排除springboot自己的数据源
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
//@MapperScan("com.hj.**.mapper")不需要了,在配置数据源的时候已经设置
public class HjApplication {
    public static void main(String[]  args) {
        SpringApplication.run(HjApplication.class, args);
    }
}

5、事务处理,这里的事务只针对某一个数据源,关于两个数据库一起操作的回滚问题和分布式事务自行百度

你可能感兴趣的:(springboot2配置hikari多数据源(postgres版))