Ibatis 简明教程
iBATIS - Create Operation
要执行任何使用iBATIS的创建、读取、更新和删除(CRUD)操作,您需要创建一个与该表对应的纯Java对象(POJO)类。此类描述将“建模”数据库表行的对象。
To perform any Create, Read, Update, and Delete (CRUD) operation using iBATIS, you would need to create a Plain Old Java Objects (POJO) class corresponding to the table. This class describes the objects that will "model" database table rows.
POJO 类具有实现执行所需操作的所有方法。
The POJO class would have implementation for all the methods required to perform desired operations.
我们假设在 MySQL 中具有以下 EMPLOYEE 表:
Let us assume we have the following EMPLOYEE table in MySQL −
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 类创建如下:
We would create an Employee class in Employee.java file as follows −
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 */
您可以定义用于在表中设置单个字段的方法。下一章将说明如何获取各个字段的值。
You can define methods to set individual fields in the table. The next chapter explains how to get the values of individual fields.
Employee.xml File
为了使用 iBATIS 定义 SQL 映射语句,我们将使用 <insert> 标记,在此标记定义内我们将定义一个“id”,该 id 将在 IbatisInsert.java 文件中用于在数据库上执行 SQL INSERT 查询。
To define SQL mapping statement using iBATIS, we would use <insert> tag and inside this tag definition, we would define an "id" which will be used in IbatisInsert.java file for executing SQL INSERT query on database.
<?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 − 可根据要求采用字符串、int、float、double 或任何类对象作为值。在此示例中,在调用 SqlMap 类的 insert 方法时,我们将 Employee 对象作为参数传递。
Here parameterClass − could take a value as string, int, float, double, or any class object based on requirement. In this example, we would pass Employee object as a parameter while calling insert method of SqlMap class.
如果数据库表使用 IDENTITY、AUTO_INCREMENT 或 SERIAL 列,或者您已定义一个 SEQUENCE/GENERATOR,则可以在 <insert> 语句中使用 <selectKey> 元素以使用或返回该数据库生成的值。
If your database table uses an IDENTITY, AUTO_INCREMENT, or SERIAL column or you have defined a SEQUENCE/GENERATOR, you can use the <selectKey> element in an <insert> statement to use or return that database-generated value.
IbatisInsert.java File
此文件将具有插入 Employee 表中记录的应用程序级逻辑:
This file would have application level logic to insert records in the Employee table −
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。
Here are the steps to compile and run the above mentioned software. Make sure you have set PATH and CLASSPATH appropriately before proceeding for compilation and execution.
-
Create Employee.xml as shown above.
-
Create Employee.java as shown above and compile it.
-
Create IbatisInsert.java as shown above and compile it.
-
Execute IbatisInsert binary to run the program.
您将获得以下结果,并且会在 EMPLOYEE 表中创建一条记录。
You would get the following result, and a record would be created in the EMPLOYEE table.
$java IbatisInsert
Going to insert record.....
Record Inserted Successfully
如果您检查 EMPLOYEE 表,它应显示以下结果:
If you check the EMPLOYEE table, it should display the following result −
mysql> select * from EMPLOYEE;
+----+------------+-----------+--------+
| id | first_name | last_name | salary |
+----+------------+-----------+--------+
| 1 | Zara | Ali | 5000 |
+----+------------+-----------+--------+
1 row in set (0.00 sec)