使用Spring Framework调用存储过程

时间:2011-05-10 12:54:53

标签: spring

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

谢谢, 拉吉

3 个答案:

答案 0 :(得分: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;
}
}

答案 1 :(得分: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();
    }
}

这或多或少应该是:)

答案 2 :(得分:0)

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

中调用