您的位置:首页 > 编程语言 > Java开发

Spring 框架下,用数组作为参数 传入到 存储过程,或存储过程返回动态数组到java程序 ,BLOB等大对象类似

2011-06-03 22:31 609 查看
一、用数组作为参数 传入到 存储过程

与ORACLE数组对应的 只能是 ORACLE.SQL.ARRAY 对象,而构造这个对象 需要 本地数据库连接JAVA.SQL.Connection 和 ORACLE.SQL.ArrayDescription 对象。

在Spring架构下,要获取到 本地Connection,找到资料基本是关于 ParameterMapper接口,但是实验了后才知道,这个方案还是需要Connection作为参数,继续寻找。

后来在《Spring 让 LOB 数据操作变得简单易行

》 这篇文章中找到了方法。 需要在Spring的配置文件中添加相应的Bean,如下:

//Spring配置文件  JdbcTemplate配置部分
<bean id="nativeJdbcExtractor"
class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"
lazy-init="true" />
<bean id="utmsJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="DataSource" ref="utmDataSource" />
<property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/>
</bean>

//DAO层的实现
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
import java.sql.Connection;
@Override
public String testArray() throws SQLException{
String procName = "TAGEXTENDLIB_TEST";

List<SqlParameter> sqlParameters = new ArrayList<SqlParameter>();
sqlParameters.add(new SqlParameter("IN_WORDSLIST",OracleTypes.ARRAY,"STR_VARLIST"));
sqlParameters.add(new SqlOutParameter("OUT_VAR",OracleTypes.VARCHAR));

String [] params = {"实验室","传媒大学"};

Connection conn = DataSourceUtils.getConnection(getJdbcTemplate().getDataSource());
conn = getJdbcTemplate().getNativeJdbcExtractor().getNativeConnection(conn);
ArrayDescriptor desc = new ArrayDescriptor("STR_VARLIST", conn);
ARRAY arrayWords = new ARRAY(desc, conn, params);

HashMap parameters = new HashMap();
parameters.put("IN_WORDSLIST", arrayWords);

CustomStoragedProcedure customStoragedProcedure = new CustomStoragedProcedure(
this.getJdbcTemplate().getDataSource(), procName, sqlParameters);

Map map= customStoragedProcedure.execute(parameters);
String output = (String)map.get("OUT_VAR");
//System.out.println(output);
return output;
}


STR_VARLIST 是在Oracle数据库中定义的动态数组。

二、 存储过程的动态数组 返回到 java程序处理

关键点是 用ORACLE.SQL.ARRAY 类型来接收。用例子来说话:

@Override
public List<String> getSimilarPinyin(String words) {
// TODO Auto-generated method stub
String procName = "TAGSTANDARDLIB_GETTIPS";

List<SqlParameter> sqlParameters = new ArrayList<SqlParameter>();
sqlParameters.add(new SqlParameter("IN_PINYININDEX",OracleTypes.VARCHAR));
sqlParameters.add(new SqlOutParameter("OUT_PINYINSTRLIST",OracleTypes.ARRAY,"STR_VARLIST"));
HashMap parameters = new HashMap();
parameters.put("IN_PINYININDEX", words);

CustomStoragedProcedure customStoragedProcedure = new CustomStoragedProcedure(
this.getJdbcTemplate().getDataSource(), procName, sqlParameters);

Map map= customStoragedProcedure.execute(parameters);

List<String> tagStandards = new ArrayList<String>();
try {
ARRAY array = (ARRAY)map.get("OUT_PINYINSTRLIST");
String[] values = (String[])array.getArray();
for(int i=0; i<values.length; i++){
tagStandards.add(values[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
return tagStandards==null?null:tagStandards;


【参考】

1. http://www.ibm.com/developerworks/cn/java/j-lo-spring-lob/index.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