Spring事务管理transactionManager

bean.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/context
        http://www.springframework.org/schema/context/spring-context.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">
    <!--配置事务管理器-->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
        <property ref="dataSource" name="dataSource"/>
    </bean>
    <!--开启事务注解-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    <!--mysql-->
    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    <!--service-->
    <bean class="com.war.service.Service" id="service">
        <property name="dao" ref="dao"></property>
    </bean>
    <!--dao-->
    <bean class="com.war.dao.DaoImpl" id="dao">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    <!--jdbcTemplate-->
    <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

</beans>

DaoImpl.java

package com.war.dao;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * Created by Administrator on 2017/6/21.
 */
public class DaoImpl implements Dao {
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    @Override
    public String addMoney() {
        String sql = "update account set money =money + ? where name = ?";
        jdbcTemplate.update(sql,1000,"tom");
        System.out.println("add over");
        return null;
    }

    @Override
    public String reduceMoney() {
        String sql = "update account set money = money - ? where name = ?";
        jdbcTemplate.update(sql,1000,"tim");
        System.out.println("reduce over");

        return null;
    }
}

Service.java

package com.war.service;

import com.war.dao.DaoImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Administrator on 2017/6/21.
 */

@Transactional
public class Service {
    private DaoImpl dao;

    public DaoImpl getDao() {
        return dao;
    }

    public void setDao(DaoImpl dao) {
        this.dao = dao;
    }
    public void  TransferAccounts(){
        dao.reduceMoney();
//        int a = 10/0;
        dao.addMoney();
        System.out.println("over");
    }
}

TestTransaction.java

package com.war;

import com.war.service.Service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Administrator on 2017/6/21.
 */
public class TestTransaction {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        Service service = (Service) ac.getBean("service");
        service.TransferAccounts();
    }
}
posted @ 2017-06-21 16:30  Mu_gua  阅读(312)  评论(0)    收藏  举报