欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Springboot+mybatis+druid 配置多数据源

程序员文章站 2022-06-13 08:10:18
项目结构 application.yml配置文件 spring: application: name: service datasource: primary: jdbc-url: jdbc:oracle:thin:@127.0.0.1:1521:ORCL username: gkh passwor ......
项目结构

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