J2EE剑侠行

直觉我的J2EE应用生涯,打造我心中的一把利剑。

常用链接

统计

技术链接

最新评论

配置Springframework与hibernate连接多数据库的事务

本文只注重配置,分别有如下几个文件
<出处:和巍雅的BLOG
web.xml 部署配置文件
struts-config.xml struts的配置文件
action-servlet.xml Spring依赖注入struts
Nhibernate_Context.xml Spring依赖注入struts
hibernate.cfg.xml mssql的Hibernate配置
hibernate_mysql.cfg.xml mysql的Hibernate配置 
开始:
web.xml
------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp">
<display-name>EAIJ2EEPRJWeb</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Nhibernate_Context.xml</param-value>
</context-param>
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<display-name>SetCharacterEncodingFilter</display-name>
<filter-class>com.throuhout.j2eemodel.util.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>SpringContextServlet</servlet-name>
<display-name>SpringContextServlet</display-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<mime-mapping>
<extension>wsdl</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xsd</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-template.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>

</web-app>
---------------------------------------------------------------------
struts_config.xml
---------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>

<!-- 数据源 -->
<data-sources></data-sources>

<!-- 表单 Bean -->
<form-beans>
<form-bean name="UserActionForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="user" type="com.throuhout.j2eemodel.hibernate.TSysuser" />
</form-bean>
<form-bean name="UserMenuActionForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="menu" type="com.throuhout.j2eemodel.hibernate.TSysmufudef">
</form-property>
</form-bean>
</form-beans>

<!-- 全局异常 -->
<global-exceptions></global-exceptions>


<!-- 全局转发 -->
<global-forwards>
<forward name="success" path="/main/success.jsp"></forward>
<forward name="failure" path="/main/fail.jsp"></forward>
</global-forwards>

<!-- 操作映射 -->
<action-mappings>
<action path="/User" type="org.springframework.web.struts.DelegatingActionProxy" name="UserActionForm" scope="request" parameter="method" validate="false">
<forward name="sucess" path="/main/project_index.htm" />
<forward name="false" path="/failure.html"></forward>
<forward name="list" path="/main/userlist.jsp">
</forward>
<forward name="edit" path="/main/userform.jsp">
</forward>
</action>
<action path="/UserMenu" type="org.springframework.web.struts.DelegatingActionProxy" name="UserMenuActionForm" scope="request" parameter="method" validate="false">
<forward name="menu" path="/main/menu.jsp"></forward>
<forward name="usermenu" path="/main/usermenu.jsp">
</forward>
</action>
</action-mappings>

<!-- 消息资源 -->
<message-resources parameter="eaij2eeprjweb.resources.ApplicationResources" />
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/action-servlet.xml" />
</plug-in>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml" />
</plug-in>

</struts-config>
-------------------------------------------------------------------
action-servlet.xml
--------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<!-- Spring依赖注入到Struts当中 -->
<beans>
<bean name="/User" class="com.throuhout.j2eemodel.struts.action.UserAction" singleton="false">
<property name="userManager">
<ref bean="userManager" />
</property>
</bean>
<bean name="/UserMenu" class="com.throuhout.j2eemodel.struts.action.UserMenuAction" singleton="false">
<property name="userManager">
<ref bean="userManager" />
</property>
</bean>

</beans>
-------------------------------------------------------------------
Nhibernate_Context.xml
--------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<!-- Spring依赖注入到Struts当中 -->
<!-- Hibernate SessionFactory -->
<beans>
<bean id="mssqlSessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">

<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.SybaseDialect</prop>

<!--<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.hibernate.cache.OSCacheProvider</prop>-->
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">25</prop>
</props>
</property>
</bean>

<bean id="mysqlSessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate_mysql.cfg.xml</value>
</property>

<property name="hibernateProperties">
<props>
<!--<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.hibernate.cache.OSCacheProvider</prop>-->
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">25</prop>
</props>
</property>
</bean>

<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->

<bean id="mysqltransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="mysqlSessionFactory" />
</property>
</bean>

<bean id="mssqltransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="mssqlSessionFactory" />
</property>
</bean>

<!--
<bean id="aDao" class="com.plat.dao.user.impl.aDaoImpl">
<property name="sessionFactory">
<ref local="oracleSessionFactory" />
</property>
</bean>
-->
<bean id="userDao" class="com.throuhout.j2eemodel.spring.service.dao.hibernate.UserHibernateDao">
<property name="sessionFactory">
<ref local="mysqlSessionFactory" />
</property>
</bean>


<!-- Service -->
<bean id="userManagerTarget" class="com.throuhout.j2eemodel.spring.service.UserManagerImpl">
<property name="userDao">
<ref local="userDao" />
</property>
</bean>

<bean id="userManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="mysqltransactionManager" />
</property>

<property name="target">
<ref local="userManagerTarget" />
</property>

<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
</beans>
---------------------------------------------------------------------
hibernate.cfg.xml
---------------------------------------------------------------------
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"hibernate-configuration-2.0.dtd">

<hibernate-configuration>

<session-factory>
<property name="show_sql">true</property>
<property name="use_outer_join">false</property>

<property name="hibernate.dialect">net.sf.hibernate.dialect.SybaseDialect</property>

<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:jtds:sqlserver://192.168.100.111:1433/testdb</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">yaya</property>
<!--
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://192.168.100.148:1433/throughoutdb</property>
<property name="hibernate.connection.username">siaote</property>
<property name="hibernate.connection.password">siaote</property>
-->
<property name="hibernate.connection.pool.size">20</property>
<property name="session_factory_name">mssqlSessionFactory</property>

<!-- Mapping files -->
<mapping resource="hbm_tb/T_COST_TYPE.hbm.xml" />


</session-factory>
</hibernate-configuration>
-------------------------------------------------------------------
hibernate_mysql.cfg.xml
-------------------------------------------------------------------
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"hibernate-configuration-2.0.dtd">

<hibernate-configuration>

<session-factory>
<property name="show_sql">true</property>
<property name="use_outer_join">false</property>

<property name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</property>

<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost/pdm</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>

<property name="hibernate.connection.pool.size">20</property>
<property name="session_factory_name">mysqlSessionFactory</property>

<!-- Mapping files -->
<mapping resource="hbm_tb/TSysuser.hbm.xml" />
<mapping resource="hbm_tb/TSysmufudef.hbm.xml" />


</session-factory>
</hibernate-configuration>

----------------------------------------------------------------
本人的开发环境:
开发工具:WSAD 5.1
中间件服务器:tomcat 4.24
数据库:mysql\mssql 完全通过。
你可以和 heweiya@yahoo.com.cn 联系

posted on 2005-12-19 11:26 @家军 阅读(993) 评论(0)  编辑  收藏 所属分类: J2EE技术类


只有注册用户登录后才能发表评论。


网站导航: