Spring Boot + MyBatisでSQLログを出力する方法
Spring Boot + MyBatisでSQLログを出力するには、application.propertiesにマッパークラスを指定していきます。
jp.co.confrage.mapper.CustomerMapperインタフェースがあるとします。application.propertiesに以下を追記します。
logging.level.org.springframework=WARN logging.level.jp.co.confrage.mapper.CustomerMapper=DEBUG
メインのクラスからSELECT分を発行してみます。
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クラス(エンティティ)です。
package jp.co.confrage.entity; import lombok.Data; @Data public class EmployeeTbl { private long empno; private String empname; }
CustomerMapperインタフェースです。
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); }
インタフェースにはインサートやアップデートなどいろいろ記述が可能ですが今回はSQLのログを確認したいだけなので、SELECT文だけです。
実行するとコンソールにSQLのログが出力されます。
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: 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
JPAでSQLログを出力する場合は「Spring Data JPAでSQLのログを出力する」を参照ください。

KHI入社して退社。今はCONFRAGEで正社員です。関西で140-170/80~120万から受け付けております^^
得意技はJS(ES6),Java,AWSの大体のリソースです
コメントはやさしくお願いいたします^^
座右の銘は、「狭き門より入れ」「願わくは、我に七難八苦を与えたまえ」です^^
コメント