티스토리 뷰

마이바티스를 이용하여 개발을 하다보면 쿼리가 보이지 않거나, 파라미터가 안나오거나 개행문자(\n) 등이 무시되면서 한줄로 쭈~~~~욱 나와서 불편한 경우가 많습니다.

 

이러한 경우에는 개발하면서 불편한게 한두가지가 아닌데, 쿼리를 이쁘게 정렬해서 보기좋게 하는 방법을 보겠습니다.

 

1. 메이븐에 라이브러리 추가

pom.xml을 열어서 다음과 같은 라이브러리를 추가합니다. 

<dependency>

<groupId>org.lazyluke</groupId>

<artifactId>log4jdbc-remix</artifactId>

<version>0.2.7</version>

</dependency>

 

2. log4j 설정 변경

log4j.xml을 열어서 다음과 같이 수정합니다.

3. jdbc 설정을 다음과 같이 바꿉니다.

저는 context-datasource.xml에 DB 연결설정이 되어있습니다. (http://addio3305.tistory.com/62 참조) 

 

만약 저랑 다르신분들은................ DB 연결설정이 되어있는 파일을 수정하시면 됩니다.

 

context-datasource.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
	<bean id="dataSourceSpied" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/> 
		<property name="username" value="아이디"/> <property name="password" value="비밀번호"/> 
	</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> 
</beans>

 

내가한 실제 소스

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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- Root Context: defines shared resources visible to all other web components -->
		
	<bean id="dataSourceSpied" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
		<property name="driverClassName" value="org.mariadb.jdbc.Driver"/> 
		<property name="url" value="jdbc:mariadb://localhost:3306/xe"/> 
		<property name="username" value="아이디"/> 
		<property name="password" value="패스워드"/> 
	</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>
        <property name="configLocation" value="classpath:/mybatis/mybatis-config.xml"></property>
        <property name="mapperLocations" value="classpath*:/mybatis/sql/*.xml"></property>
    </bean>
 
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
    </bean>


		
		
</beans>

 

기존에는 jdbc를 이용한 DB 연결을 dataSource라는 이름으로 바로 사용했었습니다. log4j-remix는 이러한 쿼리를 중간에 가로채서 

이쁘게 정렬시켜주는 역할을 합니다.

 

위에서 보신것처럼 기존에 DB와 연결하는것은 dataSourceSpied로 변경하고, 새로 dataSource를 만들었습니다.

그리고 로그타입과 쿼리를 어떻게 출력할지를 설정해주었습니다. 

 

4. 확인을 해봅시다.

이제 설정은 모두 완료가 되었으니 정확히 동작하는지 보겠습니다. 

 

먼저, 기존에 log4j 와 log4j-remix가 설정되지 않았을 경우입니다.

[2020-01-07 18:53:28] INFO : com.test.myapp.HomeController - Welcome home! The client locale is ko_KR.
[2020-01-07 18:53:28] DEBUG: com.test.myapp.HomeController - dubug test
[2020-01-07 18:53:28] DEBUG: com.test.myapp.HomeController - dubug test
[2020-01-07 18:53:28] ERROR: com.test.myapp.HomeController - Error Test
[2020-01-07 18:53:28] ERROR: com.test.myapp.HomeController - Error Test
2020-01-07 18:53:28,520  INFO SQL : SELECT document_srl 
            FROM `xe_documents` 
            limit 1
[2020-01-07 18:53:28] INFO : jdbc.resultsettable - |-------------|
[2020-01-07 18:53:28] INFO : jdbc.resultsettable - |document_srl |
[2020-01-07 18:53:28] INFO : jdbc.resultsettable - |-------------|
[2020-01-07 18:53:28] INFO : jdbc.resultsettable - |90           |
[2020-01-07 18:53:28] INFO : jdbc.resultsettable - |-------------|

 

이렇게 쿼리가 이쁘게 출력된다^^


참고: https://addio3305.tistory.com/66

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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 31
글 보관함