1. pom.xml 파일에 아래 dependency 추가
https://mvnrepository.com/artifact/org.bgee.log4jdbc-log4j2/log4jdbc-log4j2-jdbc4.1/1.16
2. resources 폴더 아래 파일 2개 추가
1) log4jdbc.log4j2.properties
log4jdbc.spylogdelegator.name=net.sf.log4jdbc.log.slf4j.Slf4jSpyLogDelegator
2) logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<!-- log4jdbc-log4j2 -->
<logger name="jdbc.sqlonly" level="INFO"/>
<logger name="jdbc.sqltiming" level="INFO"/>
<logger name="jdbc.audit" level="WARN"/>
<logger name="jdbc.resultset" level="INFO"/>
<logger name="jdbc.resultsettable" level="INFO"/>
<logger name="jdbc.connection" level="INFO"/>
</configuration>
3. root-context.xml 파일 수정
driverClassName과 url을 아래 코드로 수정
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"></property>
<property name="url" value="jdbc:log4jdbc:mysql://localhost:3306/springbasic?useUnicode=true&characterEncoding=utf8"></property>
<!-- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>-->
<!-- <property name="url" value="jdbc:mysql://localhost:3306/springbasic?useUnicode=true&characterEncoding=utf8"></property>-->
<property name="username" value=""></property>
<property name="password" value=""></property>
</bean>
그러면 로그에 이런 식으로 실행된 sql문이 찍힌다!
참조
https://fastcampus.co.kr/dev_academy_nks
'3. Back-end > 3-2. Spring MVC - 남궁성' 카테고리의 다른 글
SpringMVC - [ REST API와 Ajax ] (0) | 2023.04.18 |
---|---|
SpringMVC - [ MyBatis 동적 쿼리 ] (0) | 2023.04.04 |
SpringMVC - [ MyBatis ] (0) | 2023.03.26 |
Spring MVC - [ 서비스 계층의 분리 / Transactional ] (0) | 2023.03.17 |
Spring MVC - [ AOP ] (0) | 2023.03.09 |