MyBatis知多少(22)MyBatis刪除操做

本節從表中使用MyBatis刪除記錄。html

咱們已經在MySQL下有EMPLOYEE表:java

1 CREATE TABLE EMPLOYEE (
2    id INT NOT NULL auto_increment,
3    first_name VARCHAR(20) default NULL,
4    last_name  VARCHAR(20) default NULL,
5    salary     INT  default NULL,
6    PRIMARY KEY (id)
7 );

 

假設這個表是有兩條記錄以下:mysql

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文件。sql

 1 public class Employee {
 2   private int id;
 3   private String first_name; 
 4   private String last_name;   
 5   private int salary;  
 6 
 7   /* Define constructors for the Employee class. */
 8   public Employee() {}
 9   
10   public Employee(String fname, String lname, int salary) {
11     this.first_name = fname;
12     this.last_name = lname;
13     this.salary = salary;
14   }
15 
16  /* Here are the required method definitions */
17   public int getId() {
18     return id;
19   }
20   public void setId(int id) {
21     this.id = id;
22   }
23   public String getFirstName() {
24     return first_name;
25   }
26   public void setFirstName(String fname) {
27     this.first_name = fname;
28   }
29   public String getLastName() {
30     return last_name;
31   }
32   public void setlastName(String lname) {
33     this.last_name = lname;
34   }
35   public int getSalary() {
36     return salary;
37   }
38   public void setSalary(int salary) {
39     this.salary = salary;
40   }
41 
42  } /* End of Employee */

 

Employee.xml 文件:

要定義使用MyBatis SQL映射語句中,咱們將增長<Delete>鍵標籤Employee.xml文件,這個標籤訂義中,咱們會定義將用於在IbatisDelete.java文件執行SQL DELETE查詢數據庫的「id」。數據庫

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE sqlMap 
 3 PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
 4 "http://ibatis.apache.org/dtd/sql-map-2.dtd">
 5 
 6 <sqlMap namespace="Employee">
 7 <insert id="insert" parameterClass="Employee">
 8    INSERT INTO EMPLOYEE(first_name, last_name, salary)
 9    values (#first_name#, #last_name#, #salary#)
10 
11    <selectKey resultClass="int" keyProperty="id">
12       select last_insert_id() as id
13    </selectKey>
14 
15 </insert>
16 
17 <select id="getAll" resultClass="Employee">
18    SELECT * FROM EMPLOYEE
19 </select>
20 
21 <update id="update" parameterClass="Employee">
22    UPDATE EMPLOYEE
23    SET    first_name = #first_name#
24    WHERE  id = #id#
25 </update>
26 
27 <delete id="delete" parameterClass="int">
28    DELETE FROM EMPLOYEE
29    WHERE  id = #id#
30 </delete>
31 
32 </sqlMap>

 

IbatisDelete.java 文件:

文件將應用程序級別的邏輯從Employee表中刪除記錄:apache

 
 1 import com.ibatis.common.resources.Resources;
 2 import com.ibatis.sqlmap.client.SqlMapClient;
 3 import com.ibatis.sqlmap.client.SqlMapClientBuilder;
 4 import java.io.*;
 5 import java.sql.SQLException;
 6 import java.util.*;
 7 
 8 public class IbatisDelete{
 9   public static void main(String[] args)
10    throws IOException,SQLException{
11    Reader rd = Resources.getResourceAsReader("SqlMapConfig.xml");
12    SqlMapClient smc = SqlMapClientBuilder.buildSqlMapClient(rd);
13 
14    /* This would delete one record in Employee table. */
15    System.out.println("Going to delete record.....");
16    int id = 1;
17 
18    smc.delete("Employee.delete", id );
19    System.out.println("Record deleted Successfully ");
20 
21    System.out.println("Going to read records.....");
22    List <Employee> ems = (List<Employee>)
23                         smc.queryForList("Employee.getAll", null);
24    Employee em = null;
25    for (Employee e : ems) {
26       System.out.print("  " + e.getId());
27       System.out.print("  " + e.getFirstName());
28       System.out.print("  " + e.getLastName());
29       System.out.print("  " + e.getSalary());
30       em = e; 
31       System.out.println("");
32    }    
33 
34    System.out.println("Records Read Successfully ");
35 
36   }
37 }

 

編譯和運行:

下面是步驟來編譯並運行上述軟件。請確保已在進行的編譯和執行以前,適當地設置PATH和CLASSPATH。ui

  • 建立Employee.xml如上所示。this

  • 建立Employee.java如上圖所示,並編譯它。spa

  • 建立IbatisDelete.java如上圖所示,並編譯它。code

  • 執行IbatisDelete二進制文件來運行程序。

獲得如下結果,ID=1將在EMPLOYEE表中被刪除,並讀取EMPLOYEE表中的一條記錄。

Going to delete record.....
Record deleted Successfully
Going to read records.....
  2  Roma  Ali  3000
Records Read Successfully

系列文章:

MyBatis知多少(1)

MyBatis知多少(2)

MyBatis知多少(3)

MyBatis知多少(4)MyBatis的優點

MyBatis知多少(5)業務對象模型

MyBatis知多少(6)表現層與業務邏輯層

MyBatis知多少(7)持久層

MyBatis知多少(8)關係型數據庫

MyBatis知多少(9)不一樣類型的數據庫

MyBatis知多少(10)應用程序數據庫

MyBatis知多少(11)企業數據庫

MyBatis知多少(12)私有數據庫

MyBatis知多少(13)MyBatis如何解決數據庫的常見問題

MyBatis知多少(14)分散的數據庫系統

MyBatis知多少(15)數據模型

MyBatis知多少(16)MyBatis映射

MyBatis知多少(17)MyBatis和JDBC

MyBatis知多少(18)MyBatis系統

MyBatis知多少(19)MyBatis操做

MyBatis知多少(20)MyBatis讀取操做

MyBatis知多少(21)更新操做

相關文章
相關標籤/搜索