springboot 數據庫連接 多數據源配置(最終篇2,之前的配置)

一、三個這樣的配置,相當於三個bean

package com.huya.cw.config.mysql;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.beans.PropertyVetoException;

@Configuration
@MapperScan("com.hy.oa.cw")
public class AnchorMysqlconfiguration {


    @Value("${oa.jdbc.driverClassName}")
    private String jdbcDriver;

    @Value("${oa.anchor.game.jdbc.url}")
    private String jdbcUrl;

    @Value("${oa.anchor.game.jdbc.username}")
    private String user;

    @Value("${oa.anchor.game.jdbc.password}")
    private String password;

    @Bean(name = "C3P0DataSourceFatherForAnchorGames")
    public ComboPooledDataSource createDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        //dirver
        dataSource.setDriverClass(jdbcDriver);
        //url
        dataSource.setJdbcUrl(jdbcUrl);
        //username
        dataSource.setUser(user);
        //password
        dataSource.setPassword(password);

        //在關閉連接之後不會自動的Commit
        dataSource.setAutoCommitOnClose(false);

        //  <!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數。Default: 3 -->
        dataSource.setAcquireIncrement(5);

        // <!--定義在從數據庫獲取新連接失敗後重復嘗試的次數。Default: 30 -->

        dataSource.setAcquireRetryAttempts(30);

        //	<!--兩次連接中間隔時間,單位毫秒。Default: 1000 -->
        dataSource.setAcquireRetryDelay(1000);

        // <!-- 當連接池用完時客戶端調用getConnection()後等待獲取新連接的時間,超時後將拋出SQLException
        dataSource.setCheckoutTimeout(10000);

        dataSource.setMaxStatements(0);
        //<!--初始化時獲取三個連接,取值應在minPoolSize與maxPoolSize之間。 Default: 3 -->
        dataSource.setInitialPoolSize(10);

        dataSource.setMinPoolSize(5);

        //   	<!--連接池中保留的最大連接數。Default: 15 -->
        dataSource.setMaxPoolSize(200);
        //	<!--最大空閒時間,60秒內未使用則連接被丟棄。若爲0則永不丟棄。Default: 0 -->
        dataSource.setMaxIdleTime(60);
        //<!--How long to hang on to excess unused connections after traffic spike -->
        dataSource.setMaxIdleTimeExcessConnections(600);

        return dataSource;
    }
    // TOdo
//
//    @Bean(name = "jdbcTemplate")
//    public JdbcTemplate jdbcTemplate(ComboPooledDataSource dataSourcesecond){
//        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSourcesecond);
//        return  jdbcTemplate;
//
//    }

}

二、配置文件


#Mysql FatherMysql

oa.jdbc.driverClassName=com.mysql.jdbc.Driver
oa.jdbc.url=jdbc:mysql://10.64.102.29/hy_oa?useUnicode=true&rewriteBatchedStatements=true
oa.jdbc.username=hyoa
oa.jdbc.password=123456789

#dataSourceForAnchorGames
oa.anchor.game.jdbc.url = jdbc:mysql://127.0.0.1:3306/mysql1?useUnicode=true&characterEncoding=utf8&useSSL=false
oa.anchor.game.jdbc.username = root
oa.anchor.game.jdbc.password = root

#原項目暫時沒有這個
oa.nimo.jdbc.url = jdbc:mysql://127.0.0.1:3306/mysql2?useUnicode=true&characterEncoding=utf8&useSSL=false
oa.nimo.jdbc.username = root
oa.nimo.jdbc.password = root

三、測試,bean  一 一對應數據庫的name


@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApplicationBoot.class)
public class AppTest {


        @Autowired
    @Qualifier("C3P0DataSourceFather")
        private DataSource C3P0DataSourceFather;


    @Test
    public void testMapper(){
//        System.out.println(employeeMapper);
//        System.out.println("測試與本機數據庫連接");
//        List<Employee> employeeList = employeeMapper.selectAll();
//        for (Employee  e : employeeList) {
//            System.out.println(e);
//        }

        System.out.println("測試與oa數據庫連接");

    String sql = "select count(id) from employee ";
        Integer num = new JdbcTemplate(C3P0DataSourceFather).queryForObject(sql, Integer.class);
        System.out.println(num);
        System.out.println("ok" );

    }



    @Autowired
    @Qualifier("C3P0DataSourceFatherForAnchorGames")
    private   DataSource c3P0DataSourceFatherForAnchorGames;
    @Test
    public void test3()  {
        System.out.println("測試與oa數據庫連接");
        String sql = "select count(id) from employee ";
        Integer num = new JdbcTemplate(c3P0DataSourceFatherForAnchorGames).queryForObject(sql, Integer.class);
        System.out.println(num);
    }
    @Autowired
    @Qualifier("C3P0DataSourceFatherForNimo")
    private   DataSource C3P0DataSourceFatherForNimo;

    @Test
    public void test4()  {
        System.out.println("測試與oa數據庫連接");
        String sql = "select count(id) from employee ";
        Integer num = new JdbcTemplate(C3P0DataSourceFatherForNimo).queryForObject(sql, Integer.class);
        System.out.println(num);
    }


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章