day03-spring与数据库操作的框架解析即dataSource的获取(JDBC)、使用spring中已经有的事务进行JDBC操作(一)

2014-11-23 23:26:19 · 作者: · 浏览: 2

数据库的操作:
固定的代码(模板,datasource的获取)+动态的参数(变化的SQL语句、参数等等)
模板模式的编程
代码的结构的解析:
JdbcTemplate extends JdbcAccessor
JdbcTemplate(){

}
JdbcTemplate(DataSource dataSource){
this.dataSource = dataSource;
}
setDataSource(DataSource dataSource){
this.dataSource = dataSource;
}



public abstract class JdbcDaoSupport extends DaoSupport {
private JdbcTemplate jdbcTemplate;
public final void setDataSource(DataSource dataSource) {
if (this.jdbcTemplate == null || dataSource != this.jdbcTemplate.getDataSource()) {
this.jdbcTemplate = createJdbcTemplate(dataSource);
initTemplateConfig();
}
}
protected JdbcTemplate createJdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
public final DataSource getDataSource() {
return (this.jdbcTemplate != null this.jdbcTemplate.getDataSource() : null);
}
public final void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
initTemplateConfig();
}
public final JdbcTemplate getJdbcTemplate() {
return this.jdbcTemplate;
}
}
说明:
1、在JdbcDaoSupport中可以注入dataSource
2、在JdbcDaoSupport中注入的dataSource通过createJdbcTemplate方法
传入到JdbcTemplate
3、可以通过JdbcDaoSupport得到JdbcTemplate的引用
HibernateTemplate
SqlMapClientTemplate
JdoTemplate

spring结合jdbc的写法:共七种写法
1、
类的写法:
PersonDao extends JdbcDaoSupport
配置文件:
1、在PersonDao中注入DataSource
2、在PersonDao中注入JdbcTemplate
在JdbcTemplate中注入dataSource
1、可以利用构造器的形式
2、可以利用setter的方式
2、
类的写法
PersonDao{
private JdbcTemplate jdbcTemplate;
}
配置文件:
在PersonDao中注入JdbcTemplate
在JdbcTemplate中注入dataSource
1、可以利用构造器的形式
2、可以利用setter的方式
3、
类的写法
PersonDao extends JdbcTemplate{

}
配置文件:
利用构造器或者setter方法注入dataSource

总结:
无论什么样的形式,最终是把dataSource传入到了jdbcTemplate中,只要有了数据源,就可以进行一系列crud操作。


package com.itheima.dao;

public interface PersonDao {
	void savePerson();
}


package com.itheima.dao.impl;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.itheima.dao.PersonDao;

public class PersonDaoImpl extends JdbcDaoSupport implements PersonDao {

	@Override
	public void savePerson() {
		// TODO Auto-generated method stub
		this.getJdbcTemplate().execute("insert into person(name) values('ccc')");
	}

}

package com.itheima.service;

public interface PersonService {
	void savePerson();
}


package com.itheima.service.impl;

import com.itheima.dao.PersonDao;
import com.itheima.service.PersonService;

public class PersonServiceImpl implements PersonService {
	private PersonDao personDao;
	
	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	@Override
	public void savePerson() {
		/**
		 * 如果有事务,则以下操作将不会执行,都不会成功。因为有异常 。
		 * 要么都成功。
		 */
		this.personDao.savePerson();
		//int i = 1/0;
		this.personDao.savePerson();
	}

}


spring的配置文件applicationContext.xml:


  

  
	
    
     
     
      classpath:jdbc.properties
      
     
   
    
  	
    
     
     
     
     
   
	
   
	
    
     
      
     
   
	
    
     
      
     
   
	
	
   
	
    
     
      
     
   
	
   
	
    
     
      
      
     
   
	
	
    
     
     
   

  


spring事务的架构:

1、顶级接口
	public interface PlatformTransactionManager {
	        //得到事务
		TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException;
		//提交事务
		void commit(TransactionStatus status) throws TransactionException;
		//事务的回滚
		void rollback(TransactionStatus status) throws TransactionException;
	}

	TransactionStatus
		boolean isNewTransaction();
2、抽象类
	public abs