달력

4

« 2024/4 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
2008. 1. 23. 16:56

iBatis 따라하기 2 (중고차 정보) 그거/Tech2008. 1. 23. 16:56


# package

  ibatis.dto                     : DTO 가 있는 package
  ibatis.dao                     : DAO 프로그램이 있는 package
  ibatis.dao.sqlmapdao           : xml 파일 위치
  ibatis.biz                     : DAO, DTO를 사용해서 테스트할 프로그램이 있는 package
  ibatis.properties              : property 파일이 있는 위치

# file

  ibatis.dto.Car.java                             : DTO(자동차에 대한 간단한 정보 저장)
  ibatis.dao.SqlMapFactory.java                   : sql-map-config.xml 파일을 resource로 하는 SqlMapClient 반환
  ibatis.dao.sqlmap.CarDAO.java                   : SqlMapClient의 method들을 wrapping
  ibatis.dao.sqlmap.sql.car.xml                   : sql문이 있는 XML 파일
  ibatis.dao.sqlmap.sql.sql-map-config.xml        : sqlMap 정보(sql문이 있는 XML 파일)와 Transaction 관련 정보 XML 파일
  ibatis.biz.CarTest.java                         : iBatis를 이용해 테스트할 프로그램
  ibatis.properties.sql-map-config.properties     : DB 연결 정보가 있는 파일

# 따라하기 순서

1. 자동차 정보를 저장할 간단한 DTO인 Car.java를 만든다.

   id, company, name, year, price
   물론, getter, setter도 생성해야 한다!

2. iBatis를 이용하여 자동차 정보를 DB에서 조작하는 DAO를 만든다.

   public List<Car> getCarList(int skip, int num) throws SQLException { ... 중략 ... }
   public void insertCar(Car car) throws SQLException { ... 중략 ... }
   public void updateCar(Car car) throws SQLException { ... 중략 ... }
   public void deleteCar(Car car) throws SQLException { ... 중략 ... }
   public Car getCar(String id) throws SQLException { ... 중략 ... }
   SqlMapClient 로 부터 직접 method들을 호출할 수도 있지만, 그냥 Wrapper class를 만들었다.

3. 2번에서 생성한 DAO를 이용하여 DB 조작을 테스트하는 테스트 프로그램을 작성한다.

   insert/update/delete/select 등을 각각 수행해볼 수 있는 프로그램을 작성한다.

   Car car = new Car();
   CarDAO cardao = new CarDAO();

   // insert
   car.setCompany("Hyundai");
   car.setName("Lavita");
   car.setPrice("6,000,000");
   car.setYear("2002");
   cardao.insertCar(car);

   // select
   car = cardao.getCar("1");

   // update
   car.setPrice("8,000,000");
   cardao.update(car);

   // delete
   cardao.delete(car);

4. 테스트 프로그램에서 select/insert/update/delete 등이 호출될 때 iBatis에서 사용하는 sql query 문을 작성한다.

 <select id="getCar" resultClass="car">
  SELECT ID, NAME ... 중략 ...
  
 <insert id="insertCar" parameterClass="car">
  INSERT INTO CAR ... 중략 ...

 <update id="updateCar" parameterClass="car">
  UPDATE CAR SET  ... 중략 ...

 <delete id="deleteCar" parameterClass="car">
  DELETE FROM CAR ... 중략 ...

 <select id="getCarList" resultClass="car">
  SELECT ID, NAME, ... 중략 ...

5. iBatis에서 위에서 정의한 sql query를 사용할 수 있도록 sqlMap 정보를 설정하고, DB 연결 정보를 설정하는 xml 파일을 만든다.

   <sqlMapConfig>
     <properties resource="ibatis/properties/sql-map-config.properties"/>

     ... 중략 ...

     <transactionManager type="JDBC" commitRequired="false">
        <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}"/>

     ... 중략 ...

     <sqlMap resource="ibatis/dao/sqlmapdao/sql/car.xml"/>

     ... 중략 ...

   </sqlMapConfig>

6. DB 연결 정보를 설정할 properties 파일을 만든다.

   driver=com.mysql.jdbc.Driver
   url=jdbc:mysql://localhost:3306/jsptest
   username=lucky
   password=lucky

7. SqlMapClient 객체를 singleton pattern 을 이용하여 하나의 instance만 생성하여 사용할 수 있도록 한다.

   private static SqlMapClient sqlMap;
  
   static {
      try {
         String resource = "ibatis/dao/sqlmapdao/sql/sql-map-config.xml";
         Reader reader = Resources.getResourceAsReader(resource);
         sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
      } catch(Exception e) {
         e.printStackTrace();
         throw new RuntimeException("Error while initializing SqlMapClient : "  + e);
      }
   }
  
   public static SqlMapClient getInstance() {
      return sqlMap;
   }

8. 3번에서 작성한 테스트 프로그램을 eclipse에서 실행해본다.

   [1] Company = Hyundai, Name = Avante, year = 2001, price = 8,000,000
   [2] Company = Hyundai, Name = Lavita, year = 2002, price = 6,000,000
   [3] Company = Hyundai, Name = Sonata, year = 2006, price = 16,000,000

* 참고사항

  이전에 실행해봤던 예제를 보면, xml/properties 파일들이 default package의 class 파일과 같은 위치에 있었어야 했으나
  이번 예제처럼 package 를 설정하여 class 파일과 구분하여 놓으면
  프로그램 실행할 때 ClassLoader를 기준으로 찾기 때문에

  resource="ibatis/properties/sql-map-config.properties"
  resource="ibatis/dao/sqlmapdao/sql/car.xml"

  위와 같이 가장 상위 package 부터 경로를 작성하면 된다.















'그거 > Tech' 카테고리의 다른 글

Solaris 에서 ISO 이미지 mount 하는 법  (0) 2008.09.09
Velocity 따라하기 - 1번 예제 -  (0) 2008.01.31
iBatis 예제 따라하기~!  (0) 2008.01.22
[JDK 1.4 VS JDK 1.5] Boolean.TRUE 와 true  (0) 2007.11.05
[Network] OSI 7 Layer  (0) 2007.10.31
:
Posted by 뽀기