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));
}
}