struts2+hibernate+spring的配置信息

来源:互联网 发布:linux dhcp配置文件 编辑:程序博客网 时间:2024/06/04 19:55

hibernateTemplate是将数据持久化的一个模块。它的一些实现方法比较简单,如果需要可以上网搜索。我说一下他的配置方法:如下

是通过spring的方式注入的

1HibernateTemplateapplactionContext.xml中的配置说明

<!-- 配置数据源 -->

   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">

  <property name="driverClassName"  value="com.mysql.jdbc.Driver">

       <!--此处是配置数据库的链接,根据需要进行配置-->

</property>

       <property name="url" value="jdbc:mysql://localhost:3306/test"></property>

       <property name="username"value="root"></property>

       <property name="password"value="111111"></property>

   </bean>

    <!-- 配置sessionFactory 里面有两个属性

       1dataSource数据源配置文件

       2packagesToScan属性配置要管理的实体类所在的包

       注意:可以配置hibernatemapping配置文件,

       根据项目要求配置,在这里我使用的是hibernate的注解的方式

       3,可以配置数据库的方言,最大连接数,最大等待时间,等待连接数...

   -->

   <bean id="sessionFactory"

       class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

       <property name="dataSource"ref="dataSource"></property>

       <property name="packagesToScan">

           <list>

               <value>com.sheng.model</value>

           </list>

       </property>

       <property name="hibernateProperties">

           <props>

               <prop key="hibernate.dialect">

                   org.hibernate.dialect.MySQLDialect

               </prop>

               <prop key="hibernate.show_sql">true</prop>

           </props>

       </property>

   </bean>

    <!--Hibernate的模板(HibernateTemplate),对实体的增删查改操作  需要

sessionFactory-->

   <bean id="hibernateTemplate"

       class="org.springframework.orm.hibernate3.HibernateTemplate">

       <property name="sessionFactory" ref="sessionFactory"></property>

   </bean>

    <!-- 事物的管理需要注入sessionFactory -->

   <bean id="txManager"

      class="org.springframework.orm.hibernate3.HibernateTransactionManager">

       <property name="sessionFactory" ref="sessionFactory"></property>

   </bean>

    <!-- 利用AOPservice包以及子包中类的方法加上一个环绕的事务管理

这里特别说明:事务管理的位置,应该放在service层进行管理,比如经典ATM机,可

方便事物同事回滚

 -->

   <aop:config>

       <aop:pointcut id="service"

           expression="execution(public *com.sheng.service.*.*(..))" />

       <!-- 在这里配置需要加事务的切面类 -->

       <aop:advisor pointcut-ref="service"advice-ref="txAdvice" />

   </aop:config>

   <tx:advice id="txAdvice" transaction-manager="txManager">

       <tx:attributes>

<!-- 需要加上的事务的可操作,查找的时候用read-only="true"

propagation="REQUIRED"

 REQUIRED判断事物是否存在,如果存在就用当前的事物,如果不存在则创建一个-->

           <tx:method name="get*"read-only="true" />

               <tx:method name="load*"read-only="true" />

           <tx:method name="add*"propagation="REQUIRED" />

           <tx:method name="update*"propagation="REQUIRED" />

           <tx:method name="delete*" propagation="REQUIRED"/>

       </tx:attributes>

   </tx:advice>

 

   <!--  <import resource="applactionContext_action.xml"/>

       <import resource="applactionContext_dao.xml"/>

       <import resource="applactionContext_service.xml"/>-->

</beans>

2.web.xml中的配置说明

<?xml version="1.0"encoding="UTF-8"?>

<web-appversion="2.5" xmlns="http://java.sun.com/xml/ns/javaee"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 

    <!-- 配置spring启动的时候自动加载配置环境配置文件 -->

   <listener>

       <listener-class>

           org.springframework.web.context.ContextLoaderListener

       </listener-class>

   </listener>

    <!-- 启动服务器的时候默认情况下是在 webRoot下,如果是在src目录下需配置如下

-->

   <context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>classpath:applactionContext*.xml</param-value>

   </context-param>

    <!-- 在查找实体的时候用load以及对实体采用的时候延迟加载的时候hibernatesessio

到访完jsp页面之后才能关闭,否则在service执行完之后就关闭,页面上就取不到像要取的内容,这里配置解决延迟加载的问题 -->

 

   <filter>

       <filter-name>openHibernateSessionFactoryBean</filter-name>

       <filter-class>

           org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

       </filter-class>

       <!-- 配置sessionFactory的统一默认是sessionFactory在这里可以不配,这里

applactionContext.xml中是sessionFactory相对应 -->

       <!--  <init-param>

           <param-name>sessionFactoryBeanName</param-name>

           <param-value>sessionFactory</param-value>

           </init-param>

       -->

       <init-param>

 

           <param-name>flushMode</param-name>

 

           <param-value>AUTO</param-value>

 

       </init-param>

   </filter>

   <filter-mapping>

       <filter-name>openHibernateSessionFactoryBean</filter-name>       <url-pattern>/*</url-pattern>

</filter-mapping>

   <!-- 对页面的传的值进行拦截,拦截时候改变编码-->

   <filter>

       <filter-name>encodingFilter</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>

   </filter>

   <filter-mapping>

       <filter-name>encodingFilter</filter-name>

       <url-pattern>/*</url-pattern>

   </filter-mapping>

 

    <!-- 过滤之后交给struts2拦截器来实现控制操作 -->

    <filter>

       <filter-name>struts2</filter-name>

       <filter-class>

           org.apache.struts2.dispatcher.FilterDispatcher

       </filter-class>

</filter>

   <filter-mapping>

       <filter-name>struts2</filter-name>

       <url-pattern>/*</url-pattern>

   </filter-mapping>

</web-app>

 

3.关于hibernatesave的一点说明

例如

public class A{

private B b;

...

}

在保持A的时候,属性B必须是从数据库中取出来的,否则会报数据不能持久化的异常:

org.springframework.dao.InvalidDataAccessApiUsageException:object

references an unsaved transient instance - save the transientinstance

before flushing: com.sheng.model.User; nested exceptionis

org.hibernate.TransientObjectException: object referencesan unsaved transient instance - save the transient instance before flushing:

 4.只是个人的一些学校心得,希望大家共同进步

 

原创粉丝点击