mybatis多数据源DataSource-mybatis多数据源配置

1、数据源1

package com.zemel.replay.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageHelper;

import tk.mybatis.spring.annotation.MapperScan;

@Configuration
@MapperScan(basePackages = "com.zemel.replay.mapper.stock", sqlSessionTemplateRef = "stockSqlSessionTemplate")
public class DruidConfig {

	/**
	 * 数据源连接数据库
	 * @return
	 */
	@Bean(name = "stockDataSource")
	@ConfigurationProperties(prefix = "db.datasource.stock")
	@Primary // 这个注解是说明此是默认数据源,必须设定默认数据源。(即主数据源)
	public DataSource stockDataSource() {
//		return DataSourceBuilder.create().build();
		return new DruidDataSource();
	}
	
	/**
	 * sqlSessionFactory工厂
	 * @param dataSource
	 * @return
	 * @throws Exception
	 */
	@Bean(name = "stockSqlSessionFactory")
	@Primary
	public SqlSessionFactory stockSqlSessionFactory(@Qualifier("stockDataSource") DataSource dataSource)
			throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		 //支持属性使用驼峰的命名,mapper配置不需要写字段与属性的配置,会自动映射。
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
      //使用jdbc的getGeneratedKeys获取数据库自增主键值
        configuration.setUseGeneratedKeys(true);
      //使用列别名替换列名 select user as User
        configuration.setUseColumnLabel(true);
      //-自动使用驼峰命名属性映射字段   userId    user_id
        configuration.setMapUnderscoreToCamelCase(true);
    	//分页插件设置
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("returnPageInfo", "check");
        properties.setProperty("params", "count=countSql");
        pageHelper.setProperties(properties);
        //添加分页插件
        bean.setPlugins(new Interceptor[]{pageHelper});

        bean.setConfiguration(configuration);
		// xml路径
		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/zemel/replay/mapper/*.xml"));
		return bean.getObject();
	}

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

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

}

2、数据源2

package com.jeagine.config.datasource;

import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.plugin.Interceptor;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageHelper;

@Configuration
@MapperScan(basePackages = "com.jeagine.mapper.yidian", sqlSessionTemplateRef = "yidianSqlSessionTemplate")
public class DataSourceYidianConfig {

	@Bean(name = "yidianDataSource")
	@ConfigurationProperties(prefix = "db.datasource.yidian")
	public DataSource yidianDataSource() {
//		return DataSourceBuilder.create().build();
		return new DruidDataSource();
	}

	@Bean(name = "yidianSqlSessionFactory")
	public SqlSessionFactory yidianSqlSessionFactory(@Qualifier("yidianDataSource") DataSource dataSource)
			throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		// 支持属性使用驼峰的命名,mapper配置不需要写字段与属性的配置,会自动映射。
		org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
		// 使用jdbc的getGeneratedKeys获取数据库自增主键值
		configuration.setUseGeneratedKeys(true);
		// 使用列别名替换列名 select user as User
		configuration.setUseColumnLabel(true);
		// -自动使用驼峰命名属性映射字段 userId user_id
		configuration.setMapUnderscoreToCamelCase(true);
		//分页插件设置
		PageHelper pageHelper = new PageHelper();
		Properties properties = new Properties();
		properties.setProperty("reasonable", "true");
		properties.setProperty("supportMethodsArguments", "true");
		properties.setProperty("returnPageInfo", "check");
		properties.setProperty("params", "count=countSql");
		pageHelper.setProperties(properties);

		//添加分页插件
		bean.setPlugins(new Interceptor[]{pageHelper});

		bean.setConfiguration(configuration);
		bean.setMapperLocations(
				new PathMatchingResourcePatternResolver().getResources("classpath:/mybatis/yidian/*.xml"));
		return bean.getObject();
	}

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

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

3、依赖


			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		
		
			com.alibaba
			druid
		
		
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
		
		
		
		
			mysql
			mysql-connector-java
		
		
		
			org.projectlombok
			lombok
		
		
		
			com.github.pagehelper
			pagehelper
		
		
		
        
            tk.mybatis
            mapper-spring-boot-starter
        

4、application.properties配置

mybatis.mapperLocations=classpath:com/zemel/replay/mapper/*.xml
mybatis.typeAliasesPackage=com.zemel.replay.model

#配置dirudi数据库连接池
db.datasource.stock.driver-class-name=com.mysql.cj.jdbc.Driver
db.datasource.stock.url=jdbc:mysql://localhost:3306/stock?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
db.datasource.stock.username=root
db.datasource.stock.password=123456
#以下为连接池的相关参数配置
## 初始化大小,最小,最大
db.datasource.stock.initialSize=5
db.datasource.stock.maxActive=20
## 配置获取连接等待超时的时间
db.datasource.stock.maxWait=60000
db.datasource.stock.timeBetweenEvictionRunsMillis=60000
db.datasource.stock.minEvictableIdleTimeMillis=300000
db.datasource.stock.validationQuery=SELECT 1 FROM DUAL
db.datasource.stock.testWhileIdle=true
db.datasource.stock.testOnBorrow=false
db.datasource.stock.testOnReturn=false

 

 

你可能感兴趣的:(Java,SpringBoot)