resultMap的元素是在MyBatis的最重要和最強大的元素。您能夠經過使用MyBatis的結果映射減小高達90%的JDBC編碼,在某些狀況下,能夠讓你作JDBC不支持的事情。html
ResultMaps的設計是這樣的簡單語句不須要明確的結果映射,以及更復雜的報表要求不超過絕對必要說明的關係。java
本章將只給你一個簡單的介紹iBATIS的結果映射。mysql
咱們已經在MySQL下有EMPLOYEE表:sql
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)
使用iBATIS的結果映射,須要修改Employee.java文件。所以,讓咱們保持它,由於它已經在前一章中使用過。apache
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文件介紹<resultMap></ resultMap>標記。這個標籤就必須在咱們<select>標記的resultMap屬性運行此結果映射這是須要一個id。ui
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 8 <!-- Perform Insert Operation --> 9 <insert id="insert" parameterClass="Employee"> 10 INSERT INTO EMPLOYEE(first_name, last_name, salary) 11 values (#first_name#, #last_name#, #salary#) 12 13 <selectKey resultClass="int" keyProperty="id"> 14 select last_insert_id() as id 15 </selectKey> 16 17 </insert> 18 19 <!-- Perform Read Operation --> 20 <select id="getAll" resultClass="Employee"> 21 SELECT * FROM EMPLOYEE 22 </select> 23 24 <!-- Perform Update Operation --> 25 <update id="update" parameterClass="Employee"> 26 UPDATE EMPLOYEE 27 SET first_name = #first_name# 28 WHERE id = #id# 29 </update> 30 31 <!-- Perform Delete Operation --> 32 <delete id="delete" parameterClass="int"> 33 DELETE FROM EMPLOYEE 34 WHERE id = #id# 35 </delete> 36 37 <!-- Using ResultMap --> 38 <resultMap id="result" class="Employee"> 39 <result property="id" column="id"/> 40 <result property="first_name" column="first_name"/> 41 <result property="last_name" column="last_name"/> 42 <result property="salary" column="salary"/> 43 </resultMap> 44 <select id="useResultMap" resultMap="result"> 45 SELECT * FROM EMPLOYEE 46 WHERE id=#id# 47 </select> 48 49 </sqlMap>
文件將應用程序級別的邏輯,從使用結果映射Employee表中讀取記錄:this
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 IbatisResultMap{ public static void main(String[] args) throws IOException,SQLException{ Reader rd = Resources.getResourceAsReader("SqlMapConfig.xml"); SqlMapClient smc = SqlMapClientBuilder.buildSqlMapClient(rd); int id = 1; System.out.println("Going to read record....."); Employee e = (Employee)smc.queryForObject ("Employee.useResultMap", id); System.out.println("ID: " + e.getId()); System.out.println("First Name: " + e.getFirstName()); System.out.println("Last Name: " + e.getLastName()); System.out.println("Salary: " + e.getSalary()); System.out.println("Record read Successfully "); } }
下面是步驟來編譯並運行上述軟件。請確保您已在進行的編譯和執行以前,適當地設置PATH和CLASSPATH。編碼
建立Employee.xml如上所示。spa
建立Employee.java如上圖所示,並編譯它。
建立IbatisResultMap.java如上圖所示,並編譯它。
執行IbatisResultMap二進制文件來運行程序。
會獲得下面的結果是對EMPLOYEE表的讀操做。
Going to read record..... ID: 1 First Name: Zara Last Name: Ali Salary: 5000 Record read Successfully
系列文章: