MyBatis知多少(23)MyBatis結果映射

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)

 

Employee POJO 類:

使用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 文件:

在這裏,咱們將修改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>

 

IbatisResultMap.java 文件:

文件將應用程序級別的邏輯,從使用結果映射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

系列文章:

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)更新操做

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

相關文章
相關標籤/搜索