在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Java/ Spring JDBC調(diào)用存儲函數(shù)
Spring JDBC調(diào)用存儲函數(shù)
Spring JDBC NamedParameterJdbcTemplate類示例
Spring JDBC SimpleJdbcCall類示例
Spring JDBC PreparedStatementSetter接口示例
Spring JDBC環(huán)境設置
Spring JDBC多批次操作
Spring JDBC SimpleJdbcInsert類示例
Spring JDBC RowMapper接口示例
Spring JDBC對象批量操作
Spring JDBC ResultSetExtractor接口示例
Spring JDBC配置數(shù)據(jù)源
Spring JDBC處理CLOB類型字段
Spring JDBC刪除數(shù)據(jù)
Spring JDBC更新數(shù)據(jù)
Spring JDBC SqlUpdate類示例
Spring JDBC SqlQuery類示例
Spring JDBC插入數(shù)據(jù)
Spring JDBC StoredProcedure類示例
Spring JDBC JdbcTemplate類示例
Spring JDBC入門程序
Spring JDBC調(diào)用存儲過程
Spring JDBC查詢數(shù)據(jù)
Spring JDBC批量操作
Spring JDBC處理BLOB類型字段
Spring JDBC教程

Spring JDBC調(diào)用存儲函數(shù)

以下示例將演示spring jdbc如何調(diào)用存儲函數(shù)。在這個示例中將通過調(diào)用存儲函數(shù)來讀取Student表中的一個可用記錄信息。傳遞一個ID并獲取學生的姓名。

語法:

SimpleJdbcCall jdbcCall = new SimpleJdbcCall(dataSource).withFunctionName("get_student_name");

SqlParameterSource in = new MapSqlParameterSource().addValue("in_id", id);
String name = jdbcCall.executeFunction(String.class, in);
Student student = new Student();
student.setId(id);
student.setName(name);

在上面語法中 -

  • jdbcCall - 這是一個SimpleJdbcCall對象,它用來表示存儲函數(shù)。
  • in - 這是一個SqlParameterSource對象用于將參數(shù)傳遞給存儲函數(shù)。
  • student - Student對象。
  • jdbcTemplateObject - StudentJDBCTemplate對象從數(shù)據(jù)庫中調(diào)用存儲函數(shù)。

SimpleJdbcCall類可用于使用INOUT參數(shù)調(diào)用存儲函數(shù)。 可以在使用任何RDBMS(如Apache Derby,DB2,MySQL,Microsoft SQL Server,Oracle和Sybase)時使用此方法。

要了解這個方法的使用,請考慮以下MySQL存儲函數(shù),這需要學生ID作為參數(shù)并返回相應的學生姓名。在MySQL命令提示符在TEST數(shù)據(jù)庫中創(chuàng)建這個存儲過程 -

DELIMITER $$

DROP FUNCTION IF EXISTS `TEST`.`get_student_name` $$
CREATE FUNCTION `get_student_name` (in_id INTEGER)
RETURNS varchar(200)
BEGIN
DECLARE out_name VARCHAR(200);
   SELECT name
   INTO out_name
   FROM Student where id = in_id;

RETURN out_name;
END $$

DELIMITER ;

創(chuàng)建項目

要了解上面提到的Spring JDBC相關(guān)的概念,這里創(chuàng)建一個項目用來演示如何調(diào)用數(shù)據(jù)中的存儲過程。打開Eclipse IDE,并按照以下步驟創(chuàng)建一個名稱為:CallingStoredProcedure 的Spring應用程序項目。

步驟說明

  1. 參考第一個Spring JDBC項目來創(chuàng)建一個Maven項目 - http://www.yiibai.com/springjdbc/first_application.html
  2. 更新bean配置并運行應用程序。

完整的項目結(jié)構(gòu)如下所示 -

文件 : pom.xml 的內(nèi)容 -

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yiibai</groupId>
    <artifactId>CallingStoredFunction</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>CallingStoredFunction</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.40</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
    </dependencies>
</project>

以下是數(shù)據(jù)訪問對象接口文件:StudentDAO.java的代碼內(nèi)容:

