pack pointcut interface lns exe framework exec drive
一.xml配置文件形式
通过转账案例,学习事务管理
1.建立数据库
2.编写entity
- package huguangqin.com.cnblogs.entity;
- public class User {
- private Integer id;
- private String name;
- private Double money;
- public User() {
- super();
- }
- public User(Integer id, String name, Double money) {
- super();
- this.id = id;
- this.name = name;
- this.money = money;
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Double getMoney() {
- return money;
- }
- public void setMoney(Double money) {
- this.money = money;
- }
- }
3.编写dao
- package huguangqin.com.cnblogs.dao;
- public interface UserDao {
- void increasement(int id, double money);
- void decreasement(int id, double money);
- }
4.编写daoImpl
- package huguangqin.com.cnblogs.daoImpl;
- import org.springframework.jdbc.core.support.JdbcDaoSupport;
- import huguangqin.com.cnblogs.dao.UserDao;
- public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
- @Override
- public void increasement(int id, double money) {
- String sql = "update t_bank set money=money+? where id = ?";
- getJdbcTemplate().update(sql, money, id);
- }
- @Override
- public void decreasement(int id, double money) {
- String sql = "update t_bank set money=money-? where id = ?";
- getJdbcTemplate().update(sql, money, id);
- }
- }
5.编写service
- package huguangqin.com.cnblogs.service;
- public interface IUserService {
- void tranfer(int where, int to, double money);
- }
6.编写serviceImpl
- package huguangqin.com.cnblogs.serviceImpl;
- import org.springframework.beans.factory.annotation.Autowired;
- import huguangqin.com.cnblogs.dao.UserDao;
- import huguangqin.com.cnblogs.service.IUserService;
- public class UserServiceImpl implements IUserService {
- //调用UserDao操作数据库,spring下调用接口,并注入实例对象
- @Autowired
- private UserDao ud;
- public void setUd(UserDao ud) {
- this.ud = ud;
- }
- @Override
- public void tranfer(int where, int to, double money) {
- ud.decreasement(where, money);
- ud.increasement(to, money);
- }
- }
7.编写db.properties
- jdbc.driverClass=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql:///spring_day02
- jdbc.user=root
- jdbc.password=root
8.编写applicationContext.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
- 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-4.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.2.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
- <!--读取db.propertiey -->
- <context:property-placeholder location="classpath:db.properties" />
- <!-- 配置连接池到spring容器 -->
- <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="${jdbc.driverClass}">
- </property>
- <property name="jdbcUrl" value="${jdbc.url}">
- </property>
- <property name="user" value="${jdbc.user}">
- </property>
- <property name="password" value="${jdbc.password}">
- </property>
- </bean>
- <!-- 配置核心事务管理器 -->
- <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource">
- </property>
- </bean>
- <!-- 配置事务管理通知 -->
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"
- />
- <tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"
- />
- <tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"
- />
- </tx:attributes>
- </tx:advice>
- <!-- 配置织入 -->
- <aop:config>
- <aop:pointcut expression="execution(* huguangqin.com.cnblogs.serviceImpl.*ServiceImpl.*(..))"
- id="txPc" />
- </aop:config>
- <!-- 配置dao -->
- <bean name="userDao" class="huguangqin.com.cnblogs.daoImpl.UserDaoImpl">
- <property name="dataSource" ref="dataSource">
- </property>
- </bean>
- <!-- 配置service -->
- <bean name="userService" class="huguangqin.com.cnblogs.serviceImpl.UserServiceImpl">
- <property name="ud" ref="userDao">
- </property>
- </bean>
- </beans>
二.Annotation形式
与xml相比需修改以下文件
1.applicationContext.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
- 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-4.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.2.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
- <!--读取db.propertiey -->
- <context:property-placeholder location="classpath:db.properties" />
- <!-- 配置连接池到spring容器 -->
- <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="${jdbc.driverClass}">
- </property>
- <property name="jdbcUrl" value="${jdbc.url}">
- </property>
- <property name="user" value="${jdbc.user}">
- </property>
- <property name="password" value="${jdbc.password}">
- </property>
- </bean>
- <!-- 配置核心事务管理器 -->
- <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource">
- </property>
- </bean>
- <!--开启注解管理事务开关 -->
- <tx:annotation-driven transaction-manager="transactionManager" />
- <!-- 配置dao -->
- <bean name="userDao" class="huguangqin.com.cnblogs.daoImpl.UserDaoImpl">
- <property name="dataSource" ref="dataSource">
- </property>
- </bean>
- <!-- 配置service -->
- <bean name="userService" class="huguangqin.com.cnblogs.serviceImpl.UserServiceImpl">
- <property name="ud" ref="userDao">
- </property>
- </bean>
- </beans>
2.serviceImpl类
- package huguangqin.com.cnblogs.serviceImpl;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.transaction.annotation.Transactional;
- import huguangqin.com.cnblogs.dao.UserDao;
- import huguangqin.com.cnblogs.service.IUserService;
- @Transactional
- public class UserServiceImpl implements IUserService {
- @Autowired
- private UserDao ud;
- public UserDao getUd() {
- return ud;
- }
- public void setUd(UserDao ud) {
- this.ud = ud;
- }
- @Override
- // @Transactional(isolation = Isolation.DEFAULT, propagation =Propagation.REQUIRED, readOnly = false)
- public void tranfer(int where, int to, double money) {
- ud.decreasement(where, money);
- ud.increasement(to, money);
- }
- }
spring笔记4-事务管理
来源: http://www.bubuko.com/infodetail-2295853.html