Spring JDBC 框架

Spring JDBC 框架


JDBC 框架概述

在使用普通的 JDBC 數據庫時,就會很麻煩的寫不必要的代碼來處理異常,打開和關閉數據庫連接等。但 Spring JDBC 框架負責所有的低層細節,從開始打開連接,準備和執行 SQL 語句,處理異常,處理事務,到最後關閉連接。

所以當從數據庫中獲取數據時,你所做的是定義連接參數,指定要執行的 SQL 語句,每次迭代完成所需的工作。

Spring JDBC 提供幾種方法和數據庫中相應的不同的類與接口。我將給出使用 JdbcTemplate 類框架的經典和最受歡迎的方法。這是管理所有數據庫通信和異常處理的中央框架類。

JdbcTemplate 類

JdbcTemplate 類執行 SQL 查詢、更新語句和存儲過程調用,執行迭代結果集和提取返回參數值。它也捕獲 JDBC 異常並轉換它們到 org.springframework.dao 包中定義的通用類、更多的信息、異常層次結構。

JdbcTemplate 類的實例是線程安全配置的。所以你可以配置 JdbcTemplate 的單個實例,然後將這個共享的引用安全地注入到多個 DAOs 中。

使用 JdbcTemplate 類時常見的做法是在你的 Spring 配置文件中配置數據源,然後共享數據源 bean 依賴注入到 DAO 類中,並在數據源的設值函數中創建了 JdbcTemplate。

配置數據源

我們在數據庫 TEST 中創建一個數據庫表 Student。假設你正在使用 MySQL 數據庫,如果你使用其他數據庫,那麼你可以改變你的 DDL 和相應的 SQL 查詢。

CREATE TABLE Student(
   ID   INT NOT NULL AUTO_INCREMENT,
   NAME VARCHAR(20) NOT NULL,
   AGE  INT NOT NULL,
   PRIMARY KEY (ID)
);

現在,我們需要提供一個數據源到 JdbcTemplate 中,所以它可以配置本身來獲得數據庫訪問。你可以在 XML 文件中配置數據源,其中一段代碼如下所示:

<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"/>
   <property name="username" value="root"/>
   <property name="password" value="password"/>
</bean>

數據訪問對象(DAO)

DAO 代表常用的數據庫交互的數據訪問對象。DAOs 提供一種方法來讀取數據並將數據寫入到數據庫中,它們應該通過一個接口顯示此功能,應用程序的其餘部分將訪問它們。

在 Spring 中,數據訪問對象(DAO)支持很容易用統一的方法使用數據訪問技術,如 JDBC、Hibernate、JPA 或者 JDO。

執行 SQL 語句

我們看看如何使用 SQL 和 jdbcTemplate 對象在數據庫表中執行 CRUD(創建、讀取、更新和刪除)操作。

查詢一個整數類型:

String SQL = "select count(*) from Student";
int rowCount = jdbcTemplateObject.queryForInt( SQL );

查詢一個 long 類型:

String SQL = "select count(*) from Student";
long rowCount = jdbcTemplateObject.queryForLong( SQL );

一個使用綁定變量的簡單查詢:

String SQL = "select age from Student where id = ?";
int age = jdbcTemplateObject.queryForInt(SQL, new Object[]{10});

查詢字符串:

String SQL = "select name from Student where id = ?";
String name = jdbcTemplateObject.queryForObject(SQL, new Object[]{10}, String.class);

查詢並返回一個對象:

String SQL = "select * from Student where id = ?";
Student student = jdbcTemplateObject.queryForObject(SQL, 
                  new Object[]{10}, new StudentMapper());
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;
   }
}

查詢並返回多個對象:

String SQL = "select * from Student";
List<Student> students = jdbcTemplateObject.query(SQL,
                         new StudentMapper());
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;
   }
}

在表中插入一行:

String SQL = "insert into Student (name, age) values (?, ?)";
jdbcTemplateObject.update( SQL, new Object[]{"Zara", 11} );

更新表中的一行:

String SQL = "update Student set name = ? where id = ?";
jdbcTemplateObject.update( SQL, new Object[]{"Zara", 10} );

從表中刪除一行:

String SQL = "delete Student where id = ?";
jdbcTemplateObject.update( SQL, new Object[]{20} );

執行 DDL 語句

你可以使用 jdbcTemplate 中的 execute(…) 方法來執行任何 SQL 語句或 DDL 語句。下面是一個使用 CREATE 語句創建一個表的示例:

String SQL = "CREATE TABLE Student( " +
   "ID   INT NOT NULL AUTO_INCREMENT, " +
   "NAME VARCHAR(20) NOT NULL, " +
   "AGE  INT NOT NULL, " +
   "PRIMARY KEY (ID));"
jdbcTemplateObject.execute( SQL );

Spring JDBC 框架例子

基於上述概念,讓我們看看一些重要的例子來幫助你理解在 Spring 中使用 JDBC 框架:

序號 例子 & 描述
1 Spring JDBC Example這個例子將解釋如何編寫一個簡單的基於 Spring 應用程序的 JDBC。
2 SQL Stored Procedure in Spring學習在使用 Spring 中的 JDBC 時如何調用 SQL 存儲過程。

Spring JDBC 示例

想要理解帶有 jdbc 模板類的 Spring JDBC 框架的相關概念,讓我們編寫一個簡單的示例,來實現下述 Student 表的所有 CRUD 操作。

CREATE TABLE Student(
   ID   INT NOT NULL AUTO_INCREMENT,
   NAME VARCHAR(20) NOT NULL,
   AGE  INT NOT NULL,
   PRIMARY KEY (ID)
);

在繼續之前,讓我們適當地使用 Eclipse IDE 並按照如下所示的步驟創建一個 Spring 應用程序:

步驟 描述
1 創建一個名爲 SpringExample 的項目,並在創建的項目中的 src 文件夾下創建包 com.tutorialspoint
2 使用 Add External JARs 選項添加必需的 Spring 庫,解釋見 Spring Hello World Example 章節。
3 在項目中添加 Spring JDBC 指定的最新的庫 mysql-connector-java.jarorg.springframework.jdbc.jarorg.springframework.transaction.jar。如果這些庫不存在,你可以下載它們。
4 創建 DAO 接口 StudentDAO 並列出所有必需的方法。儘管這一步不是必需的而且你可以直接編寫 StudentJDBCTemplate 類,但是作爲一個好的實踐,我們最好還是做這一步。
5 com.tutorialspoint 包下創建其他的必需的 Java 類 StudentStudentMapperStudentJDBCTemplateMainApp
6 確保你已經在 TEST 數據庫中創建了 Student 表。並確保你的 MySQL 服務器運行正常,且你可以使用給出的用戶名和密碼讀/寫訪問數據庫。
7 src 文件夾下創建 Beans 配置文件 Beans.xml
8 最後一步是創建所有的 Java 文件和 Bean 配置文件的內容並按照如下所示的方法運行應用程序。

以下是數據訪問對象接口文件 StudentDAO.java 的內容:

package com.tutorialspoint;
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 create
    * a record in the Student table.
    */
   public void create(String name, Integer age);
   /** 
    * 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);
   /** 
    * This is the method to be used to list down
    * all the records from the Student table.
    */
   public List<Student> listStudents();
   /** 
    * This is the method to be used to delete
    * a record from the Student table corresponding
    * to a passed student id.
    */
   public void delete(Integer id);
   /** 
    * This is the method to be used to update
    * a record into the Student table.
    */
   public void update(Integer id, Integer age);
}

下面是 Student.java 文件的內容:

package com.tutorialspoint;
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 文件的內容:

package com.tutorialspoint;
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;
   }
}

下面是爲定義的 DAO 接口 StudentDAO 的實現類文件 StudentJDBCTemplate.java

package com.tutorialspoint;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
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 void create(String name, Integer age) {
      String SQL = "insert into Student (name, age) values (?, ?)";     
      jdbcTemplateObject.update( SQL, name, age);
      System.out.println("Created Record Name = " + name + " Age = " + age);
      return;
   }
   public Student getStudent(Integer id) {
      String SQL = "select * from Student where id = ?";
      Student student = jdbcTemplateObject.queryForObject(SQL, 
                        new Object[]{id}, new StudentMapper());
      return student;
   }
   public List<Student> listStudents() {
      String SQL = "select * from Student";
      List <Student> students = jdbcTemplateObject.query(SQL, 
                                new StudentMapper());
      return students;
   }
   public void delete(Integer id){
      String SQL = "delete from Student where id = ?";
      jdbcTemplateObject.update(SQL, id);
      System.out.println("Deleted Record with ID = " + id );
      return;
   }
   public void update(Integer id, Integer age){
      String SQL = "update Student set age = ? where id = ?";
      jdbcTemplateObject.update(SQL, age, id);
      System.out.println("Updated Record with ID = " + id );
      return;
   }
}

以下是 MainApp.java 文件的內容:

