1. 程式人生 > >mybatis+druid+springboot 註解方式配置多個數據源

mybatis+druid+springboot 註解方式配置多個數據源

pat nts println service ssp bsp manager 打開 iba

1\數據庫配置

#test數據源
spring.datasource.test.url=jdbc:mysql://*:3306/db?useUnicode=true&characterEncoding=utf-8
spring.datasource.test.username=root
spring.datasource.test.password=***
#線上環境
spring.datasource.online.url=jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&characterEncoding=utf-8
spring.datasource.online.username
=root spring.datasource.online.password=*** spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # 下面為連接池的補充設置,應用到上面所有數據源中 # 初始化大小,最小,最大 spring.datasource.initialSize=5 spring.datasource.minIdle=1 spring.datasource.maxActive=50 # 配置獲取連接等待超時的時間 spring.datasource.maxWait
=60000 # 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 spring.datasource.timeBetweenEvictionRunsMillis=60000 # 配置一個連接在池中最小生存的時間,單位是毫秒 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn
=false # 打開PSCache,並且指定每個連接上PSCache的大小 spring.datasource.poolPreparedStatements=false #spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 # 配置監控統計攔截的filters,去掉後監控界面sql無法統計,wall用於防火墻 spring.datasource.filters=stat,wall,log4j # 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄 spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

2\數據源配置

package com.test.config;


import javax.sql.DataSource;
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 com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;


@Configuration
@MapperScan(basePackages = "com.test.mapper.platform", sqlSessionTemplateRef  = "testSqlSessionTemplate")
public class PlatFormDataSourceConfig {

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

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

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

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

}

3、service層

package com.test.service.test;

import com.test.mapper.platform.UserMapper;
import com.test.model.platform.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class test {
    @Autowired
    private  UserMapper userMapper;

    public test() {
    }


    public void testtest(){
        User user=userMapper.selectByPrimaryKey(1);
        System.out.println("///////////////////////////////");
        System.out.println(user.getId());
    }
}

4\

package com.test.controller;

import com.test.service.test.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping(value = "/test")
@Controller
public class TestCase {
    @Autowired
    private test testtest;
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    @ResponseBody
    public String index(){
         testtest.testtest();
         return "hello";
    }
}

mybatis+druid+springboot 註解方式配置多個數據源