struts2+hibernate+spring的配置信息
来源:互联网 发布:linux dhcp配置文件 编辑:程序博客网 时间:2024/06/04 19:55
hibernateTemplate是将数据持久化的一个模块。它的一些实现方法比较简单,如果需要可以上网搜索。我说一下他的配置方法:如下
是通过spring的方式注入的
1HibernateTemplate在applactionContext.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 里面有两个属性
1,dataSource数据源配置文件
2,packagesToScan属性配置要管理的实体类所在的包
注意:可以配置hibernate的mapping配置文件,
根据项目要求配置,在这里我使用的是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>
<!-- 利用AOP对service包以及子包中类的方法加上一个环绕的事务管理
这里特别说明:事务管理的位置,应该放在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以及对实体采用的时候延迟加载的时候hibernate的sessio
到访完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.关于hibernate中save的一点说明
例如
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.只是个人的一些学校心得,希望大家共同进步
- struts2+hibernate+spring的配置信息
- Struts2+Hibernate+Spring配置
- Struts2+spring+hibernate的配置文件配置
- Spring+Hibernate 数据库配置信息的加密
- struts2 - spring -hibernate -proxool配置
- Struts2+Spring+Hibernate 配置步骤
- struts2+hibernate+spring配置详解
- Struts2,Hibernate,Spring 环境配置
- Spring+Struts2+Hibernate整合配置
- 关于Struts2+Spring+hibernate的整合开发的配置
- ssh整合:将hibernate的配置信息,配置到Spring的配置文件中(Spring整合hibernate)
- 03-Spring+Hibernate+Struts2的整合环境配置
- 在struts2,hibernate.spring版本包里找配置的范例文件
- 配置Spring+struts2+hibernate时遇到的问题
- Struts2 + Spring + Hibernate 整合 web.xml的一般配置
- Struts2+Spring+Hibernate三大框架的配置
- 基于maven 管理下Spring,Struts2,hibernate的pom配置
- Hibernate的配置信息
- [Java] 练习题009
- CCombox颜色对话框绘制2
- THinkPHP 跳转
- C++关于webbrowser的字体修改
- C#操作Xml的两种方式 XPath XmlDocument XmlNodeList
- struts2+hibernate+spring的配置信息
- 三秒钟快速破解加密的PDF文件
- 自动化批量扫描
- Message Reflection for Windows Controls
- 总是谨慎有余,自信不足
- MEL写界面
- 浅谈软件测试流程
- 思念
- Android布局整合include界面控件(重用布局)