论坛首页 Java企业应用论坛

spring boot多数据源配置

浏览 1839 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2017-03-12  

spring boot多数据源的配置,spring boot+mybatis配置数据源

第一步pom.xml配置如下:

<pre name="code" class="java">&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;1.5.1.RELEASE&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
 
  &lt;dependencies&gt;
 
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    &lt;/dependency&gt;
   
    &lt;dependency&gt; 
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; 
        &lt;artifactId&gt;spring-boot-starter-aop&lt;/artifactId&gt; 
    &lt;/dependency&gt;
   
    &lt;!-- Mybatis --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.mybatis.spring.boot&lt;/groupId&gt;
&lt;artifactId&gt;mybatis-spring-boot-starter&lt;/artifactId&gt;
&lt;version&gt;1.1.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;!-- MYSQL --&gt;
&lt;dependency&gt;
&lt;groupId&gt;mysql&lt;/groupId&gt;
&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.alibaba&lt;/groupId&gt;
&lt;artifactId&gt;druid&lt;/artifactId&gt;
&lt;version&gt;1.0.20&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.alibaba&lt;/groupId&gt;
&lt;artifactId&gt;fastjson&lt;/artifactId&gt;
&lt;version&gt;1.2.12&lt;/version&gt;
&lt;/dependency&gt;
   
  &lt;/dependencies&gt;</pre>

第二步application.properties配置如下:
<pre name="code" class="java">#datascore one
spring.datasource.primary.url=jdbc:mysql://localhost/testdb1
spring.datasource.primary.username=test1
spring.datasource.primary.password=1234
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver



#datascore two
spring.datasource.secondary.url=jdbc:mysql://localhost/testdb2
spring.datasource.secondary.username=test2
spring.datasource.secondary.password=1234
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver</pre>

第三步主要数据源对象配置如下:
<pre name="code" class="java">/**
* 数据源test1的配置
* @author zx
* @date 2017-03-12
*/
@Configuration
@MapperScan(basePackages = "com.zx.dao.test1", sqlSessionTemplateRef  = "test1SqlSessionTemplate")
public class DataSource1Config {

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

    @Bean(name = "test1SqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);   
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/test1/*Mapper*.xml"));
        return bean.getObject();
    }

    @Bean(name = "test1TransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

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

}</pre>

<pre name="code" class="java">/**
* 数据源test2的配置
* @author zx
* @date 2017-03-12
*/
@Configuration
@MapperScan(basePackages = "com.zx.dao.test2", sqlSessionTemplateRef  = "test2SqlSessionTemplate")
public class DataSource2Config {

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

    @Bean(name = "test2SqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/test2/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "test2TransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "test2SqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}</pre>

具体项目参考github如下:
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics