hikari数据源配置类_springboot hikari mybatis-plus 多数据源配置

该博客详细介绍了如何在SpringBoot项目中配置Hikari数据源,包括两个数据源primary和secondary。配置内容涵盖application.yml文件中的数据源参数设置,如驱动类、URL、用户名、密码等,以及创建对应的配置映射实体DbPropertyConfig。同时,文章展示了如何为每个数据源创建配置类,如PrimaryDataSourceConfigure和SecondaryDataSourceConfigure,这些配置类中包含了数据源的@Bean定义,包括SqlSessionFactory、TransactionManager和SqlSessionTemplate的设置。
摘要由CSDN通过智能技术生成

1、编写配置文件 application.yml

spring:

datasource:

primary:

driver-class-name: org.postgresql.Driver

url: jdbc:postgresql://ip:5432/iotdata

username: postgre

password: postgre123

type: com.zaxxer.hikari.HikariDataSource

maximum-pool-size: 5

minimum-idle: 1

connection-test-query: SELECT 1

pool-name: DatebookHikariCP1

connection-timeout: 30000

idle-timeout: 30000

max-lifetime: 1800000

secondary:

driver-class-name: org.postgresql.Driver

url: jdbc:postgresql://ip:5432/safety

username: postgre

password: postgre123

type: com.zaxxer.hikari.HikariDataSource

maximum-pool-size: 5

minimum-idle: 1

connection-test-query: SELECT 1

pool-name: DatebookHikariCP2

connection-timeout: 30000

idle-timeout: 30000

max-lifetime: 1800000

2、写配置映射实体 DbPropertyConfig

import lombok.Data;

@Data

public class DbPropertyConfig {

private String poolName;

private boolean autoCommit;

private long connectionTimeout;

private long idleTimeout;

private long maxLifetime;

private int maximumPoolSize;

private int minimumIdle;

private String connectionTestQuery;

private String type;

private String password;

private String username;

private String url;

private String driverClassName;

}

3、数据源primary配置

import lombok.Data;

import lombok.EqualsAndHashCode;

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

import org.springframework.context.annotation.Configuration;

/**

* Created by tao.zeng on 2019-03-20.

*/

@Data

@Configuration

@EqualsAndHashCode(callSuper = true)

@ConfigurationProperties(prefix = "spring.datasource.primary")

public class PrimaryDbConfig extends DbPropertyConfig {

}

4、数据源secondary配置

import lombok.Data;

import lombok.EqualsAndHashCode;

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

import org.springframework.context.annotation.Configuration;

/**

* secondary 数据库配置

*/

@Data

@Configuration

@EqualsAndHashCode(callSuper = true)

@ConfigurationProperties(prefix = "spring.datasource.secondary")

public class SecondaryDbConfig extends DbPropertyConfig {

}

5、数据源primary配置类

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;

import com.zaxxer.hikari.HikariDataSource;

import org.apache.commons.lang3.ArrayUtils;

import org.apache.ibatis.session.SqlSessionFactory;

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.core.io.Resource;

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

import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**

* Created by tao.zeng on 2019-03-19.

*/

@Configuration

@MapperScan(basePackages =

{

"com..mapper",

"com.sys.mapper",

}

, sqlSessionTemplateRef = "primarySessionTemplate")

public class PrimaryDataSourceConfigure {

private PrimaryDbConfig mysqlConfig;

public PrimaryDataSourceConfigure(PrimaryDbConfig mysqlConfig) {

this.mysqlConfig = mysqlConfig;

}

@Bean(name = "primaryDataSource")

@ConfigurationProperties("spring.datasource.primary")

public DataSource primaryDataSource() {

DataSource dataSource = DataSourceBuilder.create().build();

HikariDataSource hikariDataSource = null;

if (dataSource instanceof HikariDataSource) {

// 连接池配置

hikariDataSource = (HikariDataSource) dataSource;

hikariDataSource.setPoolName(mysqlConfig.getPoolName());

hikariDataSource.setAutoCommit(mysqlConfig.isAutoCommit());

hikariDataSource.setConnectionTestQuery(mysqlConfig.getConnectionTestQuery());

hikariDataSource.setIdleTimeout(mysqlConfig.getIdleTimeout());

hikariDataSource.setConnectionTimeout(mysqlConfig.getConnectionTimeout());

hikariDataSource.setMaximumPoolSize(mysqlConfig.getMaximumPoolSize());

hikariDataSource.setMaxLifetime(mysqlConfig.getMaxLifetime());

hikariDataSource.setMinimumIdle(mysqlConfig.getMinimumIdle());

hikariDataSource.setJdbcUrl( mysqlConfig.getUrl());

hikariDataSource.setUsername(mysqlConfig.getUsername() );

hikariDataSource.setPassword(mysqlConfig.getPassword());

}

return hikariDataSource == null ? dataSource : hikariDataSource;

}

@Bean(name = "primarySessionFactory")

public SqlSessionFactory primarySessionFactory(@Qualifier("primaryDataSource") DataSource primaryDataSource) throws Exception {

// MyBatis-Plus使用MybatisSqlSessionFactoryBean MyBatis直接使用SqlSessionFactoryBean

MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();

// 给MyBatis-Plus注入数据源

bean.setDataSource(primaryDataSource);

bean.setPlugins(paginationInterceptor()); //添加分页插件

Resource[] resources = new PathMatchingResourcePatternResolver().

getResources("classpath:com/路径/mapping/*.xml");

Resource[] resources2 = new PathMatchingResourcePatternResolver().

getResources("classpath:com/路径/mapping/*.xml");

Resource[] resources1 = ArrayUtils.addAll(resources, resources2);

bean.setMapperLocations(resources1);

return bean.getObject();

}

public PaginationInterceptor paginationInterceptor() {

return new PaginationInterceptor();

}

@Bean(name = "primaryTransactionManager")

public DataSourceTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource primaryDataSource) {

return new DataSourceTransactionManager(primaryDataSource);

}

