728x90
반응형

iBatis기본 설정 및 사용방법(회원가입 및 수정 탈퇴 예제)입니다.

war파일 첨부 합니다.

 

필요한 jar 파일들(war파일에 모두 포함되어 있습니다!!)

ibatis-2.3.4.726.jar(ibatis)

jstl.jar(jstl)

standard.jar(jstl)

ojdbc14.jar(jdbc)

 

데이터베이스(Oracle)

테이블 구성(user_t 테이블)

 

열 이름 데이터 유형 널 가능 기본값  기본 키
USRID VARCHAR2(20)     No - 1
UNAME VARCHAR2(30) No - -
UPW VARCHAR2(20) Yes - -
UCMT VARCHAR2(4000) Yes - -

 

 

 

 

 

=============== IBatisUtil.java ==================

package exam.ibatis.util;

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;

public class IBatisUtil {
  private static SqlMapClient sqlMapper;
 
  static {
      try {
       //클래스 패스 경로
        Reader reader = Resources.getResourceAsReader(

                                  "exam/ibatis/sqlmap/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);
      }
  }
  
  public static SqlMapClient getSqlMapClient(){
   return sqlMapper;
 }

}

 

 

 

=============== 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.driver.OracleDriver"/>
      <property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@127.0.0.1:1521:xe"/>
      <property name="JDBC.Username" value="java"/>
      <property name="JDBC.Password" value="java"/>
    </dataSource>
  </transactionManager>

  <!-- List the SQL Map XML files. They can be loaded from the
       classpath, as they are here (com.domain.data...) -->
  <sqlMap resource="exam/ibatis/sqlmap/user.xml"/>
  <!-- List more here...
  <sqlMap resource="com/mydomain/data/Order.xml"/>
  <sqlMap resource="com/mydomain/data/Documents.xml"/>
  -->

</sqlMapConfig>

 

 

 

=============== user.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="user_t">

  <!-- Use type aliases to avoid typing the full classname every time. -->
  <typeAlias alias="user" type="exam.ibatis.dto.UserDto"/>

  <!-- 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. -->
 
  <!-- Select with no parameters using the result map for Account class. -->
  <select id="selectAllUsers" resultClass="exam.ibatis.dto.UserDto">
    select usrid,uname,upw,ucmt,rn
    from(select usrid,uname,upw,ucmt,ceil(rownum/30)as rn
    from(select * from user_t order by usrid desc))a
    where rn = 1
  </select>
 
  <!-- 회원 등록 -->
  <insert id="registerUser" parameterClass="exam.ibatis.dto.UserDto">
  insert into user_t (usrid,uname,upw,ucmt)
  values(#usrid#,#uname#,#upw#,#ucmt#)
  </insert>
 
  <!-- 상세 보기용  -->
  <select id="selectUser" parameterClass="String" resultClass="exam.ibatis.dto.UserDto">
    select * from user_t where usrid = #usrid#
  </select>
 
  <!-- 정보 수정 -->
  <insert id="updateUser" parameterClass="exam.ibatis.dto.UserDto">
  update user_t set upw=#newUpw#,uname=#uname# where usrid = #usrid#
  </insert>
 
   <!-- 회원 탈퇴 -->
  <insert id="deleteUser" parameterClass="String">
  delete user_t where usrid=#usrid#
  </insert>
 
</sqlMap>

 

 

================= UserDao.java ===============

package exam.ibatis.dao;

import java.sql.SQLException;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

import exam.ibatis.dto.UserDto;
import exam.ibatis.util.IBatisUtil;

 

public class UserDao {
 
 public void insert(UserDto dto) throws ClassNotFoundException, SQLException{
  SqlMapClient client = IBatisUtil.getSqlMapClient();
  client.insert("registerUser", dto);
 }//end insert
 
 public List<UserDto>  list() throws ClassNotFoundException, SQLException{
  SqlMapClient client = IBatisUtil.getSqlMapClient();
  return client.queryForList("selectAllUsers");
 }

 
 public UserDto findById( String usrid ) throws ClassNotFoundException, SQLException{
  SqlMapClient client = IBatisUtil.getSqlMapClient();
  return (UserDto)client.queryForObject("selectUser",usrid);
 }
 
 public void update( UserDto dto ) throws ClassNotFoundException, SQLException{
  SqlMapClient client = IBatisUtil.getSqlMapClient();
  client.update("updateUser", dto);
 }
 
 public void delete( String usrid) throws ClassNotFoundException, SQLException{
  SqlMapClient client = IBatisUtil.getSqlMapClient();
  client.delete("deleteUser",usrid);
 }

}//end class

 

728x90
반응형
블로그 이미지

nineDeveloper

안녕하세요 현직 개발자 입니다 ~ 빠르게 변화하는 세상에 뒤쳐지지 않도록 우리모두 열심히 공부합시다 ~! 개발공부는 넘나 재미있는 것~!

,