美文网首页
Spring Boot mysql 数据源配置

Spring Boot mysql 数据源配置

作者: Vissioon | 来源:发表于2017-05-04 11:04 被阅读584次

单个数据源

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * Created by johnzh on
 * 2017/4/27. 10:27
 */

@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "dbSqlSessionFactory")
public class DataSourceConfig {

    static final String PACKAGE = "com.icekredit.merchant.mapper";

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

    @Bean(name = "dbTransactionManager")
    public DataSourceTransactionManager dbTransactionManager(){
        return new DataSourceTransactionManager(dbDataSource());
    }

    @Bean("dbSqlSessionFactory")
    public SqlSessionFactory dbSqlSessionFactory(@Qualifier("dbDataSource") DataSource dbDataSource) throws Exception{
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dbDataSource);
        //查询结果集下划线转驼峰
        sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        return sessionFactory.getObject();
    }

}

多个数据源 (主要增加@Primary 注解,其余数据源配置相同)

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = PrimaryDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "primarySqlSessionFactory")
// 主数据源
public class PrimaryDataSourceConfig {
    static final String PACKAGE = "com.icekredit.configure.dao.ds1";


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

    @Bean(name = "primaryTransactionManager")
    @Primary
    // 事务管理
    public DataSourceTransactionManager primaryTransactionManager() {
        return new DataSourceTransactionManager(primaryDataSource());
    }

    @Bean(name = "primarySqlSessionFactory")
    @Primary
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource primaryDataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(primaryDataSource);
        return sessionFactory.getObject();
    }
}

yml 配置文件格式

#mysql数据库
spring:
  datasource:
    #mysql数据库1
    primary:
      url: jdbc:mysql://******:3306/****?useUnicode=true&autoReconnect=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
      username: ******
      password: ******
      driver-class-name: com.mysql.jdbc.Driver
      # 初始化大小,最小,最大
      initialSize: 5
      minIdle: 5
      maxIdle: 100
      maxActive: 200
      # 获取连接等待超时的时间
      maxWait: 5000
      # 指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除
      testWhileIdle: true
      #获取连接时候验证,会影响性能
      test-on-borrow: true
      validation-query: SELECT 1 FROM DUAL
      # 每30秒运行一次空闲连接回收器(默认-1)
      timeBetweenEvictionRunsMillis: 300000
      # 池中的连接空闲30分钟后被回收(默认30分钟)
      minEvictableIdleTimeMillis: 1800000
      # 在每次空闲连接回收器线程(如果有)运行时检查的连接数量(设置为和maxIdle一样)
      numTestsPerEvictionRun: 100

相关文章

网友评论

      本文标题:Spring Boot mysql 数据源配置

      本文链接:https://www.haomeiwen.com/subject/unurtxtx.html