使用spring jdbcTemplate和dbcp操作数据库及事务配置

代码结构及jar包

使用spring jdbcTemplate和dbcp操作数据库及事务配置_第1张图片

 

ServiceClass.java

package com.orange.test;

import java.sql.Types;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.management.RuntimeErrorException;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional	//增加事务
public class ServiceClass {
	@Autowired	//注入dataSource属性
	private DataSource dataSource;
	private JdbcTemplate jdbcTemplate;	
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	//实例化ServiceClass类后执行init()方法初始化jdbcTemplate
	@PostConstruct  
	public void init(){
		System.out.println("初始化jdbcTemplate");
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	@Transactional(propagation=Propagation.REQUIRED)	//默认事务,可以不写
	public void save(){
		jdbcTemplate.update("insert into spring (name) value(?)", new Object[]{"sa"},new int[]{	Types.VARCHAR});
	}
	@Transactional(propagation=Propagation.REQUIRED)	//默认事务,可以不写
	public void update(){
		jdbcTemplate.update("update spring set name = ? where id = ?", new Object[]{"A",1},new int[]{	Types.VARCHAR,Types.INTEGER});
	}
	/**
	 * 默认只对unchecked异常有效 
	 * Error和RuntimeException及其子类成为未检查异常(unchecked)
	 * 其它异常成为已检查异常(checked)
	 */
	@Transactional(rollbackFor=Exception.class)
	public void delete() throws Exception{
		jdbcTemplate.update("delete from spring where id = ?", new Object[]{11},new int[]{Types.INTEGER});
//		System.out.println(1/0);	//运行异常
		throw new Exception("异常");
	}
	public String getData(){
		return (String)jdbcTemplate.queryForObject("select * from spring where id = ?", new Object[]{1},new int[]{Types.INTEGER}, new TestRowMapper());
	}
	/**
	 * 第一方法种查询所有
	 */
	@Transactional(propagation=Propagation.NOT_SUPPORTED)	//不启用事务
	public List getAll(){
		return (List)jdbcTemplate.query("select * from spring ",new TestRowMapper());
	}
	/**
	 * 第二方法种查询所有
	 */
	@Transactional(propagation=Propagation.NOT_SUPPORTED)	//不启用事务
	public List getAllList(){
		return (List)jdbcTemplate.queryForList("select * from spring ");
	}
}


TestRowMapper.java

package com.orange.test;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

public class TestRowMapper implements RowMapper{

	public Object mapRow(ResultSet rs, int arg1) throws SQLException {
		String name = rs.getString("name");
		return name;
	}
}


Test.java

package com.orange.test;

import java.util.List;

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

public class Test {

	public static void main(String[] args) {
		ApplicationContext context1 = new ClassPathXmlApplicationContext("beans.xml");
		ServiceClass service= (ServiceClass)context1.getBean("serviceClass");
//		service.save();
		List list =  service.getAll();		
		System.out.println(list);
		List list2 =  service.getAllList();
		System.out.println(list2);
		//执行删除
		try {
			service.delete();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		list =  service.getAll();
//		System.out.println(list);
	}
}


 

beans.xml



	
	        
     
	
	
	 
	 
  	 
	    
	    
	    
	    
	     
		  
		  
		  
		  
		  
		  
		  
		  
		  
		  
		  
		  
		 
		 
		  
		SELECT 1  
		  
	 
	 
	 
	 
  	   
    
    
    


jdbc.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF-8
username=root
password=123456
以上是使用注解的方式配置事务,如要要使用xml配置只需在beans.xml增加:

    
	  	
	  	
	 
	
		  
		  
		    
		    
		    
		  
	



 

你可能感兴趣的:(Spring,Java)