Spring声明式事务注解之环境搭建

1. 环境搭建步骤

  • 导入依赖,数据源、数据库驱动和Spring-jdbc模块;
  • 编写测试代码,操作数据库
  • 给方法标记 @Transactional,表示是一个事务方法;
  • 使用@EnableTransactionManagement 开启基于注解的事务管理功能;
  • 配置事务管理器来控制事务;
     

2. 测试代码

  • 2.1 测试Bean
package com.yibai.spring.annotation.tx.service.bean;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@ToString
public class Person {
	private int id;
	private String name;
	private int age;
	private String address;
}
  • 2.2 测试DAO
package com.yibai.spring.annotation.tx.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.yibai.spring.annotation.tx.service.bean.Person;

@Repository
public class PersonDao {

	@Autowired
	private JdbcTemplate jdbc;

	public int insertPerson(Person person) {
		String sql = "INSERT INTO person(name,age,address) values(?,?,?)";
		return jdbc.update(sql, person.getName(), person.getAge(), person.getAddress());
	}
}
  • 2.3 测试Service
package com.yibai.spring.annotation.tx.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yibai.spring.annotation.tx.service.bean.Person;

@Service
public class PersonService {

	@Autowired
	private PersonDao userDao;

	@Transactional
	public int savePerson(Person person) {
		int insert = userDao.insertPerson(person);
		if (insert == 1) {
			System.out.println("插入成功");
		}

//		int a = 1 / 0;

		return insert;
	}
}
  • 2.4 主配置类
package com.yibai.spring.annotation.tx;

import javax.sql.DataSource;

import org.postgresql.Driver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.pool.DruidDataSource;

@ComponentScan(basePackages = "com.yibai.spring.annotation.tx")
@EnableTransactionManagement
public class TxConfig {

	@Bean // 数据源
	public DataSource dataSource() {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setUsername("postgres");
		dataSource.setPassword("123123");
		dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/postgres");
		dataSource.setDriverClassName(Driver.class.getName());
		return dataSource;
	}

	@Bean
	public JdbcTemplate jdbcTemplate(DataSource dataSource) {
		return new JdbcTemplate(dataSource);
	}

	@Bean //事务管理器
	public PlatformTransactionManager platformTransactionManager(DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

}
  • 2.5 启动类
package com.yibai.spring.annotation.tx;

import java.util.List;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.yibai.spring.annotation.tx.service.PersonService;
import com.yibai.spring.annotation.tx.service.bean.Person;

public class MainClass {

	public static void main(String[] args) {

		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(TxConfig.class);

		PersonService personService = applicationContext.getBean(PersonService.class);
		Person person = new Person();
		person.setName("yibai");
		person.setAge(1000);
		person.setAddress("zhejiang hangz");
		try {
			personService.savePerson(person);
		} catch (Exception e) {
			e.printStackTrace();
		}

		applicationContext.close();
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值