MyBatis知多少(23)MyBatis结果映射
resultMap的元素是在MyBatis的最重要和最强大的元素。您可以通过使用MyBatis的结果映射减少高达90%的JDBC编码,在某些情况下,可以让你做JDBC不支持的事情。
ResultMaps的设计是这样的简单语句不需要明确的结果映射,以及更复杂的报表要求不超过绝对必要说明的关系。
本章将只给你一个简单的介绍iBATIS的结果映射。
我们已经在MySQL下有EMPLOYEE表:
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文件。因此,让我们保持它,因为它已经在前一章中使用过。
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。
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表中读取记录:
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如上所示。
-
创建Employee.java如上图所示,并编译它。
-
创建IbatisResultMap.java如上图所示,并编译它。
-
执行IbatisResultMap二进制文件来运行程序。
会得到下面的结果是对EMPLOYEE表的读操作。
Going to read record..... ID: 1 First Name: Zara Last Name: Ali Salary: 5000 Record read Successfully
系列文章:
MyBatis知多少(13)MyBatis如何解决数据库的常见问题