spring中对存储过程的调用

本文介绍如何使用Spring框架中的JDBC模块调用数据库存储过程。通过创建StoredProcedure的子类,并利用SqlParameter等类定义输入输出参数,实现存储过程的调用。文中提供了完整的代码示例及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
内容概要:本文针对T型三电平逆变器在电网电压跌落条件下的低电压穿越(LVRT)问题,提出了一种结合改进电流解耦控制与直流侧中点电位平衡的综合控制策略。通过建立逆变器的数学模型,采用双二阶广义积分器(DSOGI)实现电网电压正负序分量的精确分离,并在此基础上设计自适应无功支撑与故障电流限幅控制,以满足LVRT对无功补偿和电流安全性的要求。为提升不对称故障下的动态性能,引入改进的正负序解耦电流控制方法,有效克服传统PI控制器在耦合干扰下的控制滞后问题。同时,结合零序电压注入法对中点电位进行主动调控,抑制中点电压漂移,保障系统长期稳定运行。整个控制方案在Simulink平台上完成建模与仿真验证,结果表明该方法在电网故障期间具有优良的动态响应特性、稳定的中点电位控制能力和可靠的并网运行性能。; 适合人群:具备电力电子、自动控制或新能源并网技术背景,从事逆变器控制算法开发、微电网系统集成或可再生能源发电系统研究的工程技术人员及高校研究生。; 使用场景及目标:①用于提升T型三电平逆变器在电网故障条件下的并网可靠性与电能质量;②为高比例新能源接入场景下的LVRT控制策略设计提供理论支撑与技术路径;③适用于需要实现高性能电流控制与中点电位稳定的工业级变流器控制系统开发。; 阅读建议:建议结合提供的Simulink仿真模型进行学习,重点剖析正负序分离、改进电流解耦控制环设计及中点电位平衡模块的协同工作机制,可通过设置不同类型的电网故障(如单相短路、两相短路)和调节控制参数开展对比实验,深入掌握各模块对系统整体性能的影响规律。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值