'iBATIS3.0'에 해당되는 글 1건

  1. 2008/02/18 iBATIS 3.0 Whiteboard (13)

2008/02/18 00:51 Developer

iBATIS 3.0 Whiteboard

컴파일타임에 알수 있는 것과 동적인 순간에 알수 있는것은 차이가 크다.

우리가 TDD를 하는 이유중의 하나도 가능한 빠른 시간에 코드를 런타임까지 만들어서 버그를 줄이는 것일 것이다. 런타임시에 버그를 줄일 수 있는 것은 코드품질과 밀접한 관계가 있다.

TDD를 통해서 빠르게 런타임 환경을 가져가는 것도 중요하지만, 자바와 같은 경우에는 새로 만든 코드들이 컴파일 타임에 모든 에러를 잡을 수 있으면 가장 좋을 것이라는 생각이다.

iBATIS는 국내에서 많이 사용되는 프레임워크 중의 하나이다. JDBC로 직접코딩하던 시절에 iBATIS와의 처음 만남은 아름다운 추억중의 하나였다. 여러가지 제약사항과 단점이 있긴 하지만, JDBC코딩보다는 중복이나 버그의 가능성을 줄여주는 것은 사실이다.

우리는 일반적으로 다음과 같이 구현을 한다. 써보신 분들은 너무나 자주 보던 syntax이고, 예제이다.
Employee employee = (Employee)sqlMapper.queryForList("getEmployee", 5);
//...and...
List employees = sqlMapper.queryForList("listAllEmployees");
이 코드를 보면서 불편함을 느낀점은 없는가? 문자열 인자로 인해서  refactoring을 하면, 이름이 rename이 되지 않아서 의미에 맞지 않은 이름으로 유지를 하거나 수작업을 통해서 변경을 했다. 특히 JDK5.0부터는 generic을 지원을 하는데, iBATIS는 지원을 하지 않아서 수많은 warning을 제거하기위해서 annotation을 사용을 했다.

3.0에서는 다음과 같이 표현이 가능하다.
Employee emp = empMapper.getEmployee(5);
//...and...
List<Employee> employees = empMapper.listAllEmployees();
casting과 문자열로 된 인자가 없어졌다. 런타임시 실행할 쿼리의 id를 문자열로 적어서 오타로 인한 오류들은 없어질 것이다. "xxx no statement xxxx"와 같은 예외는 iBATIS를 사용하는 사람이라면 지겹도록 봤을 것이다. 그리고, generic을 통해서 리턴되는 value의 타입의 명확성을 가져왔다.

인터페이스 자체가 모든 의도를 표현을 한것이다.
 
보통의 코드개발을 하다가 보면, iBATIS이 유연성만큼 필요한 경우도 드물다. 너무 유연성만 강조하면 코드개발도 힘들고, 유지보수시에도 귀찮아질수도 있다. 그 경우는 spring JDBC가 오히려 간편하고 나아보였는데, 3.0에서 annotation을 통한 쿼리설정은 그런 불편함을 느끼지 않아도 된다.
  @Select("SELECT #id(EMP_ID:NUMERIC), #firstName(FIRST_NAME:VARCHAR), #lastName(LAST_NAME:VARCHAR) " +
      "FROM EMPLOYEE")
  List<Employee> selectAllEmployees();


ConstructorResults와 PropertyResults를 지원한다.
그토록 원하던 ConstructorResults였지만, 다음과 같은 내용이 적혀있다...-.-
  • Result and Parameter mappings to fields, constructor parameters and JavaBeans properties. - Yet again, C# will rule with regard to constructor parameters, because they can be named. Java will be limited to ordinal constructor parameter mappings.

     @ResultClass (Company.class)
      @ConstructorResults({
          @Result(property="id", column="C.COMP_ID"),
          @Result(property="name", column="C.NAME")
          })
      @PropertyResults({
          @Result(property="departments.id", column="D.DEPT_ID"),
          @Result(property="departments.name", column="D.NAME"),
          @Result(property="departments.employee.id", column="E.EMP_ID"),
          @Result(property="departments.employee.firstName", column="E.FIRST_NAME"),
          @Result(property="departments.employee.lastName", column="E.LAST_NAME")
          })
      @Collections ({
          @Collection(type=Department.class, property="departments", groupBy="id"),
          @Collection(type=Employee.class, property="departments.employees", groupBy="departments.id")
          })
      @Select("SELECT #id, #name, " +
              "#departments.id, #departments.name, " +
              "#departments.employees.id, #departments.employees.firstName, " +
              "#departments.employees.lastName " +
              "FROM COMPANY C, DEPARTMENT D, EMPLOYEE E " +
              "WHERE D.DEPT_ID = E.DEPT_ID " +
              "AND C.COMP_ID = D.COMP_ID")
      List<Company> selectAllCompaniesWithJoin();

    iBATIS를 이용해서 자바에서 immutable domain 클래스로 result를  TypeHandler, TypeHandlerCallback를 이용해서 할수 있는 방법은 있을거 같다.

    더 자세한 내용은 아래 링크를 확인하면 된다.
    http://opensource.atlassian.com/confluence/oss/display/IBATIS/iBATIS+3.0+Whiteboard
    http://opensource.atlassian.com/confluence/oss/display/IBATIS/iBATIS+3.0+Whiteboard+-+Korean

    iBATIS3.0 에서 신경을 쓴 부분은 다음과 같다. 코드를 만들면서 생각해볼만한 주제는 굵은 글씨로 표시를 해봤다,

    Themes for iBATIS 3.0

    • Test driven development
    • Code cleanliness over performance
    • Simple design over flexible design
    • One JAR file
    • No required 3rd party dependencies
    • Better plug-in support
    • Third party plug-ins are hosted elsewhere (sub-project on SF or CodePlex) http://sourceforge.net/projects/ibatiscontrib/
  • Posted by ologist
    이전버튼 1 이전버튼

    블로그 이미지
    ologist

    공지사항

    Yesterday171
    Today52
    Total34,795

    달력

     « |  » 2012.02
          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      

    최근에 받은 트랙백

    글 보관함