1**、事务添加到 JavaEE 三层结构里面 Service 层(业务逻辑层)。
2、在 Spring **进行事务管理操作。
(**1**)有两种方式:编程式事务管理和声明式事务管理(使用)。
3**、声明式事务管理
(1)基于注解方式(使用)。
(2)基于 xml 配置文件方式。
4、在 Spring 进行声明式事务管理,底层使用 AOP **原理。
5**、Spring 事务管理 **API
(1)提供一个接口,代表事务管理器,这个接口针对不同的框架提供不同的实现类。
6**、在 service 类上面(或者 service 类里面方法上面)添加事务注解
(1)@Transactional,这个注解添加到类上面,也可以添加方法上面
(2)如果把这个注解添加类上面,这个类里面所有的方法都添加事务
(3)如果把这个注解添加方法上面,为这个方法添加事务**
package com.wzy.shiwu.service.impl;import com.wzy.shiwu.dao.UserDao;import com.wzy.shiwu.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;@Service@Transactional//事务的注解public class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;@Overridepublic void zhangsanSubtract(Integer money, String name) {userDao.zhangsanMoney(money, name);}@Overridepublic void lisiMoneyAdd(Integer money, String name) {userDao.lisiMoney(money,name);}}
