1.首先需要在jdbc的配置文件里面配置两个数据库的连接

数据库1的配置

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://地址:3306/数据库名?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
username=用户名
password=密码

数据里2的配置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://地址:3306/数据库名?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
username=用户名
password=密码

2.需要在application.xml里面配置两个dataSource

实例如下:

连接数据库1的配置

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>

连接数据库2的配置

<bean id="dataSourceCY" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${db.url.product}" />
<property name="username" value="${db.username.product}" />
<property name="password" value="${db.password.product}" />
</bean>

在这里注意,你可以选择一个数据库当做主数据源,那个数据库操作更方便,选择那个数据库为主数据源

配置主数据源如下:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:com/cn/demo/mapper/*Mapper.xml"></property>
</bean>

3.写相关的类进行实现

在一个类里面引入另一个的数据源,如下:

@Resource(name = "dataSourceCY")
private DataSource dataSourceCY;

获取连接

con = dataSourceCY.getConnection();

写sql然后执行

pstmt = con.prepareStatement(sql);
ResultSet result= pstmt.executeQuery();

4.然后就可以完成数据库的交互

用java实现操作两个数据库的数据的更多相关文章

  1. 同时操作两个数据库:报错Illegal attempt to associate a collection with two open sessions

    今天我在一个操作两个数据库的SSH里 同时插入1条数据 报错 Illegal attempt to associate a collection with two open sessions 在这里有 ...

  2. 使用Red Gate Sql Data Compare 数据库同步工具进行SQL Server的两个数据库的数据比较、同步

    Sql Data Compare 是比较两个数据库的数据是否相同.生成同步sql的工具. 这一款工具由Red Gate公司出品,我们熟悉的.NET Reflector就是这个公司推出的,它的SQLTo ...

  3. JAVA对数据库进行操作,实现数据库中数据的插入,查询,更改,删除操作

    (—)通过mysql workbench 创建一个数据库,在这里命名为company,然后建一个tb_employee表 (二)以下是java代码对表tb_employee的操作 1 创建一个Empl ...

  4. 【笔记】.NET开发环境下使用PostgreSQL+Oracle_fdw 实现两个数据库之间数据交互操作(二)

    一 新的可视化工具 因为前文所提到的,看不到外部服务器和外部表的问题,我更换了可视化工具. 好用的新工具PostgreSQL Maestro! 当然如此好用的工具不是免费的,如果想免费使用还请自己去找 ...

  5. 【转】 mysql使用federated引擎实现远程访问数据库(跨网络同时操作两个数据库中的表)

    原文转自:http://www.2cto.com/database/201412/358397.html 问题: 这里假设我需要在IP1上的database1上访问IP2的database数据库内的t ...

  6. java POI导出Excel文件数据库的数据

    在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作.这里我简单实现导出Excel文件. POI jar ...

  7. java下载excel,excel从数据库获取数据

    <!-- poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId&g ...

  8. Python 学习笔记:Python 操作 SQL Server 数据库

    最近要将数据写到数据库里,学习了一下如何用 Python 来操作 SQL Server 数据库. 一.连接数据库: 首先,我们要连接 SQL Server 数据库,需要安装 pymssql 这个第三方 ...

  9. 用Java操作数据库Datetime数据

    Date.Calendar.Timestamp的区别.相互转换与使用 1 Java.util.Date 包含年.月.日.时.分.秒信息. // String转换为Date String dateStr ...

随机推荐

  1. [LeetCode] Generate Random Point in a Circle 生成圆中的随机点

    Given the radius and x-y positions of the center of a circle, write a function randPoint which gener ...

  2. 微信小程序wx.showLoading

    效果图: 代码: wx.showLoading({ title: '加载中...', }) wx.request({ url: '', success(res){}, fail(){}, comple ...

  3. JAVA新的一天

    在2019/03/22/今天里,荣幸成为这个班级的一员,认识了新的小伙伴们,上午由老师大体说了一下java的理念,下午安装了DW编辑器,以及讲解了HTML的基本构造,和标签的使用,即使以前学习过,这次 ...

  4. 解密Redis的持久化和主从复制机制

    Redis持久化 Redis 提供了多种不同级别的持久化方式: RDB 持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot). AOF 持久化记录服务器执 ...

  5. 严重:one or more listeners failed. Full details will be found in the appropriate container log file

    one or more listeners failed. Full details will be found in the appropriate container log file   这句话 ...

  6. HotSpot设计原理与实现:一、初识HotSpot

    一.HotSpot内核模块组成和功能框架 1.HotSpot内核模块图 (1)Prims模块: (2)Service模块: (3)Runtime模块: 二.虚拟机生命周期(JVM初始化过程) 1.虚拟 ...

  7. BIML 101 - ETL数据清洗 系列 - BIML 快速入门教程 - 序

    BIML 101 - BIML 快速入门教程 做大数据的项目,最花时间的就是数据清洗. 没有一个相对可靠的数据,数据分析就是无木之舟,无水之源. 如果你已经进了ETL这个坑,而且预算有限,并且有大量的 ...

  8. 【LeetCode每天一题】Set Matrix Zeroes(设置0矩阵)

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Exampl ...

  9. !!在js中的用法

    var obj = ""; //undefinedconsole.log(!!obj);  //false(强制转换成Boolean类型)

  10. 2000w数据,redis中只存放20w的数据,如何保证redis中的数据都是热点数据

    redis 内存数据集大小上升到一定大小的时候,就会施行数据淘汰策略. redis 提供 6种数据淘汰策略:voltile-lru:从已设置过期时间的数据集(server.db[i].expires) ...