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

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章