在Spring-Mybatis-Restful中配置多数据源的properties文件
在Spring的配置文件中,如下:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-autowire="byName" default-lazy-init="false">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/database.properties</value>
</list>
</property>
</bean> <bean id="ds1" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${cc.driver}"/>
<property name="url" value="${cc.url}"/>
<property name="username" value="${cc.username}"/>
<property name="password" value="${cc.password}"/>
</bean>
<bean id="ds2" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${tt.driver}"/>
<property name="url" value="${tt.url}"/>
<property name="username" value="${tt.username}"/>
<property name="password" value="${tt.password}"/>
</bean>
<bean id="dataSource" class="cn.ac.iscas.pebble.ufe.ds.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="ds1" key="ds1"></entry>
<entry value-ref="ds2" key="ds2"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="ds1"></property> <!-- 默认使用ds1的数据源 -->
</bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <bean id="ysSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:cn/ac/iscas/pebble/ufe/mapper/*Mapper.xml"/>
</bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="ysSqlSessionFactory" />
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="ysSqlSessionFactory"/>
<property name="basePackage" value="cn.ac.iscas.pebble.ufe.inter"/>
</bean>
</beans>
注意上述黄色背景的部分,注意事项:
1. 其中必须是 sqlSessionFactoryBeanName 而不能是 sqlSessionFactoryBean
2. 如果是sqlSessionFactoryBeanName 则其后应该用value,而不是ref(ref对应于sqlSessionFactoryBean)
3. org.mybatis.spring.SqlSessionFactoryBean 的id 不能是 SqlSessionFactory,也不能类似SqlSessionFactory2,sqlSessionFactory或者MySqlSessionFactory,可以是ySqlSessionFactory等。
主要原因是mybatis的问题,在有mybatis的时候,Spring会先加载MapperScannerConfigurer,而使用sqlSessionFactoryBean就会报错找不到DatabaseID,因为此时还没有读取database.properties文件,sqlSessionFactoryBean在实例化的时候报错,可以使用sqlSessionFactoryBeanName,此参数不会实例化,只是声明。
在Spring-Mybatis-Restful中配置多数据源的properties文件的更多相关文章
- 在Spring MVC项目中,注解方式使用 .properties 文件及 UTF-8编码问题
xml配置 <!-- 配置文件 --> <bean id="configProperties" class="org.springframework.b ...
- Spring MVC 使用tomcat中配置的数据源
Spring MVC 使用tomcat中配置的数据源 配置tomcat数据源 打开tomcat目录下的conf目录,编辑sever.xml目录.在<GlobalNamingResources&g ...
- spring,mybatis事务管理配置与@Transactional注解使用[转]
spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是至关 ...
- spring,mybatis事务管理配置与@Transactional注解使用
spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是 ...
- Spring+MyBatis双数据库配置
Spring+MyBatis双数据库配置 近期项目中遇到要调用其它数据库的情况.本来仅仅使用一个MySQL数据库.但随着项目内容越来越多,逻辑越来越复杂. 原来一个数据库已经不够用了,须要分库分表.所 ...
- WebSphere中配置的数据源在Web应用中引用的写法
WebSphere中配置的数据源在Web应用中引用时名称一定要和数据源的JNDI名称保持一致,否则会出现无法找到数据源的错误. 引用WAS的数据源时只需要与JNDI名称保持一致即可. 引用Tomcat ...
- Spring Boot整合Druid配置多数据源
Druid是阿里开发的数据库连接池,功能强大,号称Java语言中最好的数据库连接池.本文主要介绍Srping Boot下用Druid配置多个数据源,demo环境为:Spring Boot 2.1.4. ...
- AndroidStudio -- AndroidStuido中找不到cache.properties文件
AndroidStuido中找不到cache.properties文件 报错信息: 16:32:10 Gradle sync failed: C:\Users\***\.gradle\caches\2 ...
- spring、spring boot中配置多数据源
在项目开发的过程中,有时我们有这样的需求,需要去调用别的系统中的数据,那么这个时候系统中就存在多个数据源了,那么我们如何来解决程序在运行的过程中到底是使用的那个数据源呢? 假设我们系统中存在2个数据源 ...
随机推荐
- 使用xib需要记得的小问题
1. 图片 加载 图片上的label 不显示, 最后是因为xib 里位置动了 图片跑到最上层盖住了labe 2. 加载xib 有时候会崩 或加载不出来 先查看xib 是否有多余控件 3. 查看关联 ...
- LeetCode : 223. Rectangle Area
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRQAAAQ0CAYAAAAPPZBqAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw
- sqlite建表语句(特别是外键问题)
原创 sqlite建表语句(特别是外键问题) 下面图表示两个表关系: //表1User_invitecreate table User_invite(Invite_id INTEGER PRIMAR ...
- 三、jQuery--jQuery实践--搜索框制作
input标签讲解 <input/>作为按钮的type属性:button.submit(后面会有二者对比分析)
- 用#define来实现多份近似代码 - map,set中的应用
在stl中map,set内部都是使用相同的红黑树实现,map对应模板参数key_type,mapped_type,而set对应模板参数没有mapped_type 两者都支持insert操作 pair& ...
- ASP.NET Web Api 使用CacheCow和ETag缓存资源(转载)
转载地址:http://www.cnblogs.com/fzrain/p/3618887.html 前言 本文将使用一个开源框架CacheCow来实现针对Http请求资源缓存,本文主要介绍服务器端的缓 ...
- Mongo DB Study: first face with mongo DB
Mongo DB Study: first face with mongo DB 1. study methods: 1. Translate: I am the mongo DB organiz ...
- Linux环境下stl库使用(vector)
step1: #include <iostream> #include <vector> #include <string> using namespace std ...
- Intellij Idea 使用
一.使用前需要修改的配置: 1.当类实现Serializable接口时,自动生成 serialVersionUID 1)Setting->Inspections->java->Ser ...
- PMP 第四章 项目整合管理
1.什么是整合管理,整合什么?如何整合? 项目整合管理包括识别 定义 组合 统一与协调项目管理过组的个过程及项目管理活动二进行的各种过程和活动. 整合兼具统一 合并 连接和一体化的性质,对 ...