디비 설정 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.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydatabase"/>
<property name="username" value="디비아이디"/>
<property name="password" value="디비패스"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="/WEB-INF/spring/mybatis.config.xml"/>
<property name="mapperLocations">
<array>
<value>classpath:/sqlmapper/one/*.xml</value>
<value>classpath:/sqlmapper/two/*.xml</value>
</array>
</property>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
<!--?xml version="1.0" encoding="UTF-8"?--> <configuration> <plugins> <plugin interceptor="myprj.com.logInterceptor"> </plugin></plugins> </configuration>
package myprj.com; import java.util.Properties; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; @Intercepts( { @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) ,@Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }) }) public class logInterceptor implements Interceptor { private Logger log = LogManager.getLogger(getClass()); @Override public Object intercept(Invocation invocation) throws Throwable { Object[] args = invocation.getArgs(); MappedStatement ms = (MappedStatement) args[0]; Object param = (Object) args[1]; BoundSql boundSql = ms.getBoundSql(param); String query_name = invocation.getMethod().getName(); String query_id = ms.getId(); System.out.println("query_id:"+query_id); if ( query_id.contains("xml맵퍼의 로그출력을 원하는 쿼리id값")) { System.out.println("===================================="); System.out.println("1:" + invocation.getMethod().getName()); System.out.println("===================================="); System.out.println("2:" + ms.getId()); System.out.println("===================================="); System.out.println("3:" + boundSql.getSql()); System.out.println("===================================="); System.out.println("4:" + param); System.out.println("===================================="); System.out.println("5:" + ms.getId()); } return invocation.proceed(); } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { } }
위의 인터셉터로 인해 insert,update,delete,select 쿼리 모두 로그를 출력할 수 있었습니다.
log4j 를 사용하면 모든 쿼리를 출력하거나 출력안하거나 해야해서 쿼리호출이 많을경우 로그출력으로 인해
속도가 늦어지는 문제가 있었습니다.
그래서 인터셉터 방식으로 원하는 쿼리만 로그출력을 해봤습니다.