spring中对存储过程的调用

1. org.springframework.jdbc.object.StoredProcedure 这是一个抽象类,通过declareParameter方法来声明存储过程的输入输出参数,再由

execute方法来调用存储过程。
org.springframework.jdbc.core.SqlParameter 用来代表存储过程的参数。
org.springframework.jdbc.core.ResultSetSupportingSqlParameter SqlParameter的子类,是SqlOutParameter、SqlReturnResultSet这

些支持结果集参数类的公共基类
org.springframework.jdbc.core.SqlOutParameter ResultSetSupportingSqlParameter的子类,用来代表存储过程的输出参数。
org.springframework.jdbc.core.SqlReturnResultSet ResultSetSupportingSqlParameter的子类,用来代表调用存储过程的输出结果集


org.springframework.jdbc.core.RowMapper 这是一个接口,通常为JdbcTemplate的查询方法或者存储过程返回结果集使用。

2.新建一个StoredProcedure中的子类:
public class TestStoredProcedure extends StoredProcedure {

protected TestStoredProcedure() {
super();
}
protected TestStoredProcedure(DataSource ds) {
super();
super.setDataSource(ds);
}
protected TestStoredProcedure(DataSource ds, String name) {
super(ds, name);

}

}
2.在某一个DAO中写下这个方法:
调用TestStoredProcedure就可以了.具体可参考下面.

3.然后在spring-conf.xml中配置这个类,并注入dataSource就可以了.

测试例子如下:
import java.sql.Types;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.datasource.*;
import org.springframework.jdbc.object.StoredProcedure;

public class TestSP {

public static void main(String[] args) {

System.out.println("DB TestSP!");
TestSP t = new TestSP();
t.test();
System.out.println("Done!");

}

void test() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
ds.setUrl("jdbc:oracle:thin:@172.20.XX.XXX:1521:test");
ds.setUsername("root");
ds.setPassword("root");

StoreTemplate test=new StoreTemplate(ds);
test.setFunction(true);
test.setSql("sysdate");
test.setDateOutParam("date");

printMap( test.execute());

// MyStoredProcedure sproc = new MyStoredProcedure(ds);
// Map res = sproc.execute();
// printMap(res);

}

private class MyStoredProcedure extends StoredProcedure {
public static final String SQL = "sysdate";

public MyStoredProcedure(DataSource ds) {
setDataSource(ds);
setFunction(true);
setSql(SQL);
declareParameter(new SqlOutParameter("date", Types.DATE));
compile();
}

public Map execute() {
Map out = execute(new HashMap());
return out;
}

}

private static void printMap(Map r) {
Iterator i = r.entrySet().iterator();
while (i.hasNext()) {
System.out.println((String) i.next().toString());
}
}
}

import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.StoredProcedure;
import org.springframework.jdbc.core.SqlOutParameter;

public class StoreTemplate extends StoredProcedure {

HashMap map = new HashMap();

public StoreTemplate() {
super();

}

public void setValue(String key, Object obj) {
map.put(key, obj);
}

public StoreTemplate(DataSource ds) {
setDataSource(ds);
}

public Map execute() {
if (this.getSql() == null || this.getSql().equals(""))
return null;
this.compile();
return execute(map);
}

public void setVarcharParam(String param) {
this.declareParameter(new SqlParameter(param, Types.VARCHAR));
}

public void setDoubleParam(String param) {
this.declareParameter(new SqlParameter(param, Types.DOUBLE));
}

public void setIntegerParam(String param) {
this.declareParameter(new SqlParameter(param, Types.INTEGER));
}

public void setVarcharOutParam(String param) {
this.declareParameter(new SqlOutParameter(param, Types.VARCHAR));
}

public void setDoubleOutParam(String param) {
this.declareParameter(new SqlOutParameter(param, Types.DOUBLE));
}

public void setIntegerOutParam(String param) {
this.declareParameter(new SqlOutParameter(param, Types.INTEGER));
}
public void setDateOutParam(String param) {
this.declareParameter(new SqlOutParameter(param, Types.DATE));
}
}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/pengchua/archive/2007/12/17/1943830.aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值