spring对jdbc事务控制

pojo类

package com.pactera.pojo;

public class User {

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    private String name;
    private int age;

    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + "]";
    }
}

IUserDao

package com.pactera.dao;

import com.pactera.pojo.User;

public interface IUserDao {

    void save(User user);
}

UserDao

package com.pactera.daoImpl;


import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.pactera.dao.IUserDao;
import com.pactera.pojo.User;

@Repository
public class UserDao implements IUserDao{

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public void save(User user) {
        String sql = "insert into user(name,age) values(?,?)";
        jdbcTemplate.update(sql,user.getName(),user.getAge());
    }


}

IUserService

package com.pactera.service;

import com.pactera.pojo.User;

public interface IUserService {

    void save(User user);
}

UserService

package com.pactera.serviceImpl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.pactera.dao.IUserDao;
import com.pactera.pojo.User;
import com.pactera.service.IUserService;

@Service
public class UserService implements IUserService{

    @Resource
    private IUserDao userDao;
    @Override
    public void save(User user) {
        userDao.save(user);
        int i=1/0;
        userDao.save(user);
    }

}

applicationContext.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:p="http://www.springframework.org/schema/p"
    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 id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" 
            value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8">
        </property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        <property name="initialPoolSize" value="3"></property>
        <property name="maxPoolSize" value="50"></property>
        <property name="acquireIncrement" value="5"></property>
    </bean>

    <!-- 创建jdbcTemplate对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.pactera"></context:component-scan>

    <!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 声明式事务 -->
    <tx:advice id="txAdvice">
        <tx:attributes>
            <tx:method name="query" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!-- aop事务切面 -->
    <aop:config>
        <aop:pointcut id="myPointcut" expression="execution(* com.pactera.service.*.*(..))" />
        <!--将定义好的事务处理策略应用到上述的切入点 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />
    </aop:config>


</beans>   

测试

private IUserService userService;
    @Before
    public void init(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        this.userService = ac.getBean("userService",IUserService.class);
    }

    @Test
    public void test(){
        User user = new User();
        user.setName("hello");
        user.setAge(15);
        userService.save(user);
    }

配 有jdbc事务控制的时候service层中出现异常,再抛出异常之前对数据库的操作会回滚

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值