2011-05-10 72 views
1

任何人都可以提供Spring框架的存储过程调用的完整示例。用Spring框架存储过程调用

谢谢, 拉吉

+1

使用JDBC?休眠? JPA? JDO?请添加更多细节! – 2011-05-10 13:05:27

回答

2

使用Spring存储过程框架:

JDBC-config.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
     <property name="jndiName" value="java:comp/env/orcl/DB"/> 
    </bean> 

    <bean id="storedProc" class="com.DatabaseStoredProc"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="sql" value="aStoredProc" /> 
     <property name="parameters"> 
      <list> 
       <bean class="org.springframework.jdbc.core.SqlParameter"> 
        <constructor-arg index="0" value="p_id1" /> 
        <constructor-arg index="1"> 
         <util:constant static-field="java.sql.Types.VARCHAR" /> 
        </constructor-arg> 
       </bean> 
       <bean class="org.springframework.jdbc.core.SqlParameter"> 
        <constructor-arg index="0" value="p_id2" /> 
        <constructor-arg index="1"> 
         <util:constant static-field="java.sql.Types.VARCHAR" /> 
        </constructor-arg> 
       </bean> 
      </list> 
     </property> 
    </bean> 


</beans> 

DatabaseStoredProcedure类

import java.util.Map; 
import org.springframework.jdbc.object.StoredProcedure; 

public class DatabaseStoredProc extends StoredProcedure { 

    public Map<String, Object> execute(Map inputs){ 
     Map out=super.execute(inputs); 
     return null; 
    } 

    // Method to map data to inputs Map: 

public boolean businessRules(Object obj, Map inputs){ 
    SomeObject otd = (SomeObject) obj; 
    inputs.put("p_id1", otd.getId1()); 
    inputs.put("p_id2", otd.getId2()); 

    return true; 
} 
} 
0

参照创建一个控制器你注入你的数据源( applicationContext.xml中):

<bean id="storedProcedureDao" class="com..myapp.SpringStoredProcedureDao"> 
    <property name="dataSource"> 
     <ref bean="jtdsDataSource"/> 
    </property> 
</bean> 

数据来源:

<bean id="jtdsDataSource" class="net.sourceforge.jtds.jdbcx.JtdsDataSource"> 
    <property name="serverName"> 
     <value>servername</value> 
    </property> 
    <property name="databaseName"> 
     <value>database</value> 
    </property> 
    <property name="user"> 
     <value>username</value> 
    </property> 
    <property name="password"> 
     <value>password</value> 
    </property> 
</bean> 

在你的控制器,把下面的:

public class SpringStoredProcedureDao extends StoredProcedure { 
    private DataSource dataSource; 

    public void setDataSource(DataSource dataSource) { 
     this.dataSource = dataSource; 
    } 


    public CallStoredProcedure(String procedureName){ 
     super(this.dataSource, procedureName);    
     compile(); 
    } 
} 

这应该或多或少吧:)

0

上述解决方案将无法工作,因为您无法在子类方法中调用超类构造函数。必须在子类构造函数中调用