posts - 66,  comments - 11,  trackbacks - 0

调用存储过程:
Spring通过实现CallableStatementCallback来支持存储过程。假定有个存储过程的名字是ARCHIVE_STUDENTS,执行代码如下:

package com.testproject.spring.datasource;

import java.sql.CallableStatement;
import java.sql.SQLException;

import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.JdbcTemplate;
/*
 * 为了让JdbcTemplate工作,它所需要的,只是一个DataSource实例。
 
*/

public class StudentDaoImpl implements StudentDao {
    
private JdbcTemplate jdbcTemplate;
    
    
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        
this.jdbcTemplate = jdbcTemplate;
    }

    
/**
     * 调用存储过程,通过CallableStatementCallback来实现
     
*/

    
public void archiveStudentData(){
        CallableStatementCallback cb 
= new CallableStatementCallback(){
            
public Object doInCallableStatement(CallableStatement cs)throws SQLException{
                cs.execute();
                
return null;
            }

        }
;
        jdbcTemplate.execute(
"{ARCHIVE_STUDENTS}",cb);
    }

}


把操作创建成对象:

插入:

package com.testproject.spring.datasource;

import java.sql.Types;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.SqlUpdate;
/**
 * Spring提供了一种真正把数据库操作建模成对象的方法,这样就在的代码和直接JDBC之间又加了一个绝缘层。
 * 首先,这些数据库操作对象是线程安全的,意味着对于每个数据库操作,你只需创建一个实例。
 * 其次,任何数据库操作对象必须在运行前先编译一下,这样就让对象知道什么时候可以预备statement,以便在稍后能执行它们。
 * 使用:
 * private InsertPerson insertPerson;
 * public int insertPerson(Person person){
 *     return insertPerson.insert(person);
 * }
 *
 
*/

public class InsertPerson extends SqlUpdate {
    
public InsertPerson(DataSource ds){
        
//首先要给sqlUpdate提供一个DataSource,用来创建JdbcTemplate
        setDataSource(ds);
        setSql(
"insert into person(id,firstName,lastName) values(?,?,?)");
        
//其次,我们需要为statement中的每个参数调用这个方法,顺序也是很重要的
        declareParameter(new SqlParameter(Types.NUMERIC));
        declareParameter(
new SqlParameter(Types.VARCHAR));
        declareParameter(
new SqlParameter(Types.VARCHAR));
        
//最后编译它,每个数据库操作对象必须在它被使用之前编译好。
        compile();
    }

    
public int insert(Person person){
        Object[] params 
= new Object[]{
                person.getId(),
                person.getFirstName(),
                person.getLastName()
        }
;
        
return update(params);
    }

}

查询:

package com.testproject.spring.datasource;

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

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.MappingSqlQuery;

/**
 * 使用:
 * private PersonByIdQuery personByIdQuery;
 * public person getPerson(Integer id){
 *     Object[] params = new Object[]{id};
 *  return (Person)personByIdQuery.execute(params).get(0);
 * }
 *
 
*/

public class PersonByIdQuery extends MappingSqlQuery {
    
    
public PersonByIdQuery(DataSource ds){
        
super(ds,"select id,first_name,last_name from person where id=?");
        declareParameter(
new SqlParameter("id",Types.INTEGER));
        compile();
    }

    
    
protected Object mapRow(ResultSet rs, int rowNumber) throws SQLException {
        Person person 
= new Person();
        person.setId((Integer)rs.getObject(
"id"));
        person.setFirstName(rs.getString(
"first_name"));
        person.setLastName(rs.getString(
"last_name"));
        
return person;
    }


}

 

posted on 2009-11-14 15:21 王永庆 阅读(258) 评论(0)  编辑  收藏 所属分类: SPRING

只有注册用户登录后才能发表评论。


网站导航:
 
<2009年11月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

常用链接

留言簿(1)

随笔分类

随笔档案

关注blogs

搜索

  •  

最新评论

阅读排行榜

评论排行榜