Tools SQL2iBatis

황제낙엽 2007.08.13 16:20 조회 수 : 106 추천:116

sitelink1  
sitelink2  
extra_vars5  
extra_vars6  
http://openframework.or.kr/JSPWiki/Wiki.jsp?page=Sql2ibatisSQL문이 저장되어 있는 sql파일을 인자로 줘서 iBatis용 자바소스와 SQL Maps 맵핑 xml파일 생성하기

작성자 : 이동국

개요

http://alxeg.narod.ru/ibatis/index.html 에서 최신버전을 다운로드 받을수 있다. 해당 프로그램은 perl에서 작동하는 프로그램이기 때문에 http://www.perl.com/download.csp 에서 해당 운영체제에 대한 perl을 다운로드 받아야 한다.

사용법

sql2iBatis.pl -input=sql파일 <옵션>

옵션으로 들어가는 사항은 다음과 같다.

-package=<패키지명> 주어진 패키지아래에 자바소스가 생성된다.
-force 강제로 파일을 다시 생성한다.
-beansout java-beans가 생성되는 폴더를 지정한다(해당 폴더는 생성이 되어 있어야 한다).
-mapsout 맵핑 파일이 생성되는 폴더를 지정한다(해당 폴더는 생성이 되어 있어야 한다).
-mapsinc 사용자 정의 맵핑된 statement를 포함하는 파일의 확장자
-beansinc 사용자 정의 자바빈즈 코드를 포함하는 파일의 확장자

사용예>

  1. sql2ibatis.pl -input=user_property.sql -package=kr.or.openframework
  2. sql2ibatis.pl -input=user_property.sql -beansout=.openframeworkbeans -mapsout=.openframeworkmaps
..

주의
실행해보면 1번의 경우처럼 -package옵션만 주면 맵핑 파일인 xml파일이 생성이 되지 않는 경우가 있다. 실행후 xml파일의 생성여부를 필히 확인하길 바란다.

예제

  • 인자로 넣어줄 sql파일(user_property.sql)

CREATE TABLE USER_PROPERTY (
       USER_ID BIGINT NOT NULL
     , FIRST_NAME VARCHAR(100DEFAULT ''
     , LAST_NAME VARCHAR(100)
     , BIRTH_DATE TIMESTAMP(8)
     , PRIMARY KEY (USER_ID)
);
= END of automatically generated HTML code = ========================================================

 

  • 실행후 생성되는 java파일


// WARNING: This is an autogenerated file

public class UserProperty {

  // Constructors
  public UserProperty() {
  }
  public UserProperty(long _userId) {
    this.userId = _userId;
  }

  // Fields
  private String firstName;
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String _firstName) {
    this.firstName = _firstName;
  }

  private java.util.Date birthDate;
  public java.util.Date getBirthDate() {
    return birthDate;
  }
  public void setBirthDate(java.util.Date _birthDate) {
    this.birthDate = _birthDate;
  }

  private String lastName;
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String _lastName) {
    this.lastName = _lastName;
  }

  private long userId;
  public long getUserId() {
    return userId;
  }
  public void setUserId(long _userId) {
    this.userId = _userId;
  }

}
= END of automatically generated HTML code = ========================================================

 

  • 실행후 생성되는 맵핑 xml파일

<?xml version='1.0'?>

<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
  "http://www.ibatis.com/dtd/sql-map-2.dtd">

<!-- WARNING: This is an autogenerated file -->

<sqlMap namespace="UserProperty">

  <cacheModel id="userproperty-cache" type="MEMORY">
    <flushInterval hours="24"/>
    <flushOnExecute statement="insertUserProperty"/>
    <flushOnExecute statement="updateUserProperty"/>
    <flushOnExecute statement="deleteUserProperty"/>
    <property name="reference-type" value="WEAK" />
  </cacheModel>

  <resultMap class="UserProperty" id="userproperty-result" >
    <result property="firstName" column="FIRST_NAME" />
    <result property="birthDate" column="BIRTH_DATE" />
    <result property="lastName" column="LAST_NAME" />
    <result property="userId" column="USER_ID" />
  </resultMap>

  <select id="getUserProperty" resultClass="UserProperty" parameterClass="UserProperty"
 resultMap="userproperty-result" >
    <![CDATA[
      select * from USER_PROPERTY
      where  USER_ID = #userId# 
    ]]>
  </select>

  <update id="updateUserProperty" parameterClass="UserProperty" >
    <![CDATA[
      update USER_PROPERTY
      set  FIRST_NAME = #firstName# ,  BIRTH_DATE = #birthDate# ,  LAST_NAME = #lastName# 
      where  USER_ID = #userId# 
    ]]>
  </update>

  <insert id="insertUserProperty" parameterClass="UserProperty" >
    <![CDATA[
      insert into USER_PROPERTY(FIRST_NAME, BIRTH_DATE, LAST_NAME, USER_ID)
      values(#firstName#, #birthDate#, #lastName#, #userId#)
    ]]>
  </insert>

  <delete id="deleteUserProperty" parameterClass="UserProperty" >
    <![CDATA[
      delete from USER_PROPERTY
      where  USER_ID = #userId# 
    ]]>
  </delete>

</sqlMap>
= END of automatically generated HTML code = ========================================================