阅读(4539) (0)

iBATIS的创建操作

2016-12-09 11:01:48 更新

要执行使用iBATIS的创建,读取,更新和删除(CRUD)操作,你需要创建一个普通Java对象(PO​​JO)对应的表类。本课程介绍对象,这将“模型”的数据库表行。

POJO类将不得不实施所有执行所需操作所需要的方法。

让我们假设我们有在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)
);

员工PO​​JO类

我们将在Employee.java文件中创建一个Employee类如下 -

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;
   }
} /* End of Employee */

可以定义方法来设置表中的各个字段。下一章将介绍如何获取各个字段的值。

Employee.xml文件

要定义使用iBATIS SQL映射语句中,我们将使用<插入>标记和这个标签定义中,我们可以定义将在IbatisInsert.java文件中,用于对数据库执行SQL INSERT查询的“身份证”。

<?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> 

</sqlMap>

这里parameterClass -可能需要一个值作为字符串,整数,浮点,双 ,或根据需求任意类对象 。在这个例子中,我们将通过Employee对象作为参数,同时呼吁的SqlMap类的插入方法。

如果数据库表使用IDENTITY,AUTO_INCREMENT或串行列或您已经定义了一个SEQUENCE /发电机,可以使用<selectKey元素>元素在<插入>语句中使用或返回数据库生成的值。

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

      /* This would insert one record in Employee table. */
      System.out.println("Going to insert record.....");
      Employee em = new Employee("Zara", "Ali", 5000);

      smc.insert("Employee.insert", em);

      System.out.println("Record Inserted Successfully ");
   }
} 

编译和运行

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

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

你会得到下面的结果,并创下会在EMPLOYEE表中创建。

$java IbatisInsert
Going to insert record.....
Record Inserted Successfully

如果检查EMPLOYEE表,它应该显示以下结果 -

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