本节从表中使用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知多少(13)MyBatis如何解决数据库的常见问题