Struts2+Spring+hibernate之jndi分布式数据库搭建-weblogic中间件

1、web.xml配制文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>test</display-name>
<distributable />
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<filter> 
  <filter-name>CharacterEncoding</filter-name> 
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
  <init-param> 
  <param-name>encoding</param-name> 
  <param-value>UTF-8</param-value> 
  </init-param> 
  <init-param> 
  <param-name>forceEncoding</param-name> 
  <param-value>true</param-value> 
  </init-param> 
  </filter>
  <filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- shiro security filter 要配置在struts2的filter前面,并且要求可以拦截struts2匹配的请求-->
<!-- <filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping> -->

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>


<filter> 
    <filter-name>openSessionInViewFilter</filter-name> 
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> 
    <init-param>
       <param-name>singleSession</param-name> 
   <param-value>true</param-value> 
    </init-param>
    <init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sf2</param-value>
</init-param>
</filter>
<filter-mapping>
         <filter-name>openSessionInViewFilter</filter-name>
         <url-pattern>/*</url-pattern>
    </filter-mapping>
<filter>
   <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
   <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
   <init-param>
      <param-name>entityManagerFactoryBeanName</param-name>  
      <param-value>entityManagerFactory1</param-value>    
   </init-param>  
</filter>
   
   
<error-page>
<exception-type>500</exception-type>
<location>/error/500.jsp</location>
</error-page>
<error-page>
   <error-code>404</error-code>
   <location>/error/404.jsp</location>
</error-page>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.spring-db.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:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
     ">
<context:annotation-config/>
  <!-- jndi数据源配制 -->
  <jee:jndi-lookup id="dataSource1" jndi-name="jdbc/data1"/>
  <jee:jndi-lookup id="dataSource2" jndi-name="jdbc/data2"/>
 

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource1" />
<property name="packagesToScan">
<list>
<value>com.test.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
<property name="jtaTransactionManager">
   <ref bean="transactionManager" />
  </property>
</bean>


<bean id="sf2"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource2" />
<property name="packagesToScan">
<list>
<value>com.test.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
    <prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
<property name="jtaTransactionManager">
   <ref bean="transactionManager" />
  </property>
</bean>

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="entityManagerFactory1" 
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
        <property name="dataSource" ref="dataSource1" />
        <property name="persistenceUnitName" value="test" />
        <!-- <property name="persistenceXmlLocation" value="classpath:persistence.xml" />   -->
        <property name="jpaVendorAdapter"> 
            <bean 
                class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
                <property name="database" value="ORACLE" /> 
                <property name="showSql" value="true" /> 
                <!-- <property name="generateDdl" value="true" /> --> 
            </bean> 
        </property>
       
        <property name="jpaPropertyMap"> 
            <map> 
                <entry key="hibernate.transaction.manager_lookup_class" 
                       value="org.hibernate.transaction.WeblogicTransactionManagerLookup" /> 
                <entry key="hibernate.transaction.flush_before_completion" value="true" /> 
                <entry key="hibernate.transaction.auto_close_session" value="true" /> 
                <entry key="hibernate.current_session_context_class" value="jta" /> 
                <entry key="hibernate.connection.release_mode" value="auto" /> 
            </map> 
        </property>  
    </bean>
   
     <bean id="entityManagerFactory2" 
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
        <property name="dataSource" ref="dataSource2" />
        <property name="persistenceUnitName" value="test" />
      <!--   <property name="persistenceXmlLocation" value="classpath:persistence.xml" />   -->
        <property name="jpaVendorAdapter"> 
            <bean 
                class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
                <property name="database" value="ORACLE" /> 
                <property name="showSql" value="true" /> 
               <!--  <property name="generateDdl" value="true" />   -->
            </bean> 
        </property>
       
        <property name="jpaPropertyMap"> 
            <map> 
                <entry key="hibernate.transaction.manager_lookup_class" 
                       value="org.hibernate.transaction.WeblogicTransactionManagerLookup" /> 
                <entry key="hibernate.transaction.flush_before_completion" value="true" /> 
                <entry key="hibernate.transaction.auto_close_session" value="true" /> 
                <entry key="hibernate.current_session_context_class" value="jta" /> 
                <entry key="hibernate.connection.release_mode" value="auto" /> 
            </map> 
        </property>  
    </bean>

<!-- JTA事务管理器 -->
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager" >
</bean>
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<tx:jta-transaction-manager  />
</beans>

你可能感兴趣的:(Hibernate)