Hibernate4+Spring3+SpringMVC事务管理
来源:互联网 发布:富士照片打印软件 编辑:程序博客网 时间:2024/06/07 08:34
一,Hibernate4配置
1,首先将hibernate配置信息抽到hibernate.properties里面
dataSource.password=111111
dataSource.username=rootdataSource.databaseName=goodsmanagedataSource.driverClassName=com.mysql.jdbc.DriverdataSource.dialect=org.hibernate.dialect.MySQL5DialectdataSource.serverName=localhost:3306dataSource.url=jdbc:mysql://localhost:3306/goodsmanagedataSource.properties=user=${dataSource.username};databaseName=${dataSource.databaseName};serverName=${dataSource.serverName};password=${dataSource.password}dataSource.hbm2ddl.auto=updatedataSource.show_sql=truehibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialecthibernate.hbm2ddl.auto=nonehibernate.show_sql=falsehibernate.query.substitutions=true 1, false 0hibernate.default_batch_fetch_size=16hibernate.max_fetch_depth=2hibernate.bytecode.use_reflection_optimizer=truehibernate.cache.use_second_level_cache=truehibernate.cache.use_query_cache=truehibernate.cache.region.factory_class=org.hibernate.cache.EhCacheRegionFactorynet.sf.ehcache.configurationResourceName=/ehcache_hibernate.xmlhibernate.cache.use_structured_entries=truehibernate.generate_statistics=trueproxool.maximum.connection.count=40proxool.minimum.connection.count=5proxool.statistics=1m,15m,1h,1dproxool.simultaneous.build.throttle=30,
2,hibernate4已经不在支持使用HibernateTemplate模板类,这里需要将hibernate的sessionFactory和dataSource注入到Spring的事务管理器
SpringContext.xml的配置为
<context:property-placeholder location="classpath:/hibernate.properties" /><!-- Hibernate 参数配置 --><bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 自动扫描指定Package中的@Entity Class --><property name="packagesToScan" value="com.geng.goodsmanage" /><property name="hibernateProperties"><props><prop key="hibernate.dialect">${dataSource.dialect}</prop><prop key="hibernate.generate_statistics">true</prop><prop key="hibernate.connection.release_mode">auto</prop><prop key="hibernate.hbm2ddl.auto">${dataSource.hbm2ddl.auto}</prop><prop key="hibernate.show_sql">${dataSource.show_sql}</prop><prop key="hibernate.format_sql">true</prop><prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop><prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop><prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop><prop key="hibernate.bytecode.use_reflection_optimizer">${hibernate.bytecode.use_reflection_optimizer}</prop><prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop><prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop><prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop><prop key="net.sf.ehcache.configurationResourceName">${net.sf.ehcache.configurationResourceName}</prop><prop key="hibernate.cache.use_structured_entries">${hibernate.cache.use_structured_entries}</prop></props></property></bean><bean id="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /><property name="dataSource" ref="dataSource" /></bean><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${dataSource.driverClassName}" /><property name="url" value="${dataSource.url}" /><property name="username" value="${dataSource.username}" /><property name="password" value="${dataSource.password}" /></bean>
3,Hibernate4默认必须为开启事务,否则 getCurrentSession()获取不到
2,Spring3声明事务管理
声明事务配置变化不大
<!-- 定义AOP拦截器 --><aop:config proxy-target-class="true"><aop:advisor pointcut="execution(* com.geng..*Service.*(..))"advice-ref="txAdvice" /><!-- <aop:advisor pointcut="execution(* org.geng..*DAO.*(..))" advice-ref="txAdvice" /> --></aop:config><!-- 基本事务定义,使用transactionManager作事务管理,默认get*方法的事务为readonly,其余方法按默认设置 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/><tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/><tx:method name="create*" propagation="REQUIRED" rollback-for="java.lang.Exception"/><tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/><tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/><tx:method name="merge*" propagation="REQUIRED" rollback-for="java.lang.Exception"/><tx:method name="del*" propagation="REQUIRED" rollback-for="java.lang.Exception"/><tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception"/><tx:method name="put*" propagation="REQUIRED" rollback-for="java.lang.Exception"/><tx:method name="use*" propagation="REQUIRED" rollback-for="java.lang.Exception"/><!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到 --><tx:method name="get*" propagation="REQUIRED" read-only="true" /><tx:method name="count*" propagation="REQUIRED" read-only="true" /><tx:method name="find*" propagation="REQUIRED" read-only="true" /><tx:method name="list*" propagation="REQUIRED" read-only="true" /><tx:method name="*" read-only="true" /></tx:attributes></tx:advice>
总结:编程时需注意业务上的事务范围,如生成订单,他的事务需在一个service方法里面需要先将货物数量减去,做update,然后再在订单表插入订单,如果这两步操作放在两个service方法里面恐怕异常就无法做到同时回滚。
3,SpringMVC主要配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 会自动注册了validator ConversionService --><mvc:annotation-driven /> <!-- 防止图片js csss被拦截 --> <!-- <mvc:default-servlet-handler /> --> <!-- 对静态资源文件的访问 方案二 (二选一)--> <mvc:resources mapping="/**" location="/" /> <!-- <mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/> <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/> <mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/> --> <context:component-scan base-package="com.geng.goodsmanage"> <context:include-filter type="annotation"expression="org.springframework.stereotype.Controller" /><context:exclude-filter type="annotation"expression="org.springframework.stereotype.Service" /> </context:component-scan> <!-- 定义首页 --> <mvc:view-controller path="/" view-name="forward:/login/toLogin/" /> <!-- 视图的前缀和后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
4,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>goodsmanage</display-name> <!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring Context Loader --><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring-mvc.xml,classpath*:/applicationContext*.xml</param-value></context-param><servlet><servlet-name>goodsmanage</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/spring-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>goodsmanage</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- Filter --><filter><filter-name>hibernateFilter</filter-name><filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class><init-param><param-name>singleSession</param-name><param-value>true</param-value></init-param></filter><!-- Character Encoding filter --><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><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- opensession --><filter> <filter-name>hibernateFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> <!-- 容器啟動時 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
0 0
- Hibernate4+Spring3+SpringMVC事务管理
- 配置Spring3+SpringMVC+Hibernate4
- springmvc+hibernate4+spring3整合
- SpringMVC+Spring3+Hibernate4框架搭建
- spring3,springMVC,Hibernate4整合开发
- Spring3.1.3 + Hibernate4 事务管理简单例子
- Spring3.1.3 + Hibernate4 事务管理简单例子
- Spring3与hibernate4集中式声明事务管理
- Spring3与hibernate4注解式声明事务管理
- Hibernate4+Spring3实现声明式事务管理
- SpringMVC+Spring3+Hibernate4开发环境搭建
- SpringMVC+Spring3.2+Hibernate4整合实例
- SpringMVC+Spring3+Hibernate4开发环境搭建
- Maven+Spring3+SpringMVC +Hibernate4+ Junit 单元测试
- SpringMVC+Spring3+Hibernate4的开发环境搭建
- SpringMVC+Spring3+Hibernate4开发环境搭建
- SpringMVC+Spring3+Hibernate4的开发环境搭建
- 搭建SpringMVC+Hibernate4+Spring3+Ajax+Maven项目
- xml文件的pull解析
- 在Ubuntu 14.10下安装Lua 5.2出错的解决
- coreJava面试题
- 用getstring提高ASP运行速度
- Virtual Box 无法创建64bit的虚拟机问题
- Hibernate4+Spring3+SpringMVC事务管理
- 三通道bgr图像直方图均衡化
- ubuntu下安装、卸载软件命令
- FastJson--阿里巴巴公司开源的速度最快的Json和对象转换工具
- const char*, char const*, char*const的区别
- Dijkstra(迪杰斯特拉算法)的Java完整版实现
- 重置oracle 11G的system、sys密码
- 一个类似Listview的效果
- GPRS模块发短信