Springboot+mybatis+druid 配置多数据源

2019-11-13 08:21:54来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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);
    }
}

 

第二个数据源代码

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

  UserServiceImpl 

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

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

  StudentServiceImpl

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentMapper studentMapper;

    @Override
    public Student selectByPrimaryKey(int id) {
        return studentMapper.selectByPrimaryKey(id);
    }
}

 

 


原文链接:https://www.cnblogs.com/gekh/p/11847643.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:参与国际化项目需遵循的java命名规范

下一篇:Mybaits 源码解析 (十二)----- Mybatis的事务如何被Spring管理