SpringJDBC框架簡介

2022-09-21 13:57:12 字數 2982 閱讀 1976

目錄

在使用普通的 jdbc 資料庫時,就會很麻煩的寫不必要的**來處理異常,開啟和關閉資料庫連線等。但 spring jdbc 框架負責所有的低層細節,從開始開啟連線,準備和執行 sql 語句,處理異常,處理事務,到最後關閉連線。

所以當從資料庫中獲取資料時,你所做的是定義連線引數,指定要執行的 sql 語句,每次迭代完成所需的工作。

spring jdbc 提供幾種方法和資料庫中相應的不同的類與介面。我將給出使用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 檔案中配置資料來源,其中一段**如下所示:

dao 代表常用的資料庫互動的資料訪問物件。daos 提供一種方法來讀取資料並將資料寫入到資料庫中,它們應該通過乙個介面顯示此功能,應用程式的其餘部分將訪問它們。

在 spring 中,資料訪問物件(dao)支援很容易用統一的方法使用資料訪問技術,如 jdbc、hibernate、jpa 或者 jdo。

我們看看如何使用 sql 和 jdbctemplate 物件在資料庫表中執行 crud(建立、讀取、更新和刪除)操作。

查詢乙個整數型別:

string sql = "select count(*) from student";

int rowcount = jdbctemplateobject.queryforint( sql );

查詢乙個 long 型別:

string sql = "select count(*) from student";

lon程式設計客棧g rowcount = jdbctemplateobject.queryforlong( sql );

乙個使用繫結變數的簡單查詢:

string sql = "select age from student where id程式設計客棧 = ?";

int age = jdbctemplateobject.queryforint(sql, new object);

查詢字串:

string sql = "select name from student where id = ?";

string name = jdbctemplateobject.queryforobject(sql, new object, string.class);

查詢並返回乙個物件:

string sql = "select * from student where id = ?";

student student = jdbctemplateobject.queryforobject(sql,

new object, new studentmapper());

public class studentmapper implements rowmapper

}查詢並返回多個物件:

string sql = "select * from student";

list students = jdbctemplateobject.query(sql,

new studentmapper());

puwww.cppcns.comblic class studentmapper implements rowmapper

}在表中插入一行:

string sql = "insert into student (name, age) values (?, ?)";

jdbctemplateobject.update( sql, new object );

更新表中的一行:

string sql = "update student set name = ? where id = ?";

jdbctemplateobject.update( sql, new object );

從表中刪除一行:

string sql = "delete student where id = ?";

jdbctemplateobject.update( sql, new object );

你可以使用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 框架 簡介

在使用普通的 jdbc 資料庫時,就會很麻煩的寫不必要的 來處理異常,開啟和關閉資料庫連線等。但 spring jdbc 框架負責所有的低層細節,從開始開啟連線,準備和執行 sql 語句,處理異常,處理事務,到最後關閉連線。所以當從資料庫中獲取資料時,你所做的是定義連線引數,指定要執行的 sql 語...

web框架簡介,django簡介

目錄django簡介 建立django專案的方式 django各個檔案的作用 django小白必會三板斧 c s架構 客戶端服務端 b s架構 瀏覽器伺服器 本質 b s架構其實也是c s架構 超文字傳輸協議 規定了客戶端和服務端訊息傳輸的格式 四大特性 1 基於tcp ip協議作用於應用層的協議 ...

Spring JDBC組合開發

使用spring jdbc整合步驟如下 配置資料來源,如 略 配置事務。配置事務時,需要在xml配置檔案中引入用於宣告事務的tx命名空間 見下頁 事務的配置方式有兩種 註解方式和基於xml配置方式。所用jar包 commons dbcp.jar 使用datasource必須 commons pool...