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

}

 

你可能感兴趣的:(Spring注解,Spring注解解析)