SpringMVC事务配置.docx

上传人:b****8 文档编号:10506567 上传时间:2023-02-17 格式:DOCX 页数:14 大小:19.28KB
下载 相关 举报
SpringMVC事务配置.docx_第1页
第1页 / 共14页
SpringMVC事务配置.docx_第2页
第2页 / 共14页
SpringMVC事务配置.docx_第3页
第3页 / 共14页
SpringMVC事务配置.docx_第4页
第4页 / 共14页
SpringMVC事务配置.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

SpringMVC事务配置.docx

《SpringMVC事务配置.docx》由会员分享,可在线阅读,更多相关《SpringMVC事务配置.docx(14页珍藏版)》请在冰豆网上搜索。

SpringMVC事务配置.docx

SpringMVC事务配置

SpringMVC事务配置

本文介绍两种配置方法:

一、XML,使用tx标签配置拦截器实现事务

一、Annotation方式

以下所使用环境为Spring4.0.3、Hibernate4.3.5

一、XML,使用tx标签配置拦截器实现事务

Entity类User.java,持久化类,对应数据库表user

packagecom.lei.demo.entity;

importjavax.persistence.*;

@Entity(name="users")

publicclassUsers{

publicUsers(){

super();

}

@Id

@GeneratedValue(strategy=GenerationType.AUTO)

@Column(name="id")

privateIntegerid;

@Column(name="user_name",length=32)

privateStringuser_name;

@Column(name="age")

privateIntegerage;

@Column(name="nice_name",length=32)

privateStringnice_name;

//属性实现......

}

UserDAO.javar,表user的一些操作,其中属性sessionFactory应该由Spring注入,如下:

packagecom.lei.demo.dao;

importjava.util.List;

importjavax.annotation.Resource;

importorg.hibernate.Query;

importorg.hibernate.Session;

importorg.hibernate.SessionFactory;

importorg.springframework.stereotype.Repository;

importcom.lei.demo.entity.Users;

publicclassUsersDAO{

privateSessionFactorysessionFactory;

publicvoidsetSessionFactory(SessionFactorysessionFactory){

this.sessionFactory=sessionFactory;

}

publicSessionFactorygetSessionFactory(){

returnsessionFactory;

}

publicListgetAllUser(){

Stringhsql="fromusers";

Sessionsession=sessionFactory.getCurrentSession();

Queryquery=session.createQuery(hsql);

returnquery.list();

}

}

UserService.java,业务实现类,如下

packagecom.lei.demo.service;

importjavax.annotation.Resource;

importorg.springframework.stereotype.Service;

importorg.springframework.transaction.annotation.Isolation;

importorg.springframework.transaction.annotation.Propagation;

importorg.springframework.transaction.annotation.Transactional;

importcom.lei.demo.dao.*;

publicclassUserService{

privateUsersDAOuserDao;

publicintuserCount(){

returnuserDao.getAllUser().size();

}

publicUsersDAOgetUserDao(){

returnuserDao;

}

publicvoidsetUserDao(UsersDAOuserDao){

this.userDao=userDao;

}

}

首先看一下xml配置,spring-hibernate.xml如下:

xmlversion="1.0"encoding="UTF-8"?

>

//www.springframework.org/schema/beans"

xmlns:

context="http:

//www.springframework.org/schema/context"

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xmlns:

tx="http:

//www.springframework.org/schema/tx"

xmlns:

aop="http:

//www.springframework.org/schema/aop"

xsi:

schemaLocation="

http:

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/beans/spring-beans-4.0.xsd

http:

//www.springframework.org/schema/context

http:

//www.springframework.org/schema/context/spring-context-4.0.xsd

http:

//www.springframework.org/schema/aop

http:

//www.springframework.org/schema/aop/spring-aop-4.0.xsd

http:

//www.springframework.org/schema/tx

http:

//www.springframework.org/schema/tx/spring-tx-4.0.xsd

">

--Hibernate4-->

--加载资源文件其中包含变量信息,必须在Spring配置文件的最前面加载,即第一个加载-->

property-placeholderlocation="classpath:

persistence-mysql.properties"/>

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

--可以加多个包-->

com.lei.demo.entity

${hibernate.hbm2ddl.auto}

${hibernate.dialect}

${hibernate.show_sql}

--thread-->

--数据库映射-->

--配置Hibernate事务管理器-->

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

--配置事务异常封装-->

class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

--声明式容器事务管理,transaction-manager指定事务管理器为transactionManager-->

adviceid="txAdvice"transaction-manager="transactionManager">

attributes>

methodname="add*"propagation="REQUIRED"/>

