MyBatis知多少(21)更新操作

上一章展示了如何使用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 );

 

此表有如下只有一条记录:

1 mysql> select * from EMPLOYEE;
2 +----+------------+-----------+--------+
3 | id | first_name | last_name | salary |
4 +----+------------+-----------+--------+
5 |  1 | Zara       | Ali       |   5000 |
6 +----+------------+-----------+--------+
7 1 row in set (0.00 sec)

 

Employee POJO 类:

要执行UDPATE操作,需要修改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 文件:

要定义使用iBATIS SQL映射语句,我们想补充的<Update>标签Employee.xml文件,这个标签定义中,我们会定义将用于在IbatisUpdate.java文件的数据库执行SQL UPDATE查询的“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 </sqlMap>

 

IbatisUpdate.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 IbatisUpdate{
 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 update one record in Employee table. */
15    System.out.println("Going to update record.....");
16    Employee rec = new Employee();
17    rec.setId(1);
18    rec.setFirstName( "Roma");
19    smc.update("Employee.update", rec );
20    System.out.println("Record updated Successfully ");
21 
22    System.out.println("Going to read records.....");
23    List <Employee> ems = (List<Employee>)
24                   smc.queryForList("Employee.getAll", null);
25    Employee em = null;
26    for (Employee e : ems) {
27       System.out.print("  " + e.getId());
28       System.out.print("  " + e.getFirstName());
29       System.out.print("  " + e.getLastName());
30       System.out.print("  " + e.getSalary());
31       em = e; 
32       System.out.println("");
33    }    
34 
35    System.out.println("Records Read Successfully ");
36 
37   }
38 }

 

编译和运行:

下面是步骤来编译并运行上述软件。请确保您已在进行的编译和执行之前,适当地设置PATH和CLASSPATH。

  • 创建Employee.xml如上所示。

  • 创建Employee.java如上图所示,并编译它。

  • 创建IbatisUpdate.java如上图所示,并编译它。

  • 执行IbatisUpdate二进制文件来运行程序。

得到下面的结果,并创建纪录在EMPLOYEE表进行更新和更高版本相同的记录将从EMPLOYEE表中读出。

1 Going to update record.....
2 Record updated Successfully
3 Going to read records.....
4   1  Roma  Ali  5000
5 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读取操作


posted @ 2015-07-28 19:59 Coda 阅读(...) 评论(...) 编辑 收藏