spring事务-xml+注解组合实现

本文详细介绍了如何在Spring框架中使用XML配置和注解实现事务管理,包括数据源配置、JdbcTemplate使用、声明式事务及异常处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

beans.xml

<?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.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


	<!-- Spring 读取db.properties -->
	<context:property-placeholder location="classpath:db.properties" />
	<context:component-scan base-package="com.liuyun"></context:component-scan>

	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<!-- spring-jdbc内置数据源 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driverClass}"></property>
		<property name="url" value="${jdbc.jdbcUrl}"></property>
		<property name="username" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>

	<!-- 声明式事务 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

关键代码:
在这里插入图片描述
配合注解的使用:UserServiceImpl.java

/*
 * Copyright (c) 2019, wenwenliuyun@163.com All Rights Reserved. 
 */

package com.liuyun.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.liuyun.dao.IUserDao;
import com.liuyun.domain.User;
import com.liuyun.service.IUserService;

/**
 * Function: 业务实现类 <br>
 *
 * @author liuyun
 * @version
 * @since 2019年8月29日下午7:43:54
 */
@Service("userService")
@Transactional // 默认值propagation = Propagation.REQUIRED, readOnly = false
public class UserServiceImpl implements IUserService {

	@Resource
	private IUserDao userDao;

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public long count() throws Exception {
		System.out.println("Service的count执行");
		return userDao.count();
	}

	@Override
	public int deleteById(Integer id) throws Exception {
		return userDao.deleteById(id);
	}

	@Override
	public int insert(User record) throws Exception {
		int count = userDao.insert(record);
		int i = 1/0;// 验证事务回滚
		return count;
	}

	@Override
	public int update(Integer id, User record) throws Exception {
		if (id == null || record == null) {
			throw new RuntimeException("update input params cannot be null...");
		}
		record.setId(id);
		return userDao.update(record);
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public List<User> findAll() throws Exception {
		return userDao.findAll();
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public User findById(Integer id) throws Exception {
		return userDao.findById(id);
	}
}

关键代码:修饰类和方法,指定事务控制
在这里插入图片描述
完整代码:https://github.com/liuyunHappy/spring-tx-xml-annotation.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值