package com.tutorialspoint;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tutorialspoint.StudentJDBCTemplate;
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");
      StudentJDBCTemplate studentJDBCTemplate = 
      (StudentJDBCTemplate)context.getBean("studentJDBCTemplate");    
      System.out.println("------Records Creation--------" );
      studentJDBCTemplate.create("Zara", 11);
      studentJDBCTemplate.create("Nuha", 2);
      studentJDBCTemplate.create("Ayan", 15);
      System.out.println("------Listing Multiple Records--------" );
      List<Student> students = studentJDBCTemplate.listStudents();
      for (Student record : students) {
         System.out.print("ID : " + record.getId() );
         System.out.print(", Name : " + record.getName() );
         System.out.println(", Age : " + record.getAge());
      }
      System.out.println("----Updating Record with ID = 2 -----" );
      studentJDBCTemplate.update(2, 20);
      System.out.println("----Listing Record with ID = 2 -----" );
      Student student = studentJDBCTemplate.getStudent(2);
      System.out.print("ID : " + student.getId() );
      System.out.print(", Name : " + student.getName() );
      System.out.println(", Age : " + student.getAge());      
   }
}

下述是配置文件 Beans.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" 
    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"/>
      <property name="username" value="root"/>
      <property name="password" value="password"/>
   </bean>

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

</beans>

當你完成創建源和 bean 配置文件後,運行應用程序。如果你的應用程序一切運行順利的話,將會輸出如下所示的消息:

------Records Creation--------
Created Record Name = Zara Age = 11
Created Record Name = Nuha Age = 2
Created Record Name = Ayan Age = 15
------Listing Multiple Records--------
ID : 1, Name : Zara, Age : 11
ID : 2, Name : Nuha, Age : 2
ID : 3, Name : Ayan, Age : 15
----Updating Record with ID = 2 -----
Updated Record with ID = 2
----Listing Record with ID = 2 -----
ID : 2, Name : Nuha, Age : 20

你可以嘗試自己刪除在我的例子中我沒有用到的操作,但是現在你有一個基於 Spring JDBC 框架的工作應用程序,你可以根據你的項目需求來擴展這個框架,添加複雜的功能。還有其他方法來訪問你使用 NamedParameterJdbcTemplateSimpleJdbcTemplate 類的數據庫,所以如果你有興趣學習這些類的話,那麼你可以查看 Spring 框架的參考手冊。

Spring 中 SQL 的存儲過程

SimpleJdbcCall 類可以被用於調用一個包含 IN 和 OUT 參數的存儲過程。你可以在處理任何一個 RDBMS 時使用這個方法,就像 Apache Derby, DB2, MySQL, Microsoft SQL Server, Oracle,和 Sybase。

爲了瞭解這個方法,我們使用 Student 表,它可以在 MySQL TEST 數據庫中使用下面的 DDL 進行創建:

CREATE TABLE Student(
   ID   INT NOT NULL AUTO_INCREMENT,
   NAME VARCHAR(20) NOT NULL,
   AGE  INT NOT NULL,
   PRIMARY KEY (ID)
);

下一步,考慮接下來的 MySQL 存儲過程,該過程使用 學生 Id 並且使用 OUT 參數返回相應的學生的姓名和年齡。所以讓我們在你的 TEST 數據庫中使用 MySQL 命令提示符創建這個存儲過程:

DELIMITER $$
DROP PROCEDURE IF EXISTS `TEST`.`getRecord` $$
CREATE PROCEDURE `TEST`.`getRecord` (
IN in_id INTEGER,
OUT out_name VARCHAR(20),
OUT out_age  INTEGER)
BEGIN
   SELECT name, age
   INTO out_name, out_age
   FROM Student where id = in_id;
END $$
DELIMITER ;

現在,讓我們編寫我們的 Spring JDBC 應用程序,它可以實現對我們的 Student 數據庫表的創建和讀取操作。讓我們使 Eclipse IDE 處於工作狀態,然後按照如下步驟創建一個 Spring 應用程序:

步驟 描述
1 創建一個名爲 SpringExample 的項目,並且在所創建項目的 src 文件夾下創建一個名爲 com.tutorialspoint 的包。
2 使用 Add External JARs 選項添加所需的 Spring 庫文件,就如在 Spring Hello World Example 章節中解釋的那樣。
3 在項目中添加 Spring JDBC 指定的最新的庫文件 mysql-connector-java.jarorg.springframework.jdbc.jarorg.springframework.transaction.jar。如果你還沒有這些所需要的庫文件,你可以下載它們。
4 創建 DAO 接口 StudentDAO 並且列出所有需要的方法。 即使他不是必需的,你可以直接編寫 StudentJDBCTemplate 類,但是作爲一個良好的實踐,讓我們編寫它。
5 com.tutorialspoint 包下創建其他所需要的 Java 類 StudentStudentMapperStudentJDBCTemplateMainApp
6 確保你已經在 TEST 數據庫中創建了 Student 表。同樣確保你的 MySQL 服務器是正常工作的,並且保證你可以使用給定的用戶名和密碼對數據庫有讀取/寫入的權限。
7 src 文件夾下創建 Beans 配置文件 Beans.xml
8 最後一步是創建所有 Java 文件和 Bean 配置文件的內容,並且按如下解釋的那樣運行應用程序。

