AdSense

網頁

2020/10/22

Spring Data JPA Hibernate 印出查詢SQL參數 print query sql parameters log

Spring Data JPA及Hibernate印出查詢SQL的參數的方法如下。。

環境:

  • Spring Boot 2.3.2.RELEASE
  • H2 Database
  • JUnit 5
  • Lombok

application.properties設定以下。

application.properties

logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type=TRACE
logging.level.org.hibernate.type.descriptor.sql=trace

logging.level.org.hibernate.SQL=DEBUG印出sql語句;
logging.level.org.hibernate.type.descriptor.sql=TRACE印出sql參數。


下面為Spring Data JPA測試印出log效果。

Entity類Employee

Employee

package com.abc.demo.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
public class Employee implements Serializable {
    private static final Long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private String email;
    private Integer age;
    private Date createDate;

}

DAO(Data access object)類EmployeeRepository

EmployeeRepository

package com.abc.demo.repository;

import com.abc.demo.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}

執行測試程式EmployeeRepositoryTests

EmployeeRepositoryTests

package com.abc.demo.repository;

import com.abc.demo.entity.Employee;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;

@SpringBootTest
class EmployeeRepositoryTests {

    @Autowired
    private EmployeeRepository employeeRepository;

    @Test
    void printJpaSqlParameters() {
        Employee employee = Employee.builder().name("John")
                .email("john@abc.com")
                .age(29)
                .createDate(new Date())
                .build();

        employee = employeeRepository.save(employee);

        Assertions.assertEquals(employee.getName(), "John");
    }
}

console log印出結果如下。

2020-10-23 10:56:07.607 DEBUG 10804 --- [           main] o.h.SQL                                  : insert into employee (id, age, create_date, email, name) values (null, ?, ?, ?, ?)
2020-10-23 10:56:07.612 TRACE 10804 --- [           main] o.h.t.d.s.BasicBinder                    : binding parameter [1] as [INTEGER] - [29]
2020-10-23 10:56:07.613 TRACE 10804 --- [           main] o.h.t.d.s.BasicBinder                    : binding parameter [2] as [TIMESTAMP] - [Fri Oct 23 10:56:07 CST 2020]
2020-10-23 10:56:07.621 TRACE 10804 --- [           main] o.h.t.d.s.BasicBinder                    : binding parameter [3] as [VARCHAR] - [john@abc.com]
2020-10-23 10:56:07.621 TRACE 10804 --- [           main] o.h.t.d.s.BasicBinder                    : binding parameter [4] as [VARCHAR] - [John]

參考github


沒有留言:

AdSense