spring:
datasource:
admin:
username: root
password: root
jdbc-url: jdbc:mysql://127.0.0.1:3306/admin?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=true&connectTimeout=3000&socketTimeout=10000
driver-class-name: com.mysql.cj.jdbc.Driver
pool-name: HikariPool-admin
minimum-idle: 10
maximum-pool-size: 100
idle-timeout: 600000
max-lifetime: 1800000
connection-timeout: 30000
type: com.zaxxer.hikari.HikariDataSource
connection-test-query: SELECT 1
front:
username: root
password: root
jdbc-url: jdbc:mysql://127.0.0.1:3306/front?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=true&connectTimeout=3000&socketTimeout=10000
driver-class-name: com.mysql.cj.jdbc.Driver
pool-name: HikariPool-front
minimum-idle: 10
maximum-pool-size: 100
idle-timeout: 600000
max-lifetime: 1800000
connection-timeout: 30000
type: com.zaxxer.hikari.HikariDataSource
connection-test-query: SELECT 1
package com.olive.config;
import com.zaxxer.hikari.HikariDataSource;
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.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Configuration
@MapperScan(basePackages = {"com.admin.dao.rdb.mapper"}, sqlSessionFactoryRef = "adminSqlSessionFactory")
public class AdminDataSourceConfig {
@Primary
@Bean("adminDataSource")
@ConfigurationProperties(prefix = "spring.datasource.admin")
public DataSource adminDataSource() {
DataSource dataSource = DataSourceBuilder.create().build();
return dataSource;
}
@Primary
@Bean("adminDataSourceTransactionManager")
public DataSourceTransactionManager adminDataSourceTransactionManager(@Qualifier("adminDataSource") DataSource dataSource) {
HikariDataSource ds = (HikariDataSource)dataSource;
log.info("adminDataSource MaximumPoolSize={}, MinimumIdle={}", ds.getMaximumPoolSize(), ds.getMinimumIdle());
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean("adminSqlSessionFactory")
public SqlSessionFactory adminSqlSessionFactory(@Qualifier("adminDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
Resource[] resources = new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/admin/*Mapper.xml");
sqlSessionFactory.setMapperLocations(resources);
return sqlSessionFactory.getObject();
}
/**
* 构建sqlSession
* @param sqlSessionFactory
* @return
*/
@Bean(name = "adminSqlSessionTemplate")
public SqlSessionTemplate adminSqlSessionTemplate(@Qualifier("adminSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
package com.olive.config;
import com.zaxxer.hikari.HikariDataSource;
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.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j;
/**
* @Desc:
* @Author: huang.jinjin1
* @Date: 2024/5/7
*/
@Slf4j
@Configuration
@MapperScan(basePackages = {"com.front.dao.rdb.mapper"}, sqlSessionFactoryRef = "frontSqlSessionFactory")
public class FrontDataSourceConfig {
@Bean("frontDataSource")
@ConfigurationProperties(prefix = "spring.datasource.front")
public DataSource frontDataSource() {
DataSource dataSource = DataSourceBuilder.create().build();
return dataSource;
}
@Bean("frontDataSourceTransactionManager")
public DataSourceTransactionManager frontDataSourceTransactionManager(@Qualifier("frontDataSource") DataSource dataSource) {
HikariDataSource ds = (HikariDataSource)dataSource;
log.info("frontDataSource MaximumPoolSize={}, MinimumIdle={}", ds.getMaximumPoolSize(), ds.getMinimumIdle());
return new DataSourceTransactionManager(dataSource);
}
@Bean("frontSqlSessionFactory")
public SqlSessionFactory frontSqlSessionFactory(@Qualifier("frontDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
Resource[] resources = new PathMatchingResourcePatternResolver()
.getResources("classpath*:mapper/front/*Mapper.xml");
sqlSessionFactory.setMapperLocations(resources);
return sqlSessionFactory.getObject();
}
/**
* 构建sqlSession
* @param sqlSessionFactory
* @return
*/
@Bean(name = "frontSqlSessionTemplate")
public SqlSessionTemplate frontSqlSessionTemplate(@Qualifier("frontSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}