zoukankan      html  css  js  c++  java
  • spring笔记4-事务管理

    一.xml配置文件形式
    通过转账案例,学习事务管理
    1.建立数据库
    2.编写entity

      1 package huguangqin.com.cnblogs.entity;
      2 
      3 public class User {
      4      private Integer id;
      5      private String name;
      6      private Double money;
      7 
      8     public User() {
      9          super();
     10      }
     11 
     12     public User(Integer id, String name, Double money) {
     13          super();
     14          this.id = id;
     15          this.name = name;
     16          this.money = money;
     17      }
     18 
     19     public Integer getId() {
     20          return id;
     21      }
     22 
     23     public void setId(Integer id) {
     24          this.id = id;
     25      }
     26 
     27     public String getName() {
     28          return name;
     29      }
     30 
     31     public void setName(String name) {
     32          this.name = name;
     33      }
     34 
     35     public Double getMoney() {
     36          return money;
     37      }
     38 
     39     public void setMoney(Double money) {
     40          this.money = money;
     41      }
     42 
     43 }
     44 


    3.编写dao

      1 package huguangqin.com.cnblogs.dao;
      2 
      3 public interface UserDao {
      4      void increasement(int id, double money);
      5 
      6     void decreasement(int id, double money);
      7  }
      8 


    4.编写daoImpl

      1 package huguangqin.com.cnblogs.daoImpl;
      2 
      3 import org.springframework.jdbc.core.support.JdbcDaoSupport;
      4 
      5 import huguangqin.com.cnblogs.dao.UserDao;
      6 
      7 public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
      8 
      9     @Override
     10      public void increasement(int id, double money) {
     11          String sql = "update t_bank set money=money+? where id = ?";
     12          getJdbcTemplate().update(sql, money, id);
     13      }
     14 
     15     @Override
     16      public void decreasement(int id, double money) {
     17          String sql = "update t_bank set money=money-? where id = ?";
     18          getJdbcTemplate().update(sql, money, id);
     19      }
     20 
     21 }
     22 


    5.编写service

      1 package huguangqin.com.cnblogs.service;
      2 
      3 public interface IUserService {
      4      void tranfer(int where, int to, double money);
      5  }
      6 


    6.编写serviceImpl

      1 package huguangqin.com.cnblogs.serviceImpl;
      2 
      3 import org.springframework.beans.factory.annotation.Autowired;
      4 
      5 import huguangqin.com.cnblogs.dao.UserDao;
      6  import huguangqin.com.cnblogs.service.IUserService;
      7 
      8 public class UserServiceImpl implements IUserService {
      9      //调用UserDao操作数据库,spring下调用接口,并注入实例对象
     10     @Autowired
     11      private UserDao ud;
     12 
     13     public void setUd(UserDao ud) {
     14          this.ud = ud;
     15      }
     16 
     17     @Override
     18      public void tranfer(int where, int to, double money) {
     19         ud.decreasement(where, money);
     20          ud.increasement(to, money);
     21      }
     22  }
     23 


    7.编写db.properties

      1 jdbc.driverClass=com.mysql.jdbc.Driver
      2 jdbc.url=jdbc:mysql:///spring_day02
      3 jdbc.user=root
      4 jdbc.password=root

    8.编写applicationContext.xml
      1 <?xml version="1.0" encoding="UTF-8"?>
      2 
      3 <beans
      4  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      5  xmlns="http://www.springframework.org/schema/beans"
      6  xmlns:context="http://www.springframework.org/schema/context"
      7  xmlns:aop="http://www.springframework.org/schema/aop"
      8  xmlns:tx="http://www.springframework.org/schema/tx"
      9  xsi:schemaLocation="http://www.springframework.org/schema/beans
     10                      http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
     11                      http://www.springframework.org/schema/context
     12                      http://www.springframework.org/schema/context/spring-context-4.2.xsd
     13                      http://www.springframework.org/schema/aop
     14                      http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
     15                      http://www.springframework.org/schema/tx
     16                      http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
     17 
     18  <!--读取db.propertiey  -->
     19  <context:property-placeholder location="classpath:db.properties"/>
     20 
     21 <!-- 配置连接池到spring容器 -->
     22  <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
     23      <property name="driverClass" value="${jdbc.driverClass}"></property>
     24      <property name="jdbcUrl" value="${jdbc.url}"></property>
     25      <property name="user" value="${jdbc.user}"></property>
     26      <property name="password" value="${jdbc.password}"></property>
     27  </bean>
     28 
     29 <!-- 配置核心事务管理器 -->
     30  <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     31      <property name="dataSource" ref="dataSource"></property>
     32  </bean>
     33 
     34 <!-- 配置事务管理通知 -->
     35  <tx:advice id="txAdvice" transaction-manager="transactionManager">
     36      <tx:attributes>
     37          <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
     38          <tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
     39          <tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
     40      </tx:attributes>
     41  </tx:advice>
     42 
     43 <!-- 配置织入 -->
     44  <aop:config>
     45      <aop:pointcut expression="execution(* huguangqin.com.cnblogs.serviceImpl.*ServiceImpl.*(..))" id="txPc"/>
     46  </aop:config>
     47 
     48 <!-- 配置dao -->
     49  <bean name="userDao" class="huguangqin.com.cnblogs.daoImpl.UserDaoImpl">
     50      <property name="dataSource" ref="dataSource"></property>
     51  </bean>
     52 
     53 <!-- 配置service -->
     54  <bean name="userService" class="huguangqin.com.cnblogs.serviceImpl.UserServiceImpl">
     55      <property name="ud" ref="userDao"></property>
     56  </bean>
     57  </beans>
     58 



    二.Annotation形式
    与xml相比需修改以下文件
    1.applicationContext.xml

      1 <?xml version="1.0" encoding="UTF-8"?>
      2 
      3 <beans
      4  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      5  xmlns="http://www.springframework.org/schema/beans"
      6  xmlns:context="http://www.springframework.org/schema/context"
      7  xmlns:aop="http://www.springframework.org/schema/aop"
      8  xmlns:tx="http://www.springframework.org/schema/tx"
      9  xsi:schemaLocation="http://www.springframework.org/schema/beans
     10                      http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
     11                     http://www.springframework.org/schema/context
     12                      http://www.springframework.org/schema/context/spring-context-4.2.xsd
     13                      http://www.springframework.org/schema/aop
     14                      http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
     15                      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
     16 
     17  <!--读取db.propertiey  -->
     18  <context:property-placeholder location="classpath:db.properties"/>
     19 
     20 <!-- 配置连接池到spring容器 -->
     21  <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
     22      <property name="driverClass" value="${jdbc.driverClass}"></property>
     23      <property name="jdbcUrl" value="${jdbc.url}"></property>
     24      <property name="user" value="${jdbc.user}"></property>
     25      <property name="password" value="${jdbc.password}"></property>
     26 </bean>
     27 
     28 <!-- 配置核心事务管理器 -->
     29  <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     30      <property name="dataSource" ref="dataSource"></property>
     31  </bean>
     32 
     33 <!--开启注解管理事务开关  -->
     34  <tx:annotation-driven transaction-manager="transactionManager"/>
     35 
     36 <!-- 配置dao -->
     37  <bean name="userDao" class="huguangqin.com.cnblogs.daoImpl.UserDaoImpl">
     38      <property name="dataSource" ref="dataSource"></property>
     39  </bean>
     40 
     41 <!-- 配置service -->
     42  <bean name="userService" class="huguangqin.com.cnblogs.serviceImpl.UserServiceImpl">
     43      <property name="ud" ref="userDao"></property>
     44  </bean>
     45  </beans>
     46 


    2.serviceImpl类

      1 package huguangqin.com.cnblogs.serviceImpl;
      2 
      3 import org.springframework.beans.factory.annotation.Autowired;
      4  import org.springframework.transaction.annotation.Transactional;
      5 
      6 import huguangqin.com.cnblogs.dao.UserDao;
      7  import huguangqin.com.cnblogs.service.IUserService;
      8 
      9 @Transactional
     10  public class UserServiceImpl implements IUserService {
     11      @Autowired
     12      private UserDao ud;
     13 
     14     public UserDao getUd() {
     15          return ud;
     16      }
     17 
     18     public void setUd(UserDao ud) {
     19          this.ud = ud;
     20      }
     21 
     22     @Override
     23      // @Transactional(isolation = Isolation.DEFAULT, propagation =Propagation.REQUIRED, readOnly = false)
     24      public void tranfer(int where, int to, double money) {
     25          ud.decreasement(where, money);
     26          ud.increasement(to, money);
     27      }
     28 
     29 }
     30 

  • 相关阅读:
    VC++基于ICMP 遍历路由表
    在XP上同时运行IE6,IE7,IE8,IE9
    VC++实现ARP协议
    在Windows Sever 2008 Windows7上同时运行绿色IE10测试HTML5
    VC++实践IOCP编程
    VC++实现检测网络状态所有的TCP与UDP通信
    VC++实现UDP数据包发送
    VC++实现改变网关
    面试十大难题的样板回答
    WinAPI: GetLocalTime、SetLocalTime、SetSystemTime 获取与设置系统时间
  • 原文地址:https://www.cnblogs.com/huguangqin/p/7488614.html
Copyright © 2011-2022 走看看