spring第四天基于xml和注解的声明式事务配置

 spring基于xml文件和注解组合使用的声明式事务

相比于和xml文件声明式事务,配置比较简单灵活 

第一步.配置事务管理器 

   
   
   

第二步、配置spring开启注解事务的支持

   

第三步 在需要事务的地方使用@transactional的注解

        该注解可以写接口上、类上、方法上
        写在接口上,表示该接口上的所有实现类都有事务
        写在类上:表示该类的所有方法都有事务
        写在方法上 表示该方法有事务     
        级别:就近原则  方法   优先于 类 优先于接口  

//@Transactional   实际中可直接使用这个   默认是读写型 
//@Transactional  含有多个属性 
@Transactional(propagation=Propagation.REQUIRED,readOnly=false)   //读写型     默认使用读写型  会对性能有一定的影响    一般在有增删改的类上使用读写型的事务配置
//@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)   //只读型    配置为只读对增删改操作没有影响  事务不会回滚   

1.导包

spring第四天基于xml和注解的声明式事务配置_第1张图片

数据库

spring第四天基于xml和注解的声明式事务配置_第2张图片

业务层及实现类 

com.pro.service.IAcountService

package com.pro.service;

import com.pro.domain.Acount;



//账户的业务实现层
public interface IAcountService {
	
	//根据地查询账户 信息
	Acount findByAcount(Integer id);
	
	
	//转账业务 
	/*
	sourcename 转出账号  
	targetname 转入账号
	money  转账金额*/
	void transfer(String sourcename,String targetname,double money);
		
}

com.pro.service.impl.IAcountServiceImpl

package com.pro.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.pro.dao.IAcountDao;
import com.pro.domain.Acount;
import com.pro.service.IAcountService;


@Service("iacountservice")
//@Transactional
//@Transactional  含有多个属性 
@Transactional(propagation=Propagation.REQUIRED,readOnly=false)   //读写型     默认使用读写型  会对性能有一定的影响    一般在有增删改的类上使用读写型的事务配置
//@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)   //只读型    配置为只读对增删改操作没有影响  事务不会回滚   
public class IAcountServiceImpl implements IAcountService{
	@Resource(name="acountdao")
	private IAcountDao acountdao;
	@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
	public Acount findByAcount(Integer id) {
		// TODO Auto-generated method stub
		return acountdao.findByAcount(id);
	}

	public void transfer(String sourcename, String targetname, double money) {
		//1根据账户查询账户信息 
		Acount source = acountdao.findByName(sourcename);
		Acount target = acountdao.findByName(targetname);
		
		//2转账
		source.setMoney(source.getMoney()-money);
		target.setMoney(target.getMoney()+money);
		
		//更新账户信息
		
		acountdao.updateAcount(source);
		int i=1/0;
		acountdao.updateAcount(target);
		
		
	}

}

持久层及实现类

com.pro.dao.IAcountDao

package com.pro.dao;

import com.pro.domain.Acount;

public interface IAcountDao {

	Acount findByAcount(Integer id);

	Acount findByName(String sourcename);

	void updateAcount(Acount source);

}

com.pro.dao.IAcountDaoImpl

package com.pro.dao;

import java.util.List;

import javax.annotation.Resource;

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

import com.pro.domain.Acount;
@Repository("acountdao")
public class IAcountDaoImpl implements IAcountDao {
	
	@Resource(name="jdbctemplete")
	private JdbcTemplate jdbctemplete;

	@Override
	public Acount findByAcount(Integer id) {
		// TODO Auto-generated method stub
		List list = jdbctemplete.query("select * from acount where id=?", new BeanPropertyRowMapper(Acount.class), id);
		return list.isEmpty()?null:list.get(0);
	}

	@Override
	public Acount findByName(String sourcename) {
		// TODO Auto-generated method stub
		List list = jdbctemplete.query("select * from acount where name=?", new BeanPropertyRowMapper(Acount.class), sourcename);
		return list.isEmpty()?null:list.get(0);
	}

	@Override
	public void updateAcount(Acount source) {
		// TODO Auto-generated method stub
		jdbctemplete.update("update acount set money=? where name=?",source.getMoney(),source.getName());
	}

}

javabean

com.pro.domain.Acount

package com.pro.domain;

public class Acount {
	private int id;
	private String name;
	private double money;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	@Override
	public String toString() {
		return "Acount [id=" + id + ", name=" + name + ", money=" + money + "]";
	}
	
	
}

xml文件


 
        					
        					
        					
    
	
	    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
	
	

测试类:

com.pro.UI.Client

package com.pro.UI;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.pro.domain.Acount;
import com.pro.service.IAcountService;

public class Client {
	
	
		@Test
		public void test1(){
			ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
			IAcountService acount = (IAcountService) ac.getBean("iacountservice");
			
			Acount a=  acount.findByAcount(1);
			System.out.println(a);
			
		}
		
		@Test
		public void test2(){
			ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
			IAcountService acount = (IAcountService) ac.getBean("iacountservice");
			
			acount.transfer("xiao","zhang", 100);
			
		}
	
	
}

 

 

 

 

 

你可能感兴趣的:(java,spring)