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. [转]50 Tips for Working with Unity (Best Practices)

    About these tips These tips are not all applicable to every project. They are based on my experience ...

  2. 7个Node.js的Web框架

    NodeJS也就是Node,是众所周知的使用javascript构建Web应用框架,它启动一个服务器非常简单,如下: var http = require('http'); http.createSe ...

  3. [转]Zookeeper系列(一)

    一.ZooKeeper的背景 1.1 认识ZooKeeper ZooKeeper---译名为“动物园管理员”.动物园里当然有好多的动物,游客可以根据动物园提供的向导图到不同的场馆观赏各种类型的动物,而 ...

  4. CSS布局之圣杯布局和双飞翼布局

    其实圣杯布局和双飞翼布局实现的都是三栏布局,两边的盒子宽度固定,中间盒子自适应,也就是我们常说的固比固布局.它们实现的效果都是一样的,差别在于其实现的思想. 一.圣杯布局 html代码中,将重要的内容 ...

  5. “Hello World!”团队第七周召开的第一次会议

    今天是我们团队“Hello World!”团队第七周召开的第一次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.Todo List 六.会议照片 七.燃尽图 一.会议时间 ...

  6. A8

    组员:陈锦谋 今日内容: PS学习.抠图.图标像素调整 明日计划: 继续小组内安排的任务 困难: 无

  7. struts2 不返回result的做法

    有时候 比如提交一个弹框的表单 提交成功后我们只是让表单关闭并不进行页面跳转,那么action 里面就returne null, 然后result 也不用配置了 版权声明:本文为博主原创文章,未经博主 ...

  8. DP---(POJ1159 POJ1458 POJ1141)

    POJ1159,动态规划经典题目,很适合初学者入门练手. 求:为了使字符串左右对称,应该插入的最小字符数目. 设字符串为S1 S2 S3 - Sn. 这个字符串有n个字符,根据DP的基本思路,减少问题 ...

  9. 线段树---no end

    额,还有 :区间操作,交,并,补等 区间合并 扫描线 这些问题有空再研究吧.... 先看j2ee了..... 传送门 版权声明:本文为博主原创文章,未经博主允许不得转载.

  10. QSharedMemory共享内存实现进程间通讯(IPC)及禁止程序多开

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QSharedMemory共享内存实现进程间通讯(IPC)及禁止程序多开     本文地址:h ...