iBATIS的读操作

2018-12-09 11:01 更新

我们讨论,在最后一章,如何执行创建使用iBATIS表操作。本章介绍如何使用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 |
+----+------------+-----------+--------+
1 row in set (0.00 sec)

员工PO​​JO类

要执行读操作,我们将修改Employee类中Employee.java如下 -

public class Employee {
   private int id;
   private String first_name; 
   private String last_name;   
   private int salary;  

   /* Define constructors for the Employee class. */
   public Employee() {}
  
   public Employee(String fname, String lname, int salary) {
      this.first_name = fname;
      this.last_name = lname;
      this.salary = salary;
   }

   /* Here are the method definitions */
   public int getId() {
      return id;
   }
	
   public String getFirstName() {
      return first_name;
   }
	
   public String getLastName() {
      return last_name;
   }
	
   public int getSalary() {
      return salary;
   }
	
} /* End of Employee */

Employee.xml文件

要定义使用iBATIS SQL映射语句中,我们将增加的<select>在Employee.xml文件,这个标签定义标记内,我们将定义将在IbatisRead.java文件中,用于对数据库执行SQL SELECT查询的“身份证”。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="Employee">

   <insert id="insert" parameterClass="Employee">
      INSERT INTO EMPLOYEE(first_name, last_name, salary)
      values (#first_name#, #last_name#, #salary#)

      <selectKey resultClass="int" keyProperty="id">
         select last_insert_id() as id
      </selectKey>
   </insert>
	
   <select id="getAll" resultClass="Employee">
      SELECT * FROM EMPLOYEE
   </select>
	
</sqlMap>

在这里,我们没有使用WHERE子句与SQL SELECT语句。我们将证明,在接下来的章节中,你怎么能与WHERE SELECT语句子句如何使用可以传递值到WHERE子句。

IbatisRead.java文件

这个文件的应用程序级的逻辑读取雇员表中的记录 -

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 IbatisRead{
   public static void main(String[] args)throws IOException,SQLException{
      Reader rd = Resources.getResourceAsReader("SqlMapConfig.xml");
      SqlMapClient smc = SqlMapClientBuilder.buildSqlMapClient(rd);

      /* This would read all records from the Employee table. */
      System.out.println("Going to read records.....");
      List <Employee> ems = (List<Employee>)
         smc.queryForList("Employee.getAll", null);
      Employee em = null;
		
      for (Employee e : ems) {
         System.out.print("  " + e.getId());
         System.out.print("  " + e.getFirstName());
         System.out.print("  " + e.getLastName());
         System.out.print("  " + e.getSalary());
         em = e; 
         System.out.println("");
      }    
		
      System.out.println("Records Read Successfully ");
   }
} 

编译和运行

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

  • 创建Employee.xml如上所示。
  • 创建Employee.java如上图所示,并对其进行编译。
  • 创建IbatisRead.java如上图所示,并对其进行编译。
  • 执行IbatisRead二进制运行程序。

你会得到下面的结果,并创下会从EMPLOYEE表如下读 -

Going to read records.....
   1  Zara  Ali  5000
Record Reads Successfully

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号