마이바티스 쿼리 로그 확인 lazyluke

Web Development assistant·2022년 3월 20일
0

# spring

목록 보기
20/32

root-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

	<!-- Root Context: defines shared resources visible to all other web components -->
	

	<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
		<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:XE"></property>
		<property name="username" value="book_ex"></property>
		<property name="password" value="book_ex"></property>
	</bean>
	
    ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼핵심▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
	<bean id="dataSourceSpied" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close"> 
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:XE" />
        <property name="username" value="스키마ID"/>
        <property name="password" value="스키마PW" /> 
	</bean> 
	<bean id="dataSource" class="net.sf.log4jdbc.Log4jdbcProxyDataSource"> 
	    <constructor-arg ref="dataSourceSpied" /> 
	    <property name="logFormatter"> 
	        <bean class="net.sf.log4jdbc.tools.Log4JdbcCustomFormatter"> 
	            <property name="loggingType" value="MULTI_LINE" /> 
	            <property name="sqlPrefix" value="SQL : "/> 
	        </bean> 
	    </property> 
	</bean>
▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲핵심▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<context:component-scan base-package="org.zerock.service"></context:component-scan>
	<mybatis-spring:scan base-package="org.zerock.mapper"/>
</beans>

디펜던시 추가

		<dependency>
			<groupId>org.lazyluke</groupId>
			<artifactId>log4jdbc-remix</artifactId>
			<version>0.2.7</version>
		</dependency>

log4j.xml 설정 추가

<!-- SQL Logger -->
	<!--  SQL문과 해당 SQL을 실행시키는데 수행된 시간 정보(milliseconds) -->
	<logger name="jdbc.sqltiming" additivity="false">
	    <level value="info" />
	    <appender-ref ref="console"/> 
	</logger>
	<!--  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> 
	 
	<!--  ResultSet을 포함한 모든 JDBC 호출 정보 -->
	<logger name="jdbc.resultset" additivity="false">
	    <level value="info" />
	    <appender-ref ref="console"/> 
	</logger>
	 
	<!-- SQL 결과 조회된 데이터의 table을 로그 -->
	<logger name="jdbc.resultsettable" additivity="false"> 
	    <level value="info"/>  
	    <appender-ref ref="console"/> 
	</logger> 

결과

INFO : jdbc.sqlonly - SQL : select *
 from tbl_board order by bno desc
 
INFO : jdbc.resultsettable - |----|-----------|-------------|---------|----------------------|----------------------|
INFO : jdbc.resultsettable - |BNO |TITLE      |CONTENT      |WRITER   |REGDATE               |UPDATEDATE            |
INFO : jdbc.resultsettable - |----|-----------|-------------|---------|----------------------|----------------------|
INFO : jdbc.resultsettable - |22  |test111    |test111      |111      |2022-03-20 19:51:08.0 |2022-03-20 19:51:08.0 |
INFO : jdbc.resultsettable - |21  |test       |test         |123      |2022-03-20 16:46:54.0 |2022-03-20 16:46:54.0 |
INFO : jdbc.resultsettable - |20  |제목 테스트     |내용 테스트       |jiseong1 |2022-03-20 16:21:57.0 |2022-03-20 16:21:57.0 |
INFO : jdbc.resultsettable - |19  |제목 테스트     |내용 테스트       |jiseong1 |2022-03-20 16:21:49.0 |2022-03-20 16:21:49.0 |
INFO : jdbc.resultsettable - |18  |title test |content test |jiseong  |2022-03-20 16:15:54.0 |2022-03-20 16:15:54.0 |
INFO : jdbc.resultsettable - |17  |title test |content test |jiseong  |2022-03-20 16:15:53.0 |2022-03-20 16:15:53.0 |
INFO : jdbc.resultsettable - |16  |title test |content test |jiseong  |2022-03-20 16:15:53.0 |2022-03-20 16:15:53.0 |
INFO : jdbc.resultsettable - |15  |title test |content test |jiseong  |2022-03-20 16:15:52.0 |2022-03-20 16:15:52.0 |
INFO : jdbc.resultsettable - |14  |title test |content test |jiseong  |2022-03-20 16:15:52.0 |2022-03-20 16:15:52.0 |
....
...
..
.

log4j에 비해 훨씬 빠르게 설정가능했고 간편하다.
특히 프로퍼티즈 첨부파일을 가지고 있지 않아도 된다는점이 좋았다.

0개의 댓글