Springboot+mybatis+druid 配置多數據源

項目結構

application.yml配置文件
spring:
  application:
    name: service
  datasource:
    primary:
      jdbc-url: jdbc:oracle:thin:@127.0.0.1:1521:ORCL
      username: gkh
      password: 123456
      driver-class-name: oracle.jdbc.driver.OracleDriver
      type: com.alibaba.druid.pool.DruidDataSource  #使用druid鏈接池
      #url: jdbc:oracle:thin:@127.0.0.1:1521:ORCL
      #type: oracle.jdbc.pool.OracleDataSource
    secondary:
      jdbc-url: jdbc:oracle:thin:@127.0.0.1:1521:ORCL
      username: gkh
      password: 123456
      driver-class-name: oracle.jdbc.driver.OracleDriver
      type: com.alibaba.druid.pool.DruidDataSource #使用druid鏈接池
主數據源配置代碼
package com.gkh.springboot.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.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @Primary:指定爲默認數據源,沒有該註解會報錯,系統找不到默認數據源
 * @MapperScan:掃描指定包的mapper做爲對應數據源,建議每一個數據源都用不一樣的包區分
 * @Qualifier:與@Autowired相似,用做區分若是存在多個實現類要指定要注入哪一個 參數爲指定Bean的name
 */

@Configuration
@MapperScan(basePackages = "com.gkh.springboot.mapper.primary", sqlSessionFactoryRef = "primarySqlSessionFactory")
public class DataSource1Config {

    /**
     * 生成數據源,@Primary註解聲明爲默認數據源
     * @return
     */
    @Bean(name="primaryDataSoure")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource primaryDataSource(){
        return DataSourceBuilder.create().build();
    }

    /**
     * 建立sqlSessionFactory
     * @param datasource
     * @return
     * @throws Exception
     */
    @Bean(name = "primarySqlSessionFactory")
    @Primary
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSoure") DataSource datasource)
            throws Exception{
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        return bean.getObject();
    }

    /**
     * 配置事務管理
     * @param datasource
     * @return
     */
    @Bean(name = "primaryTransactionManager")
    @Primary
    public DataSourceTransactionManager primaryTransactionManager(@Qualifier("primaryDataSoure") DataSource datasource){
        return new DataSourceTransactionManager(datasource);
    }

    @Bean(name = "primarySqlSessionTemplate")
    @Primary
    public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

 

第二個數據源代碼java

package com.gkh.springboot.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.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.gkh.springboot.mapper.secondary", sqlSessionFactoryRef = "secondSqlSessionFactory")
public class DataSource2Config {

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

    @Bean(name = "secondSqlSessionFactory")
    public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDatasource") DataSource dataSource)
            throws Exception{
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean(name = "secondTransactionManager")
    public DataSourceTransactionManager sourceTransactionManager(@Qualifier("secondDatasource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "secondSqlSessionTemplate")
    public SqlSessionTemplate secondSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
Controller:

 

  UserController
@Controller
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 經過主鍵id查詢
     * @param id
     * @return
     */
    @GetMapping(value = "/getUser")
    @ResponseBody
    public User getUserById(@RequestParam("id") Long id){
        return this.userService.getUserById(id);
    }
}    

  

  StudentController

 

 

@Controller
@RequestMapping(value = "/student")
public class StudentController {

    @Autowired
    private  StudentService studentService;

    @GetMapping(value = "/getStudent/{id}")
    @ResponseBody
    public Student getStudent(@PathVariable int id){
        return studentService.selectByPrimaryKey(id);
    }
}

 

service

  UserServiceImplspring

  

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserById(Long id) {
        return this.userMapper.selectByPrimaryKey(id);
    }
}

  StudentServiceImplsql

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentMapper studentMapper;

    @Override
    public Student selectByPrimaryKey(int id) {
        return studentMapper.selectByPrimaryKey(id);
    }
}
相關文章
相關標籤/搜索