基于注解的spring事务管理@Transactionl

基于注解的spring事务管理@Transactionl


1. 项目布局图

项目布局图

2. 项目主要代码

User.java

package com.cmcc.bean;

public class User {

    private Integer id;
    private String name;

    public User() {
        super();
    }

    public User(Integer id, String name) {
        super();
        this.id = id;
        this.name = name;
    }


    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;
    }

}

UserDao.java

package com.cmcc.dao;

import com.cmcc.bean.User;

public interface UserDao {

    public void save(User user);
    public User getUser(Integer id);

}

UserDaoImpl.java

package com.cmcc.impl;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.cmcc.bean.User;
import com.cmcc.dao.UserDao;
import com.cmcc.row.MyRowMapper;

@Transactional
public class UserDaoImpl implements UserDao {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void save(User user){

        String sql = "insert into person(id, name) values(?,?)";
        Object[] params = new Object[]{
                user.getId(), user.getName()
        };
        int[] types = new int[]{
                java.sql.Types.INTEGER,java.sql.Types.VARCHAR
        };
        jdbcTemplate.update(sql, params, types);
        int i = 2/0;        //用来检测出现异常是否回滚
    }

    @Override
    public User getUser(Integer id) {

        String sql = "select * from person where id=?";
        Object[] params = new Object[]{
                id
        };
        int[] types = new int[]{
                java.sql.Types.INTEGER
        };
        User user = (User)jdbcTemplate.queryForObject(sql, 
                                            params, 
                                            types, 
                                            new MyRowMapper());

        return user;
    }

}

Test.java

package com.cmcc.junitTest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cmcc.bean.User;
import com.cmcc.dao.UserDao;

public class Test {

    @org.junit.Test
    public void test() {

    ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
    UserDao ud = (UserDao)ac.getBean("userDaoImpl");

    ud.save(new User(5, "Brown"));      //没有实现回滚,找原因?

    }

}

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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
           http://www.springframework.org/schema/mvc 
           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <tx:annotation-driven transaction-manager="txManager"/>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/itcast"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
    </bean>

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

    <bean id="txManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="userDaoImpl" class="com.cmcc.impl.UserDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>

</beans>

总结:

  • 要实现事务管理,只需要做到三点

    1. 在DaoImpl文件中类或方法上添加@Transactional
    2. 在配置文件中添加<tx:annotation-driven transaction-manager="txManager"/>,用来扫描文件中的事务管理的注解标记
    3. 在配置文件中添加
      <bean id="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource"/>
      </bean>
  • 用来测试的异常一般放在DaoImpl类中,而不是Test类中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值