- iBATIS 教程
- iBATIS - 主頁
- iBATIS - 概述
- iBATIS - 環境
- iBATIS - 建立操作
- iBATIS - 讀取操作
- iBATIS - 更新操作
- iBATIS - 刪除操作
- iBATIS - 結果對映
- iBATIS - 儲存過程
- iBATIS - 動態 SQL
- iBATIS - 除錯
- iBATIS - Hibernate
- iBATOR- 簡介
- iBATIS 有用資源
- iBATIS - 快速指南
- iBATIS - 有用資源
- iBATIS - 討論
iBATIS - 刪除操作
本章介紹如何使用 iBATIS 從表中刪除記錄。
我們在 MySQL 中有以下 EMPLOYEE 表 -
CREATE TABLE EMPLOYEE ( id INT NOT NULL auto_increment, first_name VARCHAR(20) default NULL, last_name VARCHAR(20) default NULL, salary INT default NULL, PRIMARY KEY (id) );
假設此表有兩個記錄,如下所示 -
mysql> select * from EMPLOYEE; +----+------------+-----------+--------+ | id | first_name | last_name | salary | +----+------------+-----------+--------+ | 1 | Zara | Ali | 5000 | | 2 | Roma | Ali | 3000 | +----+------------+-----------+--------+ 2 row in set (0.00 sec)
Employee POJO 類
要執行刪除操作,您無需修改 Employee.java 檔案。讓我們使其保持在上一個章節中的狀態。
public class Employee {
private int id;
private String first_name;
private String last_name;
private int salary;
/* Define constructors for the Employee class. */
public Employee() {}
public Employee(String fname, String lname, int salary) {
this.first_name = fname;
this.last_name = lname;
this.salary = salary;
}
/* Here are the required method definitions */
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return first_name;
}
public void setFirstName(String fname) {
this.first_name = fname;
}
public String getLastName() {
return last_name;
}
public void setlastName(String lname) {
this.last_name = lname;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
} /* End of Employee */
Employee.xml 檔案
要使用 iBATIS 定義 SQL 對映語句,我們會在 Employee.xml 中新增 <delete> 標記,並且在這個標記定義內,我們將定義一個會用於 IbatisDelete.java 檔案中,用於對資料庫執行 SQL DELETE 查詢的“id”。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Employee">
<insert id="insert" parameterClass="Employee">
INSERT INTO EMPLOYEE(first_name, last_name, salary)
values (#first_name#, #last_name#, #salary#)
<selectKey resultClass="int" keyProperty="id">
select last_insert_id() as id
</selectKey>
</insert>
<select id="getAll" resultClass="Employee">
SELECT * FROM EMPLOYEE
</select>
<update id="update" parameterClass="Employee">
UPDATE EMPLOYEE
SET first_name = #first_name#
WHERE id = #id#
</update>
<delete id="delete" parameterClass="int">
DELETE FROM EMPLOYEE
WHERE id = #id#
</delete>
</sqlMap>
IbatisDelete.java 檔案
此檔案具有應用程式級別邏輯,用於從 Employee 表中刪除記錄 -
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
public class IbatisDelete{
public static void main(String[] args)
throws IOException,SQLException{
Reader rd = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlMapClient smc = SqlMapClientBuilder.buildSqlMapClient(rd);
/* This would delete one record in Employee table. */
System.out.println("Going to delete record.....");
int id = 1;
smc.delete("Employee.delete", id );
System.out.println("Record deleted Successfully ");
System.out.println("Going to read records.....");
List <Employee> ems = (List<Employee>)
smc.queryForList("Employee.getAll", null);
Employee em = null;
for (Employee e : ems) {
System.out.print(" " + e.getId());
System.out.print(" " + e.getFirstName());
System.out.print(" " + e.getLastName());
System.out.print(" " + e.getSalary());
em = e;
System.out.println("");
}
System.out.println("Records Read Successfully ");
}
}
編譯和執行
以下是在編譯和執行上述軟體的步驟。確保在繼續進行編譯和執行之前,已經正確設定了 PATH 和 CLASSPATH。
- 如上所示建立 Employee.xml。
- 如上所示建立 Employee.java 並編譯它。
- 如上所示建立 IbatisDelete.java 並編譯它。
- 執行 IbatisDelete 二進位制檔案以執行該程式。
您將獲得以下結果,並且 ID = 1 的記錄將從 EMPLOYEE 表中刪除,並且會讀取其餘記錄。
Going to delete record..... Record deleted Successfully Going to read records..... 2 Roma Ali 3000 Records Read Successfully
廣告