methodname="get*"propagation="REQUIRED"/>

methodname="*"read-only="true"/>

attributes>

advice>

configexpose-proxy="true">

--只对业务逻辑层实施事务-->

pointcutid="txPointcut"expression="execution(*com.lei.demo.service..*.*(..))"/>

--Advisor定义,切入点和通知分别为txPointcut、txAdvice-->

advisorpointcut-ref="txPointcut"advice-ref="txAdvice"/>

config>

其中主要配置中是tx:

advice和aop:

config两个配置节,以SpringAOP的方式实现事务管理。

tx:

advice配置了事务的管理者是transactionManager,同时tx:

method也规定了如果方法名匹配“add*”和“get*”方法时使用事务,propagation是设定事务的传播级别。

除了“add*”和“get*”方法,其他的方法的事务是只读的(典型地,对于只执行查询的事务你会将该属性设为true,如果出现了更新、插入或是删除语句时只读事务就会失败)

aop:

config指定了一个aop:

pointcut去引用上边的advice。

这样就通过AOP的拦截机制实现了事务,当然你还要用Spring的方式自己配置UserDAO和UserService。

二、Annotation方式

第一步,首先看一下web.xml,如下:

xmlversion="1.0"encoding="UTF-8"?

>

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xmlns="

xmlns:

web="

xsi:

schemaLocation="

id="WebApp_ID"version="3.0">

ArchetypeCreatedWebApplication

contextConfigLocation

classpath:

/spring-*.xml

org.springframework.web.context.ContextLoaderListener

lei-dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:

/lei-dispatcher-servlet.xml

1

lei-dispatcher

/

第二步,spring-hibernate配置,见以下spring-hibernate.xml配置

xmlversion="1.0"encoding="UTF-8"?

>

//www.springframework.org/schema/beans"

xmlns:

context="http:

//www.springframework.org/schema/context"

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xmlns:

tx="http:

//www.springframework.org/schema/tx"

xmlns:

aop="http:

//www.springframework.org/schema/aop"

xsi:

schemaLocation="

http:

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/beans/spring-beans-4.0.xsd

http:

//www.springframework.org/schema/context

http:

//www.springframework.org/schema/context/spring-context-4.0.xsd

http:

//www.springframework.org/schema/aop

http:

//www.springframework.org/schema/aop/spring-aop-4.0.xsd

http:

//www.springframework.org/schema/tx

http:

//www.springframework.org/schema/tx/spring-tx-4.0.xsd

">

--Hibernate4-->

--加载资源文件其中包含变量信息,必须在Spring配置文件的最前面加载,即第一个加载-->

property-placeholderlocation="classpath:

persistence-mysql.properties"/>

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

--可以加多个包-->

com.lei.demo.entity

${hibernate.hbm2ddl.auto}

${hibernate.dialect}

${hibernate.show_sql}

--thread-->

--数据库映射-->

--class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"-->

--class="org.springframework.jdbc.datasource.DriverManagerDataSource"-->

--配置Hibernate事务管理器-->

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

--配置事务异常封装-->

class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

第一节中xml配置事务中需要通过配置tx:

advice和aop:

config来增加事务的功能。

此处采用全注释方法,这两个配置节就不需要了。

相应的需要在视图解析配置中启用注释,如下lei-dispatcher-servlet.xml

xmlversion="1.0"encoding="UTF-8"?

>

//www.springframework.org/schema/beans"

xmlns:

context="http:

//www.springframework.org/schema/context"

xmlns:

mvc="http:

//www.springframework.org/schema/mvc"

xmlns:

p="http:

//www.springframework.org/schema/p"

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xmlns:

tx="http:

//www.springframework.org/schema/tx"

xsi:

schemaLocation="

http:

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/beans/spring-beans-3.0.xsd

http:

//www.springframework.org/schema/context

http:

//www.springframework.org/schema/context/spring-context-3.0.xsd

http:

//www.springframework.org/schema/mvc

http:

//www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

http:

//www.springframework.org/schema/tx

http:

//www.springframework.org/schema/tx/spring-tx-4.0.xsd

">

--启动自动扫描该包下所有的Bean(@Controller)-->

component-scanbase-package="com.lei.demo"/>

--基于注释的事务,当注释中发现@Transactional时,使用id为“transactionManager”的事务管理器-->

--如果没有设置transaction-manager的值,则spring以缺省默认的事务管理器来处理事务,默认事务管理器为第一个加载的事务管理器-->

annotation-driventransaction-manager="transactionManager"/>

--定义视图解析器-->

/WEB-

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 管理学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1