目录

iBATIS - Create Operation

要使用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)
);

Employee POJO Class

我们将在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 File

要使用iBATIS定义SQL映射语句,我们将使用标记,在此标记定义中,我们将定义一个“id”,它将在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 −可以根据需要将值作为string, int, float, double或任何类object 。 在这个例子中,我们将调用Employee对象作为参数,同时调用SqlMap类的insert方法。

如果数据库表使用IDENTITY,AUTO_INCREMENT或SERIAL列,或者已定义SEQUENCE/GENERATOR,则可以使用语句中的元素来使用或返回该数据库生成的值。

IbatisInsert.java File

此文件将具有应用程序级逻辑以在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 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 ");
   }
} 

编译和运行 (Compilation and Run)

以下是编译和运行上述软件的步骤。 在继续编译和执行之前,请确保已正确设置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)
↑回到顶部↑
WIKI教程 @2018