package com.yiibai;

import java.util.List;
import javax.sql.DataSource;

public interface StudentDAO {
    /**
     * This is the method to be used to initialize database resources ie.
     * connection.
     */
    public void setDataSource(DataSource ds);

    /**
     * This is the method to be used to list down a record from the Student
     * table corresponding to a passed student id.
     */
    public Student getStudent(Integer id);
}

以下是文件:Student.java的代碼內(nèi)容:

package com.yiibai;

public class Student {
    private Integer age;
    private String name;
    private Integer id;

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }
}

以下是文件:StudentMapper.java的代碼內(nèi)容:

package com.yiibai;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;

public class StudentMapper implements RowMapper<Student> {
    public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
        Student student = new Student();
        student.setId(rs.getInt("id"));
        student.setName(rs.getString("name"));
        student.setAge(rs.getInt("age"));
        return student;
    }
}

以下是實現(xiàn)類文件:StudentJDBCTemplate.java,它實現(xiàn)了接口StudentDAO.java
以下是文件:StudentJDBCTemplate.java的代碼內(nèi)容:

package com.yiibai;

import java.util.Map;

import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;

public class StudentJDBCTemplate implements StudentDAO {
    private DataSource dataSource;
    private JdbcTemplate jdbcTemplateObject;

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

    public Student getStudent(Integer id) {
        SimpleJdbcCall jdbcCall = new SimpleJdbcCall(dataSource).withFunctionName("get_student_name");

        SqlParameterSource in = new MapSqlParameterSource().addValue("in_id", id);
        String name = jdbcCall.executeFunction(String.class, in);
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        return student;
    }
}

上面編寫的執(zhí)行調(diào)用代碼涉及創(chuàng)建包含IN參數(shù)的SqlParameterSource對象。 將輸入值提供的名稱與存儲過程中聲明的參數(shù)名稱的名稱進行匹配很重要。 executeFunction方法使用IN參數(shù),并返回一個存儲函數(shù)中指定的String類型變量中。

以下是程序執(zhí)行入口文件:MainApp.java的代碼內(nèi)容:

package com.yiibai;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yiibai.StudentJDBCTemplate;

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("application-beans.xml");

      StudentJDBCTemplate studentJDBCTemplate = 
      (StudentJDBCTemplate)context.getBean("studentJDBCTemplate");

      System.out.println("Get Student's Name By ID 1:");
      Student student = studentJDBCTemplate.getStudent(1);

      System.out.print("ID : " + student.getId() );
      System.out.print(", Name : " + student.getName() );

      System.out.println("\n Get Student's Name By ID 3:");
      Student student2 = studentJDBCTemplate.getStudent(3);
      System.out.print("ID : " + student2.getId() );
      System.out.print(", Name : " + student2.getName() );
   }
}

以下是Bean和數(shù)據(jù)庫配置文件:application-beans.xml的代碼內(nèi)容:

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

    <!-- Initialization for data source -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true" />
        <property name="username" value="root" />
        <property name="password" value="123456" />
    </bean>

    <!-- Definition for studentJDBCTemplate bean -->
    <bean id="studentJDBCTemplate" class="com.yiibai.StudentJDBCTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>

注意: application-beans.xml 文件的位置是:{workspace}/fistapp/src/main/java 目錄,如果放置錯了,程序可能會因為找不到此配置文件而出錯。

完成創(chuàng)建源代碼和bean和數(shù)據(jù)庫連接信息的文件配置后,運行應用程序。這里先簡單說明一下運行的步驟,在項目名稱(CallingStoredFunction)上點擊右鍵,在彈出的菜單中選擇:【Run As】-> 【Maven test】

在運行測試成功后,應該會輸出類似以下內(nèi)容(含有 BUILD SUCCESS 的信息) 。
接下來,點擊類文件:MainApp.java 選擇【Run As】->【Java Application】,如果應用程序一切正常,這將打印以下消息:

Get Student's Name By ID 1:
ID : 1, Name : Maxsu
 Get Student's Name By ID 3:
ID : 3, Name : Allen