mybatis 动态数据源 :

springboot项目yml配置:

spring:
application:
name:  name #
autoconfigure:
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
datasource:
maintype: oracle#用来获取主数据源是哪种类型
dynamic:
primary: master
strict: false
datasource:
master:
driver-class-name: org.postgresql.Driver
url: jdbc:orcl://ip:host/postgres?searchpath=xxxx&stringtype=xxx&tSchema=xxx
username: username
password: password
p6spy: true

后台代码工具类:

package com.xxx;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.BasicDataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.zz.project.provide.jk.mapper.ZzDataSourceDto;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.util.Set;
/**
* @author
* @date 2020/9/4 9:43
*/
@Component
@AllArgsConstructor
public class MoreDataSourceUtils {private final DataSource dataSource;
private final BasicDataSourceCreator basicDataSourceCreator;
public void addDataSourceToDS(ZzDataSourceDto dto){
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
Set<String> dataSourceIds = ds.getCurrentDataSources().keySet();
if(!dataSourceIds.contains(dto.getPollName())){
DataSourceProperty dataSourceProperty = new DataSourceProperty();
BeanUtils.copyProperties(dto, dataSourceProperty);
DataSource dataSource = basicDataSourceCreator.createDataSource(dataSourceProperty);
ds.addDataSource(dto.getPollName(), dataSource);
}
}
public void deleteDataSourceToDS(String dataSourceId){
//dataSourceId就是存在数据源列表中的key
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
ds.removeDataSource(dataSourceId);
}
/**
* 数据源id
* @param datasourceId
* @return 返回添加到DS中的数据源的类型
*/
public String addDataSourceToDSByDatasourceId(String datasourceId,String mainDataSourceType){
//主数据源
if(datasourceId.equals("0") ||"master".equals(datasourceId)){
return mainDataSourceType;
}else{
//0.先判断当前数据源中是否有该数据源id 若没有则继续 若有返回该数据源类型
//1.根据数据源id获取数据源信息
//2.组装成ZzDataSourceDto dto
//3.添加到DS addDataSourceToDS(dto)
//返回数据源类型
return mainDataSourceType;
}
}
}

动态数据库连接:

public void test(){
ZzDataSourceDto dto = new ZzDataSourceDto();
dto.setUsername(sjk.getUsername());
dto.setPassword(sjk.getPassword());
dto.setPollName(sjk.getSid().toString());
dto.setUrl(sjk.getWzslmc());
dto.setDriverClassName("org.postgresql.Driver");
setDataSource(dto);
}public void setDataSource(ZzDataSourceDto dto){
moreDataSourceUtils.addDataSourceToDS(dto);
return ;
}
public void removeDataSource(String dataSourceId) {
moreDataSourceUtils.deleteDataSourceToDS(dataSourceId);
return ;
}

就这。。

查询sql语句有:

package com.zz.project.provide.jk.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zz.project.provide.jk.mapper.MoreDataSourceMapper;
import com.zz.project.provide.jk.service.MoreDataSourceService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
* @author
* @date 2020/9/4 11:55
*/
@Service
public class MoreDataSourceServiceImpl implements MoreDataSourceService {
@Resource
public MoreDataSourceMapper moreDataSourceMapper;
@DS("#dataSourceId")
@Override
public List<Map<String, Object>> selectList(Page<Map<String, Object>> page, String tableName
, String querySql,String dataSourceId) {
return moreDataSourceMapper.selectList(page,tableName,querySql);
}
@DS("#dataSourceId")
@Override
public Integer insertData(String tableName, String columnNames, String columnValus,String dataSourceId) {
return moreDataSourceMapper.insertData(tableName,columnNames,columnValus);
}
@DS("#dataSourceId")
@Override
public Integer updateData(String tableName, String columnsSql, String id,String dataSourceId) {
return moreDataSourceMapper.updateData(tableName,columnsSql,id);
}
@DS("#dataSourceId")
@Override
public Integer deleteData(String tableName, String id,String dataSourceId) {
return moreDataSourceMapper.deleteData(tableName,id);
}
@DS("#dataSourceId")
@Override
public Integer executeSQL(String sqlStr, String dataSourceId)
{
return moreDataSourceMapper.executeSQL(sqlStr);
}
@DS("#dataSourceId")
@Override
public List<Map<String,Object>> selectAnysql( String querySql, String dataSourceId){
return moreDataSourceMapper.selectAnysql(querySql) ;
}
@DS("#dataSourceId")
@Override
public Map<String,Object> selectAnySingle( String querySql, String dataSourceId){
return moreDataSourceMapper.selectAnySingle(querySql) ;
}
@DS("#dataSourceId")
@Override
public Object selectOne( String querySql, String dataSourceId){
return moreDataSourceMapper.selectOne(querySql) ;
}}

mapper.xml 为:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.project.provide.jk.mapper.MoreDataSourceMapper">
<select id="selectList" parameterType="java.lang.String" resultType="java.util.Map">
select * from ${tableName} where 1=1
<if test="querySql != null and querySql !=''" >
and ${querySql}
</if>
</select>
<insert id="insertData" parameterType="java.lang.String">
insert into ${tableName} (${columnNames}) values (${columnValus})
</insert>
<update id="updateData" parameterType="java.lang.String">
update ${tableName} set ${columnsSql} where id=#{id}
</update>
<delete id="deleteData" parameterType="java.lang.String">
delete ${tableName} where id=#{id}
</delete>
<update id="executeSQL" parameterType="java.lang.String">
${sqlStr}
</update>
<select id="selectAnysql" resultType="java.util.Map">
${querySql}
</select>
<select id="selectAnySingle" resultType="java.util.Map">
${querySql}
</select>
<select id="selectOne" resultType="java.lang.Object">
${querySql}
</select>
</mapper>

