반응형

iBATIS - Create Operation


(원문 취치 : http://www.tutorialspoint.com/ibatis/ibatis_create_operation.htm )

*** iBATIS를 실제 동작시키려다 보니 오류가 발생하는 등의 문제가 발생하여 이리저리 알아보다 보니 아무래도 이 기술은 이제는 많이 사용하지 않는 것 같았다. 
*** 따라서 다른 대체 기술을 알아보고 그것으로 다시 진행해 볼 생각이다. (MyBatis?!)

iBATIS를 사용하여 CRUC(Creat, Write, Update, Delete)를 수행하기 위해서는 table과 일치하는 POJO(Plain old Java Objects)를 만들어야 한다. 이 클래스는 database table row를 형성(model)할 객체를 나타낸다.

POJO 클래스는 사용하고자하는 동작을 수행하기 위해 요구되어지는 모든 method에 대한 구현을 갖는다.

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 */

테이블에서 각각의 필드를 설정하기 위한 method를 정의할 수 있다. 다음 장은 어떻게 각각 필드의 값을 가지고 오는지를 설명한다.


iBATIS를 사용하여 SQL mapping 문장을 정의하려면, <insert> 태그와 이 태그 정의 내부 - 데이터베이스에 INSERT SQL 쿼리를 실행하기 위한 IbatisInsert.java파일에서 사용될 'id'를 정의하는 - 를 사용할 것이다. 

<?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 도는 어떤 클래스 객체를 갖는. 이번 예제에서는 SqlMap클래스의 insert method를 호출하는 동안 인자(parameter)로써 Employee객체를 전달할 것이다.


만약 database 테이블이 IDENTITY, AUTO_INCREMENT, SERIAL column을 사용하거나 SEQUENCE/GENERAOTR를 정의하였다면, database가 생성한 값을 반환 또는 사용하기 위하여 <insert>문장에서 <selectKey> 요소(element)를 사용할 수 있다.


IbatisInsert.java File

이 파일은 Employee 테이블에 레코드를 insert하기 위한 application level의 로직을 갖는다.

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

위에 언급되어진 프로그램을 컴파일하고 실해아기 위한 단계이다.


반응형

+ Recent posts