Created
September 22, 2019 09:42
-
-
Save keepingcoding/a3fa773799166675b7684f6201689b62 to your computer and use it in GitHub Desktop.
多数据源配置
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo.config.ds; | |
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; | |
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.context.annotation.Primary; | |
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; | |
import org.springframework.jdbc.core.JdbcTemplate; | |
import org.springframework.jdbc.datasource.DataSourceTransactionManager; | |
import javax.sql.DataSource; | |
/** | |
* mysql | |
*/ | |
@Configuration | |
@MapperScan(basePackages = "com.example.demo.db.primary.dao", sqlSessionTemplateRef = "primarySqlSessionTemplate") | |
public class PrimaryDataSourceConfig { | |
@Primary | |
@Bean(name = "primaryDataSource") | |
@ConfigurationProperties(prefix = "spring.datasource.primary") | |
public DataSource primaryDataSource() { | |
return DruidDataSourceBuilder.create().build(); | |
} | |
@Primary | |
@Bean(name = "primaryJdbcTemplate") | |
public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource) { | |
return new JdbcTemplate(dataSource); | |
} | |
@Primary | |
@Bean(name = "primarySqlSessionFactory") | |
public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception { | |
SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); | |
bean.setDataSource(dataSource); | |
//bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml")); | |
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/example/demo/db/primary/mapper/**/*.xml")); | |
return bean.getObject(); | |
} | |
@Primary | |
@Bean(name = "primaryTransactionManager") | |
public DataSourceTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) { | |
return new DataSourceTransactionManager(dataSource); | |
} | |
@Primary | |
@Bean(name = "primarySqlSessionTemplate") | |
public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) { | |
return new SqlSessionTemplate(sqlSessionFactory); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment