springBoot+mybatis实现多数据源配置

第一步,pom.xml(因为这个项目测试多个知识点jar包引得比较多,比较乱,根据个人需求删除)




    4.0.0

    xm_demo
    xm_demo
    1.0-SNAPSHOT


    xm_demo Maven Webapp
    
    http://www.example.com
    
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
         
    
    
    
        UTF-8
        UTF-8
        1.8
        1.2.33
        1.0.14
        1.3.1
        3.0.8.RELEASE
        2.2.2
        3.0.2.RELEASE
    

    
        
        
            com.google.code.gson
            gson
            2.6.2
        
        
        
            org.apache.cxf
            cxf-spring-boot-starter-jaxws
            3.1.11
        

        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
        
            net.sourceforge.nekohtml
            nekohtml
            1.9.22
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            com.alibaba
            druid
            1.1.6
        

        
        
            com.oracle
            ojdbc6
            11.2.0.3
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
       
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.0.1
        
       
        
            com.alibaba
            fastjson
            1.2.41
        

        
        
            org.apache.httpcomponents
            httpclient
            4.5
        
        
            org.apache.httpcomponents
            httpmime
            4.5
        
        
            org.apache.httpcomponents
            httpcore
            4.4.1
        

        
        
            net.sf.json-lib
            json-lib
            2.4
            jdk15
        
        
        
            commons-fileupload
            commons-fileupload
            1.3
        
        
        
            com.thoughtworks.xstream
            xstream
            1.4.3
        
        
        
            org.quartz-scheduler
            quartz
            2.2.3
        
        
            org.quartz-scheduler
            quartz-jobs
            2.2.3
        
        
            org.springframework
            spring-context-support
        
        
            org.apache.commons
            commons-lang3
            3.9
        
    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    true
                
            
        
        
        
            
                src/main/resources
                
                    *
                    **/*
                
                false
            
            
                src/main/java
                
                    **/*.properties
                    **/*.xml
                
                false
            
        
    

第二步,数据源配置

package com.xlt.jczb.config;

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.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;


/**
 * @Classname DataSourceConfig1
 * @Description 主数据源配置
 * @Date 2019/11/27 10:00
 * @Created by xm
 */

@Configuration
// 配置主数据源mapper位置
@MapperScan(basePackages = "com.xlt.jczb.mapper", sqlSessionFactoryRef = "test1SqlSessionFactory")
public class DataSourceConfig1 {

    // 将这个对象放入Spring容器中
    @Bean(name = "test1DataSource")


    // 表示这个数据源是默认数据源
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.test1")
    public DataSource getDateSource1() {
        return DataSourceBuilder.create().build();
    }



    @Bean(name = "test1SqlSessionFactory")
    @Primary
    public SqlSessionFactory test1SqlSessionFactory(@Qualifier("test1DataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath:/com/xlt/jczb/mapper/xml/*.xml"));
        return bean.getObject();
    }


    @Bean("test1SqlSessionTemplate")
    @Primary
    public SqlSessionTemplate test1sqlsessiontemplate(
            @Qualifier("test1SqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}
package com.xlt.jczb.config;

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.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @Classname DataSourceConfig2
 * @Description 第二数据源配置
 * @Date 2019/11/27 10:13
 * @Created by xm
 */
@Configuration
@MapperScan(basePackages = "com.xlt.jczb.mapper2", sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DataSourceConfig2 {
    
    
    @Bean(name = "test2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test2")
    public DataSource getDateSource2() {
        return DataSourceBuilder.create().build();
    }

    
    @Bean(name = "test2SqlSessionFactory")
    public SqlSessionFactory test2SqlSessionFactory(@Qualifier("test2DataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:/com/xlt/jczb/mapper2/xml/*.xml"));
        return bean.getObject();
    }

    
    @Bean("test2SqlSessionTemplate")
    public SqlSessionTemplate test2sqlsessiontemplate(
            @Qualifier("test2SqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }
}

第三步,持久层

springBoot+mybatis实现多数据源配置_第1张图片

主数据源

package com.xlt.jczb.mapper;

import com.xlt.jczb.entity.Xtzjgl;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 系统自检管理
 *
 * @date 2019-11-22 10:18:48
 * @author xm
 */
@Service
public interface XtzjglMapper  {


    /**
     * 查询更新
     * @param xtzjgl
     */
    void updateTime (Xtzjgl xtzjgl);

    /**
     * 查询所有
     * @param
     * @return
     */
    List getList();

    /**
     * 查询需要消息推送列表
     * @param
     * @return
     */
    List sendMsgList(Xtzjgl xtzjgl);

}

xtzjglMapper.xml




  
    
    
    
    
    
    
    
    
    
  

  
  
    update xtzjgl set updatetime= #{updateTime}, maxtime=(select max(${cname})  from ${tname} where
    ${zbcName}=#{zbc} ) where tName=#{tname} and cName=#{cname} and zbc=#{zbc}
  

  
  

  
  

第二数据源

package com.xlt.jczb.mapper2;

import com.xlt.jczb.entity.Xtzjgl;
import org.springframework.stereotype.Service;


/**
 * 系统自检管理
 *
 * @date 2019-11-22 10:18:48
 * @author xm
 */
//@Service
public interface XtzjglMapper2 {


    /**
     * 查询更新
     * @param xtzjgl
     */
    void updateTime(Xtzjgl xtzjgl);


    /**
     * 多数据源链接测试
     * @param xtzjgl
     */
    void updateTest(Xtzjgl xtzjgl);
}

xtzjglMapper2.xml 




  
    
    
    
    
    
    
    
    
  

  
  
    update xtzjgl set maxtime=(select max(${cname})  from ${tname} where
    ${zbcName}=#{zbc} ) where tName=#{tname} and cName=#{cname} and zbc=#{zbc}
  

  
  
    update xtzjgl set jwd= #{jwd} where zbc= #{zbc}
  

第四步,业务层

package com.xlt.jczb.service;

import com.xlt.jczb.entity.Xtzjgl;

import java.util.List;


/**
 * 

* 系统自检管理业务实现 *

* * @author 徐明明 * @date 2019-11-22 10:21:22 */ public interface XtzjglService { /** * 查询更新 * @param xtzjgl */ void updateTime (Xtzjgl xtzjgl); /** * 查询所有 * @param * @return */ List getList(); /** * 查询需要消息推送列表 * @param * @return */ List sendMsgList(Xtzjgl xtzjgl); /** * 多数据源链接测试 * @param xtzjgl */ void updateTest(Xtzjgl xtzjgl); }
package com.xlt.jczb.service.impl;

import com.xlt.jczb.entity.Xtzjgl;
import com.xlt.jczb.mapper.XtzjglMapper;
import com.xlt.jczb.mapper2.XtzjglMapper2;
import com.xlt.jczb.service.XtzjglService;
import com.xlt.jczb.util.HttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 

* 系统自检管理 *

* * @author 徐明明 * @date 2019/07/24 */ @Service public class XtzjglServiceImpl implements XtzjglService { private static final Logger log=LoggerFactory.getLogger(XtzjglServiceImpl.class); //消息发送地址 @Value("${sendMsgUrl}") private String sendMsgUrl; //整备场ID @Value("${baseInfo.zbcid}") private String deptid; //系统自检持久层 @Autowired private XtzjglMapper xtzjglMapper; //第二数据源 @Autowired private XtzjglMapper2 xtzjglMapper2; /** * 系统自检数据更新 * @param xtzjgl */ @Override public void updateTime(Xtzjgl xtzjgl) { xtzjglMapper.updateTime(xtzjgl); } @Override public List getList() { return xtzjglMapper.getList(); } @Override public List sendMsgList(Xtzjgl xtzjgl) { return xtzjglMapper.sendMsgList(xtzjgl); } @Override public void updateTest(Xtzjgl xtzjgl) { xtzjglMapper2.updateTest(xtzjgl); } }

第五步,前端控制器

package com.xlt.xfzb.controller;


import com.xlt.xfzb.entity.Xtzjgl;
import com.xlt.xfzb.service.XtzjglService;
import com.xlt.xfzb.service.impl.XtzjglServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * 

* *

* * @author 徐明明 * @date 2019/07/24 */ @Controller @CrossOrigin @RequestMapping("Xtzjgl") public class XtzjglController { @Autowired private XtzjglService xtzjglService; /** * 测试接口 * * @return */ @GetMapping("save") public ResponseEntity save(Xtzjgl xtzjgl) { System.out.println("执行了!!!!!!"); //查询主库数据 List test = xtzjglService.getTest(); //更新到第二数据源 xtzjglService.updateTest(test.get(0)); return new ResponseEntity("成功!!",HttpStatus.OK); } }

第六步,application.yml

server:
  port: 8081 # 应用程序监听的web端口
  max-http-header-size: 4048576

spring:
  #应用名称
  application:
    name: scheduling_xfzb
  #配置文件
  profiles:
    active: dev
  #页面模板
  thymeleaf:
    mode: LEGACYHTML5
    cache: false
    content-type: text/html
    encoding: UTF-8
  #附件上传大小限制
  http:
    multipart:
      maxRequestSize: 100MB
      maxFileSize: 100MB
      max-file-size: 100MB #上传文件的大小限定
      max-request-size: 100MB #上传请求数据的大小限定





application-dev.yml


#多数据源配置
spring:
  datasource:
        test1:
          username: xfzb
          password: xfzb
          url: jdbc:oracle:thin:@127.0.0.1:1521:swwgorcl
          driver-class-name: oracle.jdbc.driver.OracleDriver
          druid:
            initial-size: 5
        test2:
          username: xazb
          password: xazb
          url: jdbc:oracle:thin:@127.0.0.1:1521:swwgorcl
          driver-class-name: oracle.jdbc.driver.OracleDriver
          druid:
            initial-size: 6



配置完毕,想配置更多数据源,都是相同套路,去测试一下呗!

你可能感兴趣的:(后端,多数据源,springBoot,mybatis)