@Bean(name = "primarySessionTemplate")

public SqlSessionTemplate primarySessionTemplate(@Qualifier("primarySessionFactory") SqlSessionFactory primarySessionFactory) {

return new SqlSessionTemplate(primarySessionFactory);

}

}

6、数据源 secondary配置类

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;

import com.zaxxer.hikari.HikariDataSource;

import org.apache.ibatis.session.SqlSessionFactory;

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.core.io.Resource;

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

import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**

* SecondaryDbConfig

* secondary

*/

@Configuration

@MapperScan(basePackages = "com.safety", sqlSessionTemplateRef = "secondarySessionTemplate")

public class SecondaryDataSourceConfigure {

private SecondaryDbConfig mysqlConfig;

public SecondaryDataSourceConfigure(SecondaryDbConfig verticaConfig) {

this.mysqlConfig = verticaConfig;

}

@Bean(name = "secondaryDataSource")

@ConfigurationProperties("spring.datasource.secondary")

public DataSource secondaryDataSource() {

DataSource dataSource = DataSourceBuilder.create().build();

HikariDataSource hikariDataSource = null;

if (dataSource instanceof HikariDataSource) {

// 连接池配置

hikariDataSource = (HikariDataSource) dataSource;

hikariDataSource.setPoolName(mysqlConfig.getPoolName());

hikariDataSource.setAutoCommit(mysqlConfig.isAutoCommit());

hikariDataSource.setConnectionTestQuery(mysqlConfig.getConnectionTestQuery());

hikariDataSource.setIdleTimeout(mysqlConfig.getIdleTimeout());

hikariDataSource.setConnectionTimeout(mysqlConfig.getConnectionTimeout());

hikariDataSource.setMaximumPoolSize(mysqlConfig.getMaximumPoolSize());

hikariDataSource.setMaxLifetime(mysqlConfig.getMaxLifetime());

hikariDataSource.setMinimumIdle(mysqlConfig.getMinimumIdle());

hikariDataSource.setJdbcUrl( mysqlConfig.getUrl());

hikariDataSource.setUsername(mysqlConfig.getUsername() );

hikariDataSource.setPassword(mysqlConfig.getPassword());

// hikariDataSource.setUsername();

}

return hikariDataSource == null ? dataSource : hikariDataSource;

}

@Bean(name = "secondarySqlSessionFactory")

public SqlSessionFactory secondarySessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {

// MyBatis-Plus使用MybatisSqlSessionFactoryBean MyBatis直接使用SqlSessionFactoryBean

MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();

// 给MyBatis-Plus注入数据源

bean.setDataSource(dataSource);

bean.setPlugins(paginationInterceptor()); //添加分页插件

Resource[] resources = new PathMatchingResourcePatternResolver().

getResources("classpath:com/xml路径/mapper/mapping/*.xml");

bean.setMapperLocations(resources);

return bean.getObject();

}

public PaginationInterceptor paginationInterceptor() {

return new PaginationInterceptor();

}

@Bean(name = "secondaryTransactionManager")

public DataSourceTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) {

return new DataSourceTransactionManager(dataSource);

}

@Bean(name = "secondarySessionTemplate")

public SqlSessionTemplate secondarySessionTemplate(@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {

return new SqlSessionTemplate(sqlSessionFactory);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值