1、创建好数据库的配置文件

mysql.url=jdbc:mysql://***/***?useUnicode=true&characterEncoding=UTF-8
mysql.username=***
mysql.password=***
mysql.driverClassName=com.mysql.jdbc.Driver dws.url=jdbc:postgresql://***/***
dws.username=***
dws.password=***
dws.driverClassName=org.postgresql.Driver

2、applicationContext.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--开启自动扫描-->
<context:component-scan base-package="com.huawei.test"/> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/jdbc.properties</value>
</list>
</property>
</bean>
<!--现在可以替换为如下方式-->
<!--使用逗号分隔多个值-->
<!--<context:property-placeholder location="classpath:config/jdbc.properties"/>--> <!--配置数据源-->
<bean id="dataSource_mysql" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
<property name="driverClassName" value="${mysql.driverClassName}"/>
</bean>
<bean id="sqlSessionFactory_mysql" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource_mysql"/>
<property name="mapperLocations" value="classpath:mybatis/mysql/*.xml"/>
</bean>
<bean id="mapperScannerConfigurer_mysql" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.huawei.test.dao.mysql"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_mysql"/>
</bean>
<!--数据库事务控制-->
<bean id="transactionManager_mysql" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource_mysql"/>
</bean> <bean id="dataSource_dws" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="url" value="${dws.url}"/>
<property name="username" value="${dws.username}"/>
<property name="password" value="${dws.password}"/>
<property name="driverClassName" value="${dws.driverClassName}" />
</bean>
<bean id="sqlSessionFactory_dws" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource_dws"/>
<property name="mapperLocations" value="classpath:mybatis/dws/*.xml"/>
</bean>
<bean id="mapperScannerConfigurer_dws" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.huawei.test.dao.dws"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_dws"/>
</bean>
<bean id="transactionManager_dws" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource_dws"/>
</bean> <!--使用annotation定义事务,比如使用注解-->
<tx:annotation-driven/>
<mvc:annotation-driven/>
</beans>

3、创建好对应的mappers文件

spring mybatis 多数据源配置的更多相关文章

  1. Spring+MyBatis多数据源配置实现

    最近用到了MyBatis配置多数据源,原以为简单配置下就行了,实际操作后发现还是要费些事的,这里记录下,以作备忘 不多废话,直接上代码,后面会有简单的实现介绍 jdbc和log4j的配置 #定义输出格 ...

  2. Spring Mybatis多数据源配置范例

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  3. springMVC+spring+mybatis多数据源配置

    1.application.properties配置 <?xml version="1.0" encoding="UTF-8"?> <bean ...

  4. Spring+Mybatis多数据源配置

    一.配置文件 properties ds1.driverClassName=com.mysql.jdbc.Driver ds1.url=jdbc:mysql://192.168.200.130:330 ...

  5. Spring实现多数据源配置

    一.前言 对于小型项目,服务器与数据库是可以在同一台机子上的,但随着业务的庞大与负责,数据库和服务器就会分离开来.同时随着数据量的增大,数据库也要分开部署到多台机子上. 二.Spring配置文件修改 ...

  6. Spring Boot 2.X(五):MyBatis 多数据源配置

    前言 MyBatis 多数据源配置,最近在项目建设中,需要在原有系统上扩展一个新的业务模块,特意将数据库分库,以便减少复杂度.本文直接以简单的代码示例,如何对 MyBatis 多数据源配置. 准备 创 ...

  7. MyBatis多数据源配置(读写分离)

    原文:http://blog.csdn.net/isea533/article/details/46815385 MyBatis多数据源配置(读写分离) 首先说明,本文的配置使用的最直接的方式,实际用 ...

  8. spring,mybatis事务管理配置与@Transactional注解使用[转]

    spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是至关 ...

  9. spring,mybatis事务管理配置与@Transactional注解使用

    spring,mybatis事务管理配置与@Transactional注解使用[转]   spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是 ...

随机推荐

  1. Unity学习笔记(1)

    transform: transform是GameObject的一个默认的组件,其包含着该对象的几种属性,坐标(Position)以及旋转角度(Rotation)和尺寸(Scale). transfo ...

  2. 【转】: 塞尔达组在GDC2017演讲的文字翻译:创新的勇气

    大家好,我是藤林秀麿,以导演的身份参与<荒野之息>的制作,感谢大家的出席.我曾经作为设计者和导演制作了诸多塞尔达游戏(大地与时空之章.缩小帽.四支剑.幻影沙漏.天空之剑),回首望去,我已经 ...

  3. OA系统与Exchange 日历打通

    目前我碰到好几个案例是希望将客户以后的OA系统与Exchange中的日历系统相结合,比如致远或者泛微的OA系统. 客户的需求如下: 1.有了OA系统 2.客户使用Outlook当邮件客户端 3.客户希 ...

  4. Sublime Text 2 - Unable to find git.exe 错误

    今日打开 Sublime Text 2,随即弹出 Package Control - Unable to find git.exe 错误.如下, 原因:曾经通过 git clone 命令获取过 Sub ...

  5. Scrum立会报告+燃尽图(Beta阶段第二周第七次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2415 项目地址:https://coding.net/u/wuyy694 ...

  6. Docker 安装与常用命令介绍

    docker的镜像文件作用就是:提供container运行的文件系统层级关系(基于AUFS实现),所依赖的库文件.已经配置文件等等. 安装docker yum install -y docker 启动 ...

  7. WebService(二)

    使用eclipse开发webservice的服务器端以及客户端的简单实例 1.服务端 在eclipse中像建立一个web项目一样,new->Dynamic Web Project A.建一个需要 ...

  8. 【第一周】第一周工作统计(psp)

    项目:词频统计 项目类型:个人项目 项目完成情况:已完成 项目改进:未变更 项目日期:2016.9.3-2016.9.4 3号 类别c 内容c 开始时间s 结束e 中断I 净时间T 项目实践 构思   ...

  9. 个人项目----词频统计WEB(部分功能)

    需求分析 1.使用web上传txt文件,对上传的txt进行词频统计. 2.将统计后的结果输出到web页面,力求界面优美. 3.在界面上展示所给url的文章词频统计,力求界面优美. 3.将每个单词同四. ...

  10. PHP中类中成员及常量

    类中成员概述 面向对象编程,是需要通过“对象”去做什么事情(以完成某种任务): 而: 对象总是来源于类: 所以: 面向对象的编程,一切都是从定义类开始: 类中成员分为3大类: 属性: 方法: 常量: ...