日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MyBatis知多少(22)MyBatis删除操作

發布時間:2025/3/19 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MyBatis知多少(22)MyBatis删除操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

我們已經在MySQL下有EMPLOYEE表:

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

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表中刪除記錄:

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。

  • 創建Employee.xml如上所示。

  • 創建Employee.java如上圖所示,并編譯它。

  • 創建IbatisDelete.java如上圖所示,并編譯它。

  • 執行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)更新操作

?

轉載于:https://www.cnblogs.com/Coda/p/4687214.html

總結

以上是生活随笔為你收集整理的MyBatis知多少(22)MyBatis删除操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。