最近在做一个系统管理项目,需要使用到多数据源,尝试了注解形式和xml形式的多数据源配置,以下是基于注解形式的Mybatis多数据源配置。

1.application.yml 配置文件

database-einstein:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://192.168.128.129:5432/einstein
username: postgres
password: 123456
config-location: mybatis-web-config.xml database-dataplatform:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://192.168.128.129:5432/DataPlatform_20180107
username: postgres
password: postgres
config-location: mybatis-web-config.xml server:
port: 8093

2.针对einstein数据库的配置类(配置多数据源一定要有主数据源,如类中所示,可以在主数据源的sessionFactory上加上注解@Primary)

package cn.antiy.weiqing.configutation;

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.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 javax.sql.DataSource; /**
* @author miaoying
* @date 2/5/18
*/
@Configuration
@MapperScan(basePackages = {"cn.antiy.weiqing.mapper.einstein"}, sqlSessionFactoryRef = "einsteinSqlSessionFactory")
public class MybatisEinsteinDbConfig {
@Bean
@ConfigurationProperties(prefix = "database-einstein")
public DataSource dataSourceEinstein() {
return DataSourceBuilder.create().build();
} @Bean
@Primary
public SqlSessionFactory einsteinSqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSourceEinstein());
return sqlSessionFactoryBean.getObject();
} @Bean
public SqlSessionTemplate einsteinSqlSessionTemplate() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(einsteinSqlSessionFactory());
return template;
}
}

3.针对DataPlatform_20180107数据库的配置类

package cn.antiy.weiqing.configutation;

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.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; /**
* @author miaoying
* @date 2/5/18
*/
@Configuration
@MapperScan(basePackages = {"cn.antiy.weiqing.mapper.dataplatform"}, sqlSessionFactoryRef = "dataPlatformSqlSessionFactory")
public class MybatisDataPlatformDbConfig { @Bean
@ConfigurationProperties(prefix = "database-dataplatform")
public DataSource dataSourceDataPlatform() {
return DataSourceBuilder.create().build();
} @Bean
public SqlSessionFactory dataPlatformSqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSourceDataPlatform());
return sqlSessionFactoryBean.getObject();
} @Bean
public SqlSessionTemplate dataPlatformSqlSessionTemplate() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(dataPlatformSqlSessionFactory());
return template;
}
}

4.Application.java(由于Spring Boot默认单数据源,所以在启动类中需要使用注解去除该默认项,如下所示:@SpringBootApplication(exclude = DataSourceAutoConfiguration.class))

package cn.antiy.weiqing;

import cn.antiy.weiqing.constants.SysConstants;
import cn.antiy.weiqing.utils.SpringContextUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener; @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class Application {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(Application.class);
application.addListeners(new AppStartListener());
application.addListeners();
application.run(args);
}
} class AppStartListener implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
SpringContextUtils.setApplicationContext(event.getApplicationContext());
new SysConstants().init();
}
}

mybatis基于注解形式的多数据源的更多相关文章

  1. 基于注解的Spring多数据源配置和使用(非事务)

    原文:基于注解的Spring多数据源配置和使用 1.创建DynamicDataSource类,继承AbstractRoutingDataSource package com.rps.dataSourc ...

  2. Spring7——开发基于注解形式的spring

    开发基于注解形式的spring SpringIOC容器的2种形式: (1)xml配置文件:applicationContext.xml; 存bean:<bean> 取bean: Appli ...

  3. Spring+Mybatis基于注解整合Redis

    基于这段时间折腾redis遇到了各种问题,想着整理一下.本文主要介绍基于Spring+Mybatis以注解的形式整合Redis.废话少说,进入正题. 首先准备Redis,我下的是Windows版,下载 ...

  4. SpringMvc+Spring+MyBatis 基于注解整合

    最近在给学生们讲Spring+Mybatis整合,根据有的学生反映还是基于注解实现整合便于理解,毕竟在先前的工作中团队里还没有人完全舍弃配置文件进行项目开发,由于这两个原因,我索性参考spring官方 ...

  5. 基于注解的Spring多数据源配置和使用

    前一段时间研究了一下spring多数据源的配置和使用,为了后期从多个数据源拉取数据定时进行数据分析和报表统计做准备.由于之前做过的项目都是单数据源的,没有遇到这种场景,所以也一直没有去了解过如何配置多 ...

  6. Mybatis基于注解开启使用二级缓存

    关于Mybatis的一级缓存和二级缓存的概念以及理解可以参照前面文章的介绍.前文连接:https://www.cnblogs.com/hopeofthevillage/p/11427438.html, ...

  7. 基于注解实现SpringBoot多数据源配置

    1.功能介绍 在实际的开发中,同一个项目中使用多个数据源是很常见的场景.最近在学习的过程中使用注解的方式实现了一个Springboot项目多数据源的功能.具体实现方式如下. 2.在applicatio ...

  8. springboot基于注解动态配置多数据源以及多数据源的事务统一

    参考文档:https://www.cnblogs.com/zhangboyu/p/7622412.html https://blog.csdn.net/qq_34322777/article/deta ...

  9. spring boot整合mybatis基于注解开发以及动态sql的使用

    让我们回忆一下上篇博客中mybatis是怎样发挥它的作用的,主要是三类文件,第一mapper接口,第二xml文件,第三全局配置文件(application.properties),而今天我们就是来简化 ...

随机推荐

  1. C166 8位字节位运算赋值-代码优化

    8位字节位运算赋值优化特记录下: unsigned short func1(){ unsigned short a; return a;} unsigned char func2(){ unsigne ...

  2. centos 6,7 上cgroup资源限制使用举例

    在Centos6,7使用cgroup做内存限制,使用的配置包是libcgroup,具体方案和操作步骤如下. 步骤1:安装配置管理包 Centos 6: # yum install libcgroup ...

  3. Hanlp在java中文分词中的使用介绍

    项目结构 该项目中,.jar和data文件夹和.properties需要从官网/github下载,data文件夹下载 项目配置 修改hanlp.properties: 1 #/Test/src/han ...

  4. 根据数据库结构生成TreeView

    procedure TUIOperate.FillTree(treeview: TTreeView); var findq: TADOQuery; node: TTreeNode; //这个方法是根据 ...

  5. Winform 开发基础分层框架

    Winform 开发基础分层框架:

  6. ALGO-10_蓝桥杯_算法训练_集合运算(排序)

    问题描述 给出两个整数集合A.B,求出他们的交集.并集以及B在A中的余集. 输入格式 第一行为一个整数n,表示集合A中的元素个数. 第二行有n个互不相同的用空格隔开的整数,表示集合A中的元素. 第三行 ...

  7. OpenSSH多路复用Multiplexing配置

    设置 Session Multiplexing 在客户端节点如下配置/etc/ssh/ssh_config 或~/.ssh/config 就可以直接开启 Session Multiplexing 功能 ...

  8. 【转】XP_cmdshell存储过程

    原文地址:http://www.cnblogs.com/love_study/archive/2010/03/02/1676583.html 一 .简介 xp_cmdshell 扩展存储过程将命令字符 ...

  9. 部署redis5.0.3

    一.准备环境 1.下载 # wget http://download.redis.io/releases/redis-5.0.3.tar.gz [root@localhost ~]# wget htt ...

  10. centos7部署openvpn-2.4.6

    一.环境说明 返回主机的IP地址 # ip a | grep "scope global" | awk -F'[ /]+' '{print $3}' | head -1 [root ...