본문 바로가기

iBATIS/CRUD for Web

iBATIS 2.3.4 를 이용한 CRUD 웹 기반 테스트

웹상에서 iBATIS 2.3.4를 이용하여 CRUD 문장을 실행하는 예

먼저 다음 라이브러리를 Tomcat/lib디렉토리 안에 복사한다.




Eclipse 3.5에서 Dynamic Web Project를 생성하고 WEB-INF/classes 안에 iBATIS설정파일(*.xml)들을 저장하면 된다. 아래 그림 참조

사용자 삽입 이미지



Employee.xml

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

  <!-- Use type aliases to avoid typing the full classname every time. -->
  <typeAlias alias="Employee" type="com.micropilot.ibatis.Employee"/>

  <!-- Result maps describe the mapping between the columns returned
       from a query, and the class properties.  A result map isn't
       necessary if the columns (or aliases) match to the properties
       exactly. -->

  <resultMap id="EmployeeResult" class="Employee">
 <result property="empno" column="EMPNO"/>
 <result property="ename" column="ENAME"/>
 <result property="hiredate" column="HIREDATE"/>
  </resultMap>

  <!-- Select with no parameters using the result map for Employee class. -->
  <select id="selectAllEmployee" resultMap="EmployeeResult">
    select * from EMPLOYEE
  </select>

  <!-- A simpler select example without the result map.  Note the
       aliases to match the properties of the target result class. -->

  <select id="selectEmployeeByEmpno" parameterClass="int" resultClass="Employee">
    select
      EMPNO as empno,
      ENAME as ename,
      HIREDATE as hiredate
    from EMPLOYEE
    where EMPNO = #empno#
  </select>
  
  <!-- Insert example, using the Employee parameter class -->
  <insert id="insertEmployee" parameterClass="Employee">
    insert into Employee (
      EMPNO,
   ENAME,
   HIREDATE
    values (
      #empno#, #ename#, #hiredate#
    )
  </insert>

  <!-- Update example, using the Employee parameter class -->
  <update id="updateEmployee" parameterClass="Employee">
    update EMPLOYEE set
      EMPNO = #empno#,
      ENAME = #ename#,
      HIREDATE = #hiredate#
    where
      EMPNO = #empno#
  </update>

  <!-- Delete example, using an integer as the parameter class -->
  <delete id="deleteEmployeeByEmpno" parameterClass="int">
    delete from Employee where EMPNO = #empno#
  </delete>

</sqlMap>




SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig     
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"     
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>

  <!-- Configure a built-in transaction manager.  If you're using an
       app server, you probably want to use its transaction manager
       and a managed datasource -->
  <transactionManager type="JDBC" commitRequired="false">
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver" value="oracle.jdbc.OracleDriver"/>
      <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@micropilot.co.kr:1521:ORCL"/>
      <property name="JDBC.Username" value="scott"/>
      <property name="JDBC.Password" value="tiger"/>
    </dataSource>
  </transactionManager>

  <!-- List the SQL Map XML files. They can be loaded from the
       classpath, as they are here (com.domain.data...) -->

  <sqlMap resource="Employee.xml"/>
  <!-- List more here...
  <sqlMap resource="com/mydomain/data/Order.xml"/>
  <sqlMap resource="com/mydomain/data/Documents.xml"/>
  -->

</sqlMapConfig>




Employee.java

package com.micropilot.ibatis;
import java.sql.Date;
import java.util.*;

public class Employee {
 
 private int empno;
 private String ename;
 private java.sql.Date hiredate;
 
 public Employee() {}

 public Employee(int empno, String ename, Date hiredate) {
  super();
  this.empno = empno;
  this.ename = ename;
  this.hiredate = hiredate;
 }

 public int getEmpno() {
  return empno;
 }

 public void setEmpno(int empno) {
  this.empno = empno;
 }

 public String getEname() {
  return ename;
 }

 public void setEname(String ename) {
  this.ename = ename;
 }

 public java.sql.Date getHiredate() {
  return hiredate;
 }

 public void setHiredate(java.sql.Date hiredate) {
  this.hiredate = hiredate;
 }
}




SimpleExample.java

package com.micropilot.ibatis;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.ibatis.common.resources.Resources;


import java.io.Reader;
import java.io.IOException;
import java.util.List;
import java.sql.SQLException;

/**
 * This is not a best practices class.  It's just an example
 * to give you an idea of how iBATIS works.  For a more complete
 * example, see JPetStore 5.0 at
http://www.ibatis.com.
 */

public class SimpleExample {

  /**
   * SqlMapClient instances are thread safe, so you only need one.
   * In this case, we'll use a static singleton.  So sue me.  ;-)
   */

  private static SqlMapClient sqlMapper;

  /**
   * It's not a good idea to put code that can fail in a class initializer,
   * but for sake of argument, here's how you configure an SQL Map.
   */

  static {
    try {
      Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
      //Reader reader = new java.io.FileReader("SqlMapConfig.xml");
      sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
      reader.close();
    } catch (IOException e) {
      // Fail fast.
      throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);
    }
  }

  /* 이 클래스 자체 테스트를 위해 작성한 main 메소드*/
  public static void main(String[] args) throws Exception {
   List list = selectAllEmployee();
   for(int i=0;i<list.size();i++){
    Employee emp = (Employee)list.get(i);
    int empno = emp.getEmpno();
    String ename = emp.getEname();
    java.sql.Date hiredate = emp.getHiredate();
    System.out.println("사번:"+empno+", 이름:"+ename+", 입사일:"+hiredate);
   }
  }
 
  public static List selectAllEmployee () throws SQLException {
    return sqlMapper .queryForList("selectAllEmployee");
  }

  public static Employee selectEmployeeById  (int empno) throws SQLException {
    return (Employee) sqlMapper .queryForObject("selectEmployeeByEmpno", empno);
  }

  public static void insertEmployee (Employee emp) throws SQLException {
    sqlMapper .insert("insertEmployee", emp);
  }

  public static void updateEmployee (Employee emp) throws SQLException {
    sqlMapper .update("updateEmployee", emp);
  }

  public static void deleteEmployee (int id) throws SQLException {
    sqlMapper .delete("deleteEmployeeByEmpno", id);
  }
}




iBATIS.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="java.util.*" %>
<%@ page import="com.micropilot.ibatis.*" %>
<%
 List list = SimpleExample.selectAllEmployee();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>iBATIS Test</title>
</head>
<body><center><br></br>
iBATIS 2.3.4를 이용한 모든 사원정보 출력
<hr width="40%">
<table>
<tr><th>사 번</th><th>이 름</th><th>입사일</th></tr>
<%
 for(int i=0;i<list.size();i++) {
  Employee e = (Employee) list.get(i);%>
  <tr><td><%=e.getEmpno()%></td><td><%=e.getEname()%></td><td><%=e.getHiredate()%></td></tr>
<% }
%>
</table>
</center>
</body>
</html>




실행결과 화면

사용자 삽입 이미지