[TIL] 2024-08-19

성장일기·2024년 8월 24일

회고

목록 보기
32/37

중요 학습 내용 [MyBatis, SpringBoot]

Mapper element

  • Mybatis 에서 매핑하는 ResultMap에 대해서 상속관계를 만들 수 있다.

  • use

    <mapper ...>
        <resultMap id="menuResultMap1" type="MenuDTO">
            <id property="menuCode" column="MENU_CODE"/>
            <result property="menuName" column="MENU_NAME"/>
            <result property="menuPrice" column="MENU_PRICE"/>
            <result property="categoryCode" column="CATEGORY_CODE"/>
        </resultMap>
        <resultMap id="menuResultMap2" type="MenuDTO" extends="menuResultMap1">
            <result property="orderableStatus" column="ORDERABLE_STATUS"/>
        </resultMap>
    </mapper>  
  • utility: JOIN을 통해 한 번 객체를 담아놓으면, spring 서버 내에서 객체 그래프 탐색이 가능하다.

객체 그래프 탐색:
객체 속성의 또 다른 속성을 넘나들며 검색

  • category <- menu
  • DB 모델링 관점에서 양방향 관계
  • JAVA 관점에서 단방향 관계

ASSOCIATION RELATION

  • 자식 테이블(객체)을 기준으로 부모 테이블(객체)과 매핑
  • Mapping[Mapper.xml]
    • AssociationMapper.xml
      <resultMap id="menuAndCategoryResultMap" type="classpath.MenuAndCategoryDTO">
          <id property="menuCode" column="MENU_CODE"/>
          <result property="menuName" column="MENU_NAME"/>
          <result property="menuPrice" column="MENU_PRICE"/>
          <result property="orderableStatus" column="ORDERABLE_STATUS"/>
          <association property="category" resultMap="categoryResultMap"/>
      </resultMap>
      <resultMap id="categoryResultMap" type="classpath.CategoryDTO">
          <id property="categoryCode" column="CATEGORY_CODE"/>
          <result property="categoryName" column="CATEGORY_NAME"/>
          <result property="refCategoryCode" column="REF_CATEGORY_CODE"/>
      </resultMap>
      or
      <resultMap id="menuAndCategoryResultMap" type="classpath.MenuAndCategoryDTO">
          <id property="menuCode" column="MENU_CODE"/>
          <result property="menuName" column="MENU_NAME"/>
          <result property="menuPrice" column="MENU_PRICE"/>
          <result property="orderableStatus" column="ORDERABLE_STATUS"/>
          <association property="category" javaType="classpath.CategoryDTO">
              <id property="categoryCode" column="CATEGORY_CODE"/>
              <result property="categoryName" column="CATEGORY_NAME"/>
              <result property="refCategoryCode" column="REF_CATEGORY_CODE"/>
          </association>
      </resultMap>

COLLECTION RELATION

  • 부모 테이블(객체)을 기준으로 자식 테이블(객체)과 매핑

    • CollectionMapper.xml
      <resultMap id="categoryAndMenuResultMap" type="classpath.CategoryAndMenuDTO">
          <id property="categoryCode" column="CATEGORY_CODE"/>
          <result property="categoryName" column="CATEGORY_NAME"/>
          <result property="refCategoryCode" column="REF_CATEGORY_CODE"/>
          <collection property="menuList" resultMap="menuResultMap"/>
      </resultMap>
      <resultMap id="menuResultMap" type="classpath.MenuDTO">
          <id property="menuCode" column="MENU_CODE"/>
          <result property="menuName" column="MENU_NAME"/>
          <result property="menuPrice" column="MENU_PRICE"/>
          <result property="categoryCode" column="CATEGORY_CODE"/>
          <result property="orderableStatus" column="ORDERABLE_STATUS"/>
      </resultMap>
      or
      <resultMap id="categoryAndMenuResultMap" type="classpath.CategoryAndMenuDTO">
          <id property="categoryCode" column="CATEGORY_CODE"/>
          <result property="categoryName" column="CATEGORY_NAME"/>
          <result property="refCategoryCode" column="REF_CATEGORY_CODE"/>
          <collection property="menuList" ofType="MenuDTO">
              <id property="menuCode" column="MENU_CODE"/>
              <result property="menuName" column="MENU_NAME"/>
              <result property="menuPrice" column="MENU_PRICE"/>
              <result property="categoryCode" column="CATEGORY_CODE"/>
              <result property="orderableStatus" column="ORDERABLE_STATUS"/>
          </collection>
      </resultMap>

SpringBoot - Mybatis

연동 및 로깅

  • for test ref: log4j2 docs
  • level
    • 개발 시: debug level
    • 배포 시: info level
  • [build.gradle]
...
implementation 'org.bgee.log4jdbc-log4j2:log4jdbc-log4j2-jdbc4.1:1.16'
  • [application.yml]
spring:
  datasource:
#    driver-class-name: org.mariadb.jdbc.Driver
#    jdbc-url: jdbc:mariadb://localhost:3306/test
    driver-class-name: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
    jdbc-url: jdbc:log4jdbc:mariadb://localhost:3306/test
    username: root
    password: admin
logging:
  level:
    root: info
  • [log4j2.xml]
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
    <!-- Appenders -->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <charset>UTF-8</charset>
            <Pattern>%d %5p [%c] %m%n %r</Pattern>
        </encoder>
    </appender>

    <!-- info부터 적용 table사용시 -->
    <logger name="jdbc.resultsettable" additivity="false">
        <level value="info" />
        <appender-ref ref="console" />
    </logger>

    <!-- sql 로깅 -->
    <!-- SQL문이 preparedStatement일 경우 argument값으로 대체된 SQL문 출력 -->
    <logger name="jdbc.sqlonly" additivity="false">
        <level value="info" />
        <appender-ref ref="console" />
    </logger>

    <!-- resultset제외한 모든 jdbc 호출 정보 출력 -->
    <logger name="jdbc.audit" additivity="false">
        <level value="info" />
        <appender-ref ref="console" />
    </logger>

    <!-- info부터 적용 기본적인 resultset-->
    <!-- resultset을 포함한 모든 jdbc 호출 정보 로그 -->
    <!-- resultsettable 만 사용하기 위해서 로그레벨은 warning시킨다. resultset이 중복됨 -->
    <logger name="jdbc.resultset" additivity="false">
        <level value="info" />
        <appender-ref ref="console" />
    </logger>

    <!-- 수행시간을 찍는다-->
    <logger name="jdbc.sqltiming" additivity="false">
        <level value="info" />
        <appender-ref ref="console" />
    </logger>

    <!-- Root Logger -->
    <root level="off">
        <appender-ref ref="console" />
    </root>
</configuration>
  • [log4jdbc.log4j2.properties]
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
log4jdbc.dump.sql.maxlinelength=0
profile
엔지니어로의 성장일지

0개의 댓글