Ssh多数据源Word文档格式.docx
《Ssh多数据源Word文档格式.docx》由会员分享,可在线阅读,更多相关《Ssh多数据源Word文档格式.docx(10页珍藏版)》请在冰豆网上搜索。
xsi:
schemaLocation="
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
//www.springframework.org/schema/aop
//www.springframework.org/schema/aop/spring-aop-3.0.xsd
//www.springframework.org/schema/tx
//www.springframework.org/schema/tx/spring-tx-3.0.xsd
//www.springframework.org/schema/context
//www.springframework.org/schema/context/spring-context-3.0.xsd"
<
!
--开启注解处理器-->
context:
annotation-config/>
--定义使用C3P0连接池的数据源-->
beanid="
dataSourceA"
class="
com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="
close"
<
--指定连接数据库的JDBC驱动-->
propertyname="
driverClass"
<
value>
dm.jdbc.driver.DmDriver<
/value>
/property>
--连接数据库所用的URL-->
jdbcUrl"
jdbc:
dm:
//192.168.1.250:
5236<
--连接数据库的用户名-->
user"
sysdba<
--连接数据库的密码-->
password"
222<
--设置数据库连接池的最大连接数-->
maxPoolSize"
20<
--设置数据库连接池的最小连接数-->
minPoolSize"
2<
--设置数据库连接池的初始化连接数-->
initialPoolSize"
--设置数据库连接池的连接的最大空闲时间,单位为秒-->
maxIdleTime"
/bean>
--定义使用C3P0连接池的数据源(基础库数据源)-->
dataSourceB"
5237<
--定义使用C3P0连接池的数据源(测试数据源)-->
dataSourceC"
5238<
dynamicDataSource"
mon.DynamicDataSource"
--通过key-value的形式来关联数据源-->
targetDataSources"
mapkey-type="
java.lang.String"
<
entryvalue-ref="
key="
/entry>
/map>
defaultTargetDataSource"
ref="
--定义Hibernate的SessionFactory-->
sessionFactory"
class="
org.springframework.orm.hibernate3.LocalSessionFactoryBean"
--依赖注入上面定义的数据源dataSource-->
dataSource"
/>
--注册Hibernate的ORM映射文件-->
mappingResources"
list>
com/hjzbjcxcyw/po/test/liwh/Book3.hbm.xml<
com/hjzbjcxcyw/po/test/liwh/Book.hbm.xml<
com/hjzbjcxcyw/po/test/liwh/Department.hbm.xml<
com/hjzbjcxcyw/po/test/liwh/Employee.hbm.xml<
com/hjzbjcxcyw/po/test/liwh/Bookb.hbm.xml<
/list>
--设置Hibernate的相关属性-->
hibernateProperties"
props>
propkey="
hibernate.cache.user_second_level_cache"
false<
/prop>
--设置Hibernate的数据库方言-->
hibernate.dialect"
org.hibernate.dialect.DmDialect<
--设置Hibernate是否在控制台输出SQL语句,开发调试阶段通常设为true-->
show_sql"
true<
--设置Hibernate一个提交批次中的最大SQL语句数-->
hibernate.jdbc.batch_size"
50<
/props>
--定义Hibernate的事务管理器HibernateTransactionManager-->
transactionManager"
org.springframework.orm.hibernate3.HibernateTransactionManager"
--依赖注入上面定义的sessionFactory-->
--定义一个事务通知txAdvice,配置事务的传播特性-->
tx:
adviceid="
txAdvice"
transaction-manager="
attributes>
--所有以browse、list、load、get及is开头的业务逻辑方法均不需要事务控制且只读-->
methodname="
browse*"
propagation="
NOT_SUPPORTED"
read-only="
true"
list*"
load*"
get*"
is*"
--设置所有方法均进行事务控制,如果当前没有事务,则新建一个事务-->
*"
REQUIRED"
/tx:
advice>
--装配HibernateTemplate实例-->
hibernateTemplate"
org.springframework.orm.hibernate3.HibernateTemplate"
constructor-argref="
--装配通用数据库访问类BaseDAOImpl-->
dao"
com.hjzbjcxcyw.dao.BaseDAOImpl"
/beans>
Book3.hbm.xml文件如下:
utf-8"
DOCTYPEhibernate-mappingPUBLIC"
-//Hibernate/HibernateMappingDTD3.0//EN"
"
hibernate-mapping>
classname="
com.hjzbjcxcyw.po.test.liwh.Book3"
table='
liwh"
."
BOOK3"
'
(liwh是模式名,BOOK3是表名)
idname="
id"
column="
type="
length="
36"
generatorclass="
assigned"
/id>
bookName"
columnname="
BOOK_NAME"
50"
isbn"
ISBN"
256"
author"
AUTHOR"
title"
TITLE"
date"
java.util.Date"
ids"
IDS"
22"
/class>
/hibernate-mapping>
DynamicDataSource.action//动态分配数据源
packagemon;
importorg.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
publicclassDynamicDataSourceextendsAbstractRoutingDataSource{
@Override
protectedObjectdetermineCurrentLookupKey(){
returnDataSourceContextHolder.getProxType();
}
DataSourceContextHolder.action//设置数据源
publicclassDataSourceContextHolder{
publicstaticfinalStringDATA_SOURCE_A="
;
publicstaticfinalStringDATA_SOURCE_B="
publicstaticfinalStringDATA_SOURCE_C="
privatestaticfinalThreadLocal<
String>
contextHolder=newThreadLocal<
();
publicstaticvoidsetProxType(StringcustomerType){
contextHolder.set(customerType);
publicstaticStringgetProxType(){
returncontextHolder.get();
publicstaticvoidclearProxType(){
contextHolder.remove();
}
Tools.action//指定数据源
publicstaticvoidsetDataSource(inth){
if(h==1)
DataSourceContextHolder.setProxType(DataSourceContextHolder.DATA_SOURCE_A);
elseif(h==2)
DataSourceContextHolder.setProxType(DataSourceContextHolder.DATA_SOURCE_B);
elseif(h==3)
DataSourceContextHolder.setProxType(DataSourceContextHolder.DATA_SOURCE_C);
在service文件的各个方法中指定数据源。
publicList<
Employee>
browseEmployee(){
Tools.setDataSource(3);
List<
l=dao.listAll("
Employee"
);
System.out.println("
booksize="
+l.size());
returnl;
在acton文件中调用service的browseEmployee()方法。
大功告成,呵呵!
2013-04-01