上篇文章咱们讲到了运用代码注入DataSource,这篇文章首要解说一下在项目中配置多DataSource和启用Transactional业务.

将application.yml配置文件参加以下内容

custom-second:
  source:
    driver: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test1
    username: root
    password: 123456

在源码目录下新建config目录, 在该目录下新建CustomSecondDataSource类文件

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edurt.config;
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.core.JdbcTemplate;
import javax.sql.DataSource;
/**
 * CustomSecondDataSource <br/>
 * 描述 : CustomSecondDataSource <br/>
 * 作者 : qianmoQ <br/>
 * 版别 : 1.0 <br/>
 * 创立时间 : 2018-03-29 下午1:29 <br/>
 * 联系作者 : <a href="mailTo:shichengoooo@163.com">qianmoQ</a>
 */
@Configuration
public class CustomSecondDataSource {
    @Bean(name = "secondDatasource")
    @ConfigurationProperties(prefix = "custom-second.source")
    public DataSource secondDataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "secondJdbcTemplate")
    public JdbcTemplate secondJdbcTemplate(@Qualifier("secondDatasource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

留意secondJdbcTemplate在创立时,传入的DataSource必须用@Qualifier(“secondDatasource”)声明,这样,才能运用第二个DataSource。

将CustomDataSource文件中的dataSource和jdbcTemplate符号为首要的, 修改为以下内容

@Bean
@Primary
public DataSource dataSource() {
    return DataSourceBuilder.create()
            .driverClassName(driver)
            .url(url)
            .username(username)
            .password(password).build();
}
@Bean
@Primary
public JdbcTemplate jdbcTemplate() {
    return new JdbcTemplate(dataSource());
}

在repository文件夹下创立SecondRepository用于测验第二个datasource

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edurt.repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
/**
 * SecondRepository <br/>
 * 描述 : SecondRepository <br/>
 * 作者 : qianmoQ <br/>
 * 版别 : 1.0 <br/>
 * 创立时间 : 2018-03-29 下午1:35 <br/>
 * 联系作者 : <a href="mailTo:shichengoooo@163.com">qianmoQ</a>
 */
@Component
public class SecondRepository {
    @Autowired
    @Qualifier("secondJdbcTemplate")
    // 在需要运用第二个JdbcTemplate的地方,咱们注入时需要用@Qualifier("secondJdbcTemplate")标识
    private JdbcTemplate jdbcTemplate;
    public int save(int i) {
        return jdbcTemplate.update("INSERT INTO a(id) VALUES (?)", i);
    }
}

修改ArticleController对外接口类

@Autowired
private SecondRepository secondRepository;
@RequestMapping(value = "test", method = RequestMethod.POST)
int test(@RequestParam Integer id) {
    return secondRepository.save(id);
}

多个DataSource,多个JdbcTemplate时,强烈建议总是运用@Primary把其间某一个Bean标识为“首要的”,运用@Autowired注入时会首要运用被符号为@Primary的Bean

启用Transactional的话咱们只需要将**@EnableTransactionManagement增加到项目的发动类中和将@Transactional**注解增加到咱们要做业务的方法上即可