下面是數據訪問對象接口文件 StudentDAO.java 的內容:

package com.tutorialspoint;
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 create
    * a record in the Student table.
    */
   public void create(String name, Integer age);
   /** 
    * 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);
   /** 
    * This is the method to be used to list down
    * all the records from the Student table.
    */
   public List<Student> listStudents();
}

下面是 Student.java 文件的內容:

package com.tutorialspoint;
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 文件的內容:

package com.tutorialspoint;
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;
   }
}

下面是實現類文件 StudentJDBCTemplate.java,定義了 DAO 接口 StudentDAO:

package com.tutorialspoint;
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;
public class StudentJDBCTemplate implements StudentDAO {
   private DataSource dataSource;
   private SimpleJdbcCall jdbcCall;
   public void setDataSource(DataSource dataSource) {
      this.dataSource = dataSource;
      this.jdbcCall =  new SimpleJdbcCall(dataSource).
                       withProcedureName("getRecord");
   }
   public void create(String name, Integer age) {
      JdbcTemplate jdbcTemplateObject = new JdbcTemplate(dataSource);
      String SQL = "insert into Student (name, age) values (?, ?)";
      jdbcTemplateObject.update( SQL, name, age);
      System.out.println("Created Record Name = " + name + " Age = " + age);
      return;
   }
   public Student getStudent(Integer id) {
      SqlParameterSource in = new MapSqlParameterSource().
                              addValue("in_id", id);
      Map<String, Object> out = jdbcCall.execute(in);
      Student student = new Student();
      student.setId(id);
      student.setName((String) out.get("out_name"));
      student.setAge((Integer) out.get("out_age"));
      return student;
   }
   public List<Student> listStudents() {
      String SQL = "select * from Student";    
      List <Student> students = jdbcTemplateObject.query(SQL, 
                                      new StudentMapper());
      return students;
   }
}

關於上述項目的幾句話:你編寫的課調用執行的代碼涉及創建一個包含 IN 參數的 SqlParameterSource。名稱的匹配是很重要的,該名稱可以使用在存儲過程彙總聲明的參數名稱來提供輸入值。execute 方法利用 IN 參數返回一個包含在存儲過程中由名稱指定的任何外部參數鍵的映射。現在讓我們移動主應用程序文件 MainApp.java,如下所示:

package com.tutorialspoint;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tutorialspoint.StudentJDBCTemplate;
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");
      StudentJDBCTemplate studentJDBCTemplate = 
      (StudentJDBCTemplate)context.getBean("studentJDBCTemplate");     
      System.out.println("------Records Creation--------" );
      studentJDBCTemplate.create("Zara", 11);
      studentJDBCTemplate.create("Nuha", 2);
      studentJDBCTemplate.create("Ayan", 15);
      System.out.println("------Listing Multiple Records--------" );
      List<Student> students = studentJDBCTemplate.listStudents();
      for (Student record : students) {
         System.out.print("ID : " + record.getId() );
         System.out.print(", Name : " + record.getName() );
         System.out.println(", Age : " + record.getAge());
      }
      System.out.println("----Listing Record with ID = 2 -----" );
      Student student = studentJDBCTemplate.getStudent(2);
      System.out.print("ID : " + student.getId() );
      System.out.print(", Name : " + student.getName() );
      System.out.println(", Age : " + student.getAge());      
   }
}

下面是配置文件 Beans.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" 
    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"/>
      <property name="username" value="root"/>
      <property name="password" value="password"/>
   </bean>

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

</beans>

一旦你已經完成的創建了源文件和 bean 配置文件,讓我們運行一下應用程序。如果你的應用程序一切都正常的話,這將會輸出以下消息:

------Records Creation--------
Created Record Name = Zara Age = 11
Created Record Name = Nuha Age = 2
Created Record Name = Ayan Age = 15
------Listing Multiple Records--------
ID : 1, Name : Zara, Age : 11
ID : 2, Name : Nuha, Age : 2
ID : 3, Name : Ayan, Age : 15
----Listing Record with ID = 2 -----
ID : 2, Name : Nuha, Age : 2
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章