> Hello World !!!

     

@syaku

테스트용 코드관리 모듈 개발 #1 SqlMap : Struts2 + iBATIS is Sample

반응형

  

Oracle Schema

-- DROP TABLE COMMON_CODE CASCADE CONSTRAINTS;

CREATE TABLE COMMON_CODE (
       CODE_ID              CHAR(3) NOT NULL,
       CODE_TITLE           VARCHAR2(250) NULL,
       CODE_COMMENT         VARCHAR2(3000) NULL,
       CODE_PARENT          CHAR(3) NULL,
       CODE_CHILD           CHAR(3) NULL,
       CODE_USE             CHAR(1) DEFAULT 'N' NOT NULL,
       CODE_RDATE           DATE NULL
);

CREATE UNIQUE INDEX XPKCOMMON_CODE ON COMMON_CODE
(
       CODE_ID                        ASC
);

ALTER TABLE COMMON_CODE
       ADD  ( PRIMARY KEY (CODE_ID) ) ;

CodeSqlMap.xml : iBatis Map 설정

<?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>
  <!-- // iBatis 프로퍼티 설정 정보 불러옴 -->
  <properties resource="SqlMapConfig.properties" />

  <settings
    cacheModelsEnabled="true"
    enhancementEnabled="true"
    lazyLoadingEnabled="true"
    maxRequests="32"
    maxSessions="10"
    maxTransactions="5"
    useStatementNamespaces="false"
  />

  <transactionManager type="JDBC" >
    <!--
    <dataSource type="SIMPLE">
      <property name="JDBC.Driver" value="${driver}"/>
      <property name="JDBC.ConnectionURL" value="${url}"/>
      <property name="JDBC.Username" value="${username}"/>
      <property name="JDBC.Password" value="${password}"/>
    </dataSource>
    -->
      <dataSource type="JNDI">
          <!-- // iBatis 프로퍼티 data_source_name 정보 불러옴 -->
          <property name="DataSource" value="${data_source_name}" />
      </dataSource>
  </transactionManager>

  <!-- // 쿼리스트링을 보유한 sqlMap 을 정의함 -->
  <sqlMap resource="com/xf/modules/code/CodeSql.xml"/>
</sqlMapConfig>

CodeSql.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">

<!--
/**
 * @xml CodeSql
 * @brief 코드관리
 *
 * registered date 20090327
 * updated date 20090327
 * programmed by Seok Kyun. Choi. (최석균)
 * http://syaku.tistory.com
 */
-->

<sqlMap namespace="CodeSql">

  <typeAlias alias="CodeModel" type="com.xf.modules.code.CodeModel"/>

  <!-- @ 일반 컬럼 -->
  <sql id="coulmns-code">
    CODE_ID,
    CODE_TITLE,
    CODE_COMMENT,
    CODE_PARENT,
    CODE_CHILD,
    CODE_USE,
    CODE_RDATE
  </sql>

  <!-- @ 조인 컬럼 -->
  <sql id="join-coulmns-code">
    CC.CODE_ID,
    CC.CODE_TITLE,
    CC.CODE_COMMENT,
    CC.CODE_PARENT,
    CC.CODE_CHILD,
    CC.CODE_USE,
    CC.CODE_RDATE
  </sql>

  <!--@ 전체 레코드 -->
  <select id="select-code" parameterClass="CodeModel" resultClass="CodeModel">
    SELECT
    <include refid="coulmns-code" />
    FROM COMMON_CODE
    <isNotEmpty prepend="WHERE" property="code_id" >
      code_id = #code_id#
    </isNotEmpty>
  </select>

  <!-- @ 전체 레코드 수 -->
  <select id="select-code-count" parameterClass="CodeModel" resultClass="int">
    SELECT COUNT(CODE_ID) FROM COMMON_CODE
  </select>

  <!--@ insert -->
  <insert id="insert-code" parameterClass="CodeModel">
    INSERT INTO COMMON_CODE
    (
      CODE_ID,
      CODE_TITLE,
      CODE_COMMENT,
      CODE_PARENT,
      CODE_CHILD,
      CODE_USE,
      CODE_RDATE
    ) VALUES (
      #code_id#,
      #code_title#,
      #code_comment#,
      #code_parent#,
      #code_child#,
      #code_use#,
      SYSDATE
    )
  </insert>

  <!--@ update -->
  <update id="update-code" parameterClass="CodeModel">
    UPDATE COMMON_CODE SET
    CODE_TITLE = #code_title#,
    CODE_COMMENT = #code_comment#,
    CODE_USE = #code_use#
    WHERE CODE_ID = #code_id#
  </update>

  <!--@ delete -->
  <delete id="delete-code" parameterClass="CodeModel">
    DELETE FROM COMMON_CODE WHERE code_id = #code_id#
  </delete>

</sqlMap>

struts.xml : 스트럿츠 기본

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
  <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  <constant name="struts.devMode" value="false" />

  <include file="com/xf/modules/code/Code.xml" />
</struts>
Code.xml : 액션 처리

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

  <package name="code" namespace="/xf/code" extends="struts-default">
    <default-action-ref name="CodeAdmin" />

    <action name="CodeAdmin" class="com.xf.modules.code.CodeAction">
      <result name="list">/xf/modules/code/default/code.admin.list.jsp</result>
      <result name="input">/xf/modules/code/default/code.admin.input.jsp</result>
      <result  type="redirectAction">
        <param name="actionName">CodeAdmin</param>
      </result>
    </action>

  </package>

</struts>
CodeModel.java : 코드관리 모델

/**
 * @class CodeModel
 * @brief 코드관리
 *
 * registered date 20090327
 * updated date 20090327
 * programmed by Seok Kyun. Choi. (최석균)
 * http://syaku.tistory.com
 */

package com.xf.modules.code;

import java.util.Date;

public class CodeModel {
  private String code_id;
  private String code_title;
  private String code_comment;
  private String code_parent;
  private String code_child;
  private String code_use;
  private Date code_rdate;

  public String getCode_id() { return code_id; }
  public void setCode_id(String code_id) { this.code_id = code_id; }

  public String getCode_title() { return code_title; }
  public void setCode_title(String code_title) { this.code_title = code_title; }

  public String getCode_comment() { return code_comment; }
  public void setCode_comment(String code_comment) { this.code_comment = code_comment; }

  public String getCode_parent() { return code_parent; }
  public void setCode_parent(String code_parent) { this.code_parent = code_parent; }

  public String getCode_child() { return code_child; }
  public void setCode_child(String code_child) { this.code_child = code_child; }

  public String getCode_use() { return code_use; }
  public void setCode_use(String code_use) { this.code_use = code_use; }

  public Date getCode_rdate() { return code_rdate; }
  public void setCode_rdate(Date code_rdate) { this.code_rdate = code_rdate; }

}

written by Seok Kyun. Choi. 최석균.
http://www.cyworld.com/syaku
http://syaku.tistory.com

반응형