How to output SQL logs with Spring Boot + MyBatis

How to output SQL logs with Spring Boot + MyBatis

To output SQL logs in Spring Boot + MyBatis, we will specify the mapper class in application.properties.

Assuming there is a jp.co.confrage.mapper.CustomerMapper interface, add the following to application.properties.

logging.level.org.springframework=WARN
logging.level.jp.co.confrage.mapper.CustomerMapper=DEBUG

Try issuing a SELECT minute from the main class.

package jp.co.confrage;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import jp.co.confrage.entity.EmployeeTbl;
import jp.co.confrage.mapper.CustomerMapper;
import lombok.AllArgsConstructor;

@SpringBootApplication
@AllArgsConstructor
public class MybatisApplication implements CommandLineRunner{
  private final CustomerMapper customerMapper;

  public static void main(String[] args) {
    SpringApplication.run(MybatisApplication.class, args);
  }
  @Override
  public void run(String... args) throws Exception {
    EmployeeTbl emp = customerMapper.select(1L);
    System.out.println(emp.getEmpname());
  }
}

EmployeeTbl class (entity).

package jp.co.confrage.entity;
import lombok.Data;
@Data
public class EmployeeTbl {
  private long empno;
  private String empname;
}

CustomerMapper interface.

package jp.co.confrage.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import jp.co.confrage.entity.EmployeeTbl;
@Mapper
public interface CustomerMapper {
  @Select("SELECT empno, empname FROM employee_tbl WHERE empno = #{empno}")
  EmployeeTbl select(@Param("empno")long empno);
}

The interface can contain inserts, updates, etc., but in this case we just want to check the SQL log, so we will only use the SELECT statement.

When executed, the SQL log is output to the console.

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.5.RELEASE)

2019-05-21 19:56:50.580 INFO 9484 --- [ main] jp.co.confrage.MybatisApplication : Starting MybatisApplication on DESKTOP-DNSQQ62 with PID 9484 (C:\Users\takahashi\Documents\workspace-sts\mybatis\bin\main started by takahashi in C:\Users\takahashi\Documents\workspace-sts\mybatis)
2019-05-21 19:56:50.583 INFO 9484 --- [ main] jp.co.confrage.MybatisApplication : No active profile set, falling back to default profiles: default
2019-05-21 19:56:51.375 INFO 9484 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-05-21 19:56:51.375 INFO 9484 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.19]
2019-05-21 19:56:51.461 INFO 9484 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-05-21 19:56:51.882 INFO 9484 --- [ main] jp.co.confrage.MybatisApplication : Started MybatisApplication in 1.557 seconds (JVM running for 2.135)
2019-05-21 19:56:51.894 INFO 9484 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-05-21 19:56:51.979 INFO 9484 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2019-05-21 19:56:52.039 DEBUG 9484 --- [ main] j.c.c.mapper.CustomerMapper.select : ==> Preparing: SELECT empno, empname FROM employee_tbl WHERE empno = ?
2019-05-21 19:56:52.057 DEBUG 9484 --- [ main] j.c.c.mapper.CustomerMapper.select : ==> Parameters: 1(Long)
2019-05-21 19:56:52.076 DEBUG 9484 --- [ main] j.c.c.mapper.CustomerMapper.select : <== Total: 1

コメント

Discover more from 株式会社CONFRAGE ITソリューション事業部

Subscribe now to keep reading and get access to the full archive.

Continue reading

Copied title and URL