`
qdpurple
  • 浏览: 272683 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用spring调用oracle 的存储过程procedure实践

阅读更多

平台中调用oracle 的存储过程procedure实例解析 中解析了平台使用spring调用oracle procedure的实例. 这里自己写一个简单的测试类.

1.获得datasource, 这里通过jdbc连接.见 java项目使用spring jdbc连接数据库

2. 分析知spring主要通过StoredProcedure(spring-jdbc.jar包中的 org.springframework.jdbc.object.StoredProcedure)类进行对procedure的调用.

 

而StoreProcedure为abstract, 不能实例化对象 ,所以需子类继承.并重写父类一些方法.

public class TestSpring extends StoredProcedure {

3.加载datasource,通过读源代码知 在构造方法中加载DataSource

public TestSpring(DataSource dataSource, String procedureName) {
		super(dataSource, procedureName);
	}	
 public TestSpring(DataSource dataSource, String procedureName, boolean isFunction) {

  super(dataSource, procedureName);
  super.setFunction(isFunction);
 }

 注:第二个构造方法, isFunction指所调用的是函数(Function)or存储过程(Procedure), 这里我以调用function为例.是有第二个构造方法

4.调用方法

所调用的存储过程为

function isUser(LOGIN_NAME in varchar2, PASSWORD in varchar2)
    return integer as
    vUserID integer;
  begin
    select ID
      into vUserID
      from Users
     where upper(LOGIN_ID) = upper(isUser.LOGIN_NAME)
       and PASSWORD = isUser.PASSWORD
       and STATUS in (1, 3);
  
    return vUserID;
  
  exception
    when NO_DATA_FOUND then
      return - 1;
  end;

 

5.声明 输入和输出 的参数和oracle 中定义的一致(用到org.springframework.jdbc.core.SqlParameter 和 org.springframework.jdbc.core.SqlOutParameter )  

 SqlParameter paramIn = new SqlParameter("LOGIN_NAME", OracleTypes.IVARCHAR);

sp.declareParameter(paramIn);

 

paramIn = new SqlParameter("PASSWORD", OracleTypes.VARCHAR);

sp.declareParameter(paramIn);

 

paramIn = new SqlOutParameter("vUserID", OracleTypes.INTEGER);  // SqlOutParameter是Sqlparameter的子类

sp.declareParameter(paramIn);

// 编译

sp.compile(); 

6. 加载输入和输出值 都以Map的形式进行加载

Map inParams =  new HashMap();

inParams.put("LOGIN_NAME", "***");

inParams.put("PASSWORD", "***");

 

7 ,执行调用

Map result = sp.execute(inParams);

结果处理:

Integer uid = (Integer) result.get("vUserID");

 

由于代码写的较乱, 只粘贴主要代码:

	DataSource ds;
	@Test
	public void getDatasource() throws SQLException{
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		 ds = (DataSource)ctx.getBean("dataSource");
		logger.info("datasource : " + ds);
	}

 

 

	public void testInvokeProcedure() throws SQLException{
		getDatasource();
		StoredProcedure sp = new TestSpring(ds, "UserPkg.isUser", true);
		SqlParameter param;
		//out parameter
		param = new SqlOutParameter("vUserID", OracleTypes.INTEGER);
		sp.declareParameter(param);
		//in parameter
		param = new SqlParameter("LOGIN_NAME", OracleTypes.VARCHAR);
		sp.declareParameter(param);
		
		param = new SqlParameter("PASSWORD", OracleTypes.VARCHAR);
		sp.declareParameter(param);
		
		sp.compile();

		Encryption enc = new Encryption("abn_admin", "000000" );
		String encPassword = enc.encrypt();
		logger.info("encPassword:  " +encPassword);
		
		Map inParams = new HashMap();
		inParams.put("LOGIN_NAME", "abn_admin");
		inParams.put("PASSWORD", encPassword);

		Map result = sp.execute(inParams );
		logger.info("message : " + (Integer)result.get("vUserID") );

	}

 

 

0
8
分享到:
评论
2 楼 曾老师 2014-02-06  
请问你这个调用数据库中 StoredProcedure 的方法 是最常用到的方式吗?? 总觉得有点麻烦 勿见怪,感觉你的代码很有条理不错
1 楼 liang_sky 2012-02-05  
你好,谢谢你的帖子给我提供了不小的帮助,正好要用到spring的StoredProcedure,对于代码里的Map inParams = new HashMap();是不是要用LinkedHashMap,因为我这使用HashMap总是出问题,用LinkedHashMap就可以

相关推荐

Global site tag (gtag.js) - Google Analytics