iBATISを使ってみました。
設定
Mavenを使ってjarファイルを取得しました。データベースはMySQLを使用します。
<dependencies>
<dependency>
<groupId>org.apache.ibatis</groupId>
<artifactId>ibatis-sqlmap</artifactId>
<version>2.3.4.726</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
サンプル
基本的なCRUD属性の処理を一通り試してみました。
まず、iBATISの設定ファイルです。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings useStatementNamespaces="true" />
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/sample" />
<property name="JDBC.Username" value="user" />
<property name="JDBC.Password" value="password" />
</dataSource>
</transactionManager>
<sqlMap resource="ibatis/employee-mapping.xml" />
</sqlMapConfig>
DBの設定とマッピングファイルのパスを指定します。
次に、マッピングファイルです。
<?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">
<select id="select" resultClass="Employee">
<![CDATA[
select
id,
name,
age,
department
from
employee
where id = #value#
]]>
</select>
<select id="selectAll" resultClass="Employee">
<![CDATA[
select
id,
name,
age,
department
from
employee
]]>
</select>
<update id="update" parameterClass="Employee">
<![CDATA[
update
employee
set
name = #name#,
age = #age#,
department = #department#
where
id = #id#
]]>
</update>
<insert id="insert" parameterClass="Employee">
<![CDATA[
insert into
employee(name, age, department)
values(#name#, #age#, #department#)
]]>
</insert>
<delete id="delete" parameterClass="Employee">
<![CDATA[
delete from
employee
where
name = #name# and
age = #age# and
department = #department#
]]>
</delete>
</sqlMap>
おっと、忘れてました。
データベースには以下のテーブルを作成しておきます。
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`department` varchar(32) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB
で、上のDBがマッピングされるEntity Beanを作成します。
public class Employee {
public int id;
public String name;
public int age;
public String department;
public int getId() {
return id;
}
public Employee(){}
public Employee(String name, int age, String department) {
this.name = name;
this.age = age;
this.department = department;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(id);
sb.append(" ");
sb.append(name);
sb.append(" ");
sb.append(age);
sb.append(" ");
sb.append(department);
return sb.toString();
}
}
最後に、メインクラスです。
import java.io.Reader;
import java.util.List;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class Main {
public static void main(String[] args) throws Exception {
Reader reader = Resources.getResourceAsReader("ibatis/ibatis-config.xml");
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
/*
* select an employee by his/her id
*/
Employee employee = (Employee) sqlMap.queryForObject("employee.select", 1);
System.out.println(employee);
/*
* select all employees
*/
List<Employee> employees = sqlMap.queryForList("employee.selectAll");
/*
* update the selected employee
*/
employee.setAge(employee.getAge()+1);
sqlMap.update("employee.update", employee);
/*
* insert a new employee
*/
sqlMap.insert("employee.insert", new Employee("nichole", 22, "Finance"));
employees = sqlMap.queryForList("employee.selectAll");
/*
* delete an employee
*/
sqlMap.delete("employee.delete", new Employee("nichole", 22, "Finance"));
employees = sqlMap.queryForList("employee.selectAll");
}
}
シンプルで使いやすいですね。