为了记录而写,叙述不详请见谅。

mybatis 实现动态数据源连接相关推荐

  1. SpringBoot+MyBatis(动态数据源/分布式事务XA(Atomikos))

    快速集成工具,欢迎打脸 说明 适用环境:SpringBoot / MyBatis / Atomickos  特性: 多数据源,动态切换 多数据源,XA分布式事务支持(需引入Atomickos) 仅支持 ...

  2. mysql mybatis多库查询_SpringBoot2.0.3整合Mybatis添加动态数据源实现多库查询(DynamicDataSource)...

    最近由于项目使用了spring boot 2.0.3版本,业务从多个数据查询,必须支持动态数据源,由于2.0.3的版本与之前的版本有了较大的改动其实现上有些不同,再采坑以后在此记录 1.需要Java类 ...

  3. SpringBoot+Mybatis 实现动态数据源切换方案

    背景 最近让我做一个大数据的系统,分析了一下,麻烦的地方就是多数据源切换抽取数据.考虑到可以跨服务器跨数据库抽数,再整理数据,就配置了这个动态数据源的解决方案.在此分享给大家. 实现方案 数据库配置文 ...

  4. Spring Boot + Mybatis 实现动态数据源

    动态数据源 在很多具体应用场景的时候,我们需要用到动态数据源的情况,比如多租户的场景,系统登录时需要根据用户信息切换到用户对应的数据库.又比如业务A要访问A数据库,业务B要访问B数据库等,都可以使用动 ...

  5. mysql动态配置数据源_Spring整合Mybatis实现动态数据源切换教程配置

    一.摘要 这篇文章将介绍Spring整合Mybatis 如何完成SqlSessionFactory的动态切换的.并且会简单的介绍下MyBatis整合Spring中的官方的相关代码. Spring整合M ...

  6. mybatis默认的数据源连接池(PooledDataSource和UnPooledDataSource)

    一般情况下我们操作数据库都是通过connection,但是频繁创建和删除connection会严重影响效率,因此在这种情况下我们一般会用到连接池,因为项目中用到的是mybatis,所以了解一下myba ...

  7. spring c3p0 mysql_spring boot整合mybatis使用c3p0数据源连接mysql

    刚刚接触springboot,对很多东西都不熟悉,例如,它的注解方式,他的配置方式等:听说它很牛逼,所以就尝试着去学习.在基本熟悉springboot的第一个程序之后.想到当时spring整合myba ...

  8. java多个数据库数据进行访问_通过Spring Boot配置动态数据源访问多个数据库的实现代码...

    之前写过一篇博客<Spring+Mybatis+Mysql搭建分布式数据库访问框架>描述如何通过Spring+Mybatis配置动态数据源访问多个数据库.但是之前的方案有一些限制(原博客中 ...

  9. mysql 多数据源访问_通过Spring Boot配置动态数据源访问多个数据库的实现代码

    之前写过一篇博客<Spring+Mybatis+Mysql搭建分布式数据库访问框架>描述如何通过Spring+Mybatis配置动态数据源访问多个数据库.但是之前的方案有一些限制(原博客中 ...

最新文章

  1. 深入理解 Android Https
  2. 2019蓝桥杯省赛---java---C---9(等差数列)
  3. 中国信通院金融科技负责人韩涵:大数据是生产资料的变革,区块链是生产关系的变革...
  4. 一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient)——收流篇:(四)example代码解析...
  5. javabean更新到mysql_javabean连mysql 数据库更新问题
  6. HTML_简单JQ的AJAX响应式交互
  7. python title函数意义_Python 字符串首字母大写-Python设置字符串首字母大写-python title()作用-python title函数-嗨客网...
  8. 游戏服务器的思考之三:谈谈MVC
  9. vue $slot基本用法
  10. 极大似然估计学习笔记
  11. 小型pascal编译器C语言代码,Pascal简单编译器
  12. C#CAD二次开发 非模态对话框切换窗口焦点
  13. 神舟微型计算机登录密码忘记,win10开机密码忘记按f2(win10忘记密码强制重置)
  14. C++Primer 5th_Exercise 习题答案
  15. Pyinstaller 打包exe运行时找不到源码,函数 错误 OSError: could not get source code
  16. 医疗物联网七大应用场景案例解析
  17. 用python打开文件然后写个欢迎代码
  18. python内存地址不变,关于python内存地址问题
  19. 【粒子动画tsParticles】
  20. 4月11日 星期六

热门文章

  1. DROID-SLAM: 用于单目双目RGBD相机的深度视觉SLAM
  2. matlab simca,SIMCA软件|SIMCA-多元数据分析软件
  3. 野蛮时代SLG数据分析报告
  4. 【故事】程序员到底是干什么的
  5. [嵌入式开发模块]SHT30/20 温湿度传感器 驱动模块
  6. 解决vue google无状态播放音频文件
  7. QC —什么是量子计算机?
  8. New UWP Community Toolkit - DeveloperTools
  9. android红外遥控驱动
  10. C++读取通达信shm.tnf文件股票代码/名称