有声明式事务管理和编程式事务管理,一般用声明式事务管理:结合dbcp和aop如:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scan base-package="com.fsj" /><!--自动扫描-->
<!-- 配置dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/s2shTest">
</property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 事务管理器 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 事务管理策略 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED" />
<tx:method name="*" rollback-for="Exception" />
</tx:attributes>
</tx:advice>
<!-- 切面,设置对哪些类进行事务管理 -->
<aop:config>
<aop:pointcut id="ptc"
expression="execution(* com.fsj.service.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="ptc" />
</aop:config>
</beans>
在dao层是采用JdbcTemplate:
package com.fsj.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.fsj.dao.UserDao;
import com.fsj.model.User;
@Repository("userDao")
public class UserDaoImpl implements UserDao {
private JdbcTemplate jdbcTemplate;
@Resource
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Override
public void delete(int id) {
System.out.println("delete"+id);
this.jdbcTemplate.update(
"delete from userinfo where id = ?",
new Object[] {id});
}
@Override
public User getById(int id) {
System.out.println("getById"+id);
User user = (User) this.jdbcTemplate.queryForObject(
"select * from userinfo where id = ?",
new Object[]{id},
new RowMapper() {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPwd(rs.getString("pwd"));
return user;
}
});
return user;
}
@Override
public void save(User user) {
System.out.println("save"+user.getName());
this.jdbcTemplate.update("insert into userinfo (name, pwd) values (?, ?)",
new Object[] {user.getName(), user.getPwd()});
}
@Override
public void update(User user) {
System.out.println("update"+user.getId());
this.jdbcTemplate.update(
"update userinfo set pwd = ? where id = ?",
new Object[] {user.getPwd(), user.getId()});
}
}
配合hibernate和注解方式,进行声明式事务管理
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scan base-package="com.fsj" /><!-- 打开自动扫描 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="file:src/hibernate.cfg.xml">
</property>
</bean>
<!-- 基于hibernate的事务管理器 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager"/><!-- 注解式事务 -->
</beans>
在需要事务管理的类上(一般是service层)使用注解:@Transactional进行事务管理
在dao层采用注入sessionFactory的时候初始化HibernateTemplate:
package com.fsj.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.fsj.dao.UserDao;
import com.fsj.model.Userinfo;
@Repository("userDao")
public class UserDaoImpl implements UserDao {
private HibernateTemplate hibernateTemplate;
@Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
@Override
public void delete(int id) {
hibernateTemplate.delete(hibernateTemplate.load(Userinfo.class, id));
}
@SuppressWarnings("unchecked")
@Override
public List<Userinfo> getAll() {
return (List<Userinfo>)hibernateTemplate.find("from Userinfo");
}
@Override
public Userinfo getByid(int id) {
return (Userinfo)hibernateTemplate.get(Userinfo.class, id);
}
@Override
public void save(Userinfo user) {
hibernateTemplate.persist(user);
}
@Override
public void update(Userinfo user) {
hibernateTemplate.merge(user);
}
}
分享到:
相关推荐
Spring事务管理Demo
Spring事务管理教程,详细讲解了Spring中的事务管理,包括声明式事务,注解式事务,以及事务配置等等
Spring事务管理.pdf 1.资料 2.本地事务与分布式事务 3.编程式模型 4.宣告式模型
Synchronized锁在Spring事务管理下,导致线程不安全。
spring 事务管理的理解
Spring事务管理(全),需要的可下载!
spring事务管理几种方式代码实例:涉及编程式事务,声明式事务之拦截器代理方式、AOP切面通知方式、AspectJ注解方式,通过不同方式实例代码展现,总结spring事务管理的一般规律,从宏观上加深理解spring事务管理特性...
在该JAR包的org.springframework.transaction包中,有3个接口文件PlatformTransactionManager、TransactionDefinition和TransactionStatus,如下图所示: Spring事务管理的三个核心接口 5.1.1 事务管理的核心接口 1....
Spring事务管理4种方式 入门级 最简单demo PlatformTransactionManager TransactionTemplate
Spring事务管理中所需要的jar包是 spring-tx-3.2.0.RELEASE.jar
Spring 事务管理,Spring 事务管理,Spring 事务管理,Spring 事务管理
详细介绍Spring事务管理
此包含有所有Spring事务管理开发所需要的Jar包,包含框架、核心、表达式、日志、编译、事务切面aop、数据库连接、单元测试、框架文本等jar包。
Spring事务管理A方法内部调用B方法的回滚问题测试代码