當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot和Hibernate:打印查询和变量
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot和Hibernate:打印查询和变量
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
辦公室已經很晚了,您陷入了這個帶有JoinColumns和層疊的奇怪的Jpa代碼中,而您找不到錯誤所在。 您希望有一種方法可以查看打印的查詢以及值。
稍微調整一下Spring Boot應用程序就可以實現。
借助lombock,這是我們的jpa模型。
package com.gkatzioura.hibernatelog.dao; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import lombok.Data; @Data @Entity @Table (name = "application_user" ) public class ApplicationUser { @Id private Long id; private String username; private String password; }是倉庫
package com.gkatzioura.hibernatelog.dao; import org.springframework.data.repository.CrudRepository; public interface ApplicationUserRepository extends CrudRepository { }找不到異常
package com.gkatzioura.hibernatelog.controller; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus (value = HttpStatus.NOT_FOUND) @ResponseStatus (value = HttpStatus.NOT_FOUND) class ApplicationUserNotFoundException extends RuntimeException { public ApplicationUserNotFoundException() { } public ApplicationUserNotFoundException(String message) { super (message); } public ApplicationUserNotFoundException(String message, Throwable cause) { super (message, cause); } public ApplicationUserNotFoundException(Throwable cause) { super (cause); } public ApplicationUserNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super (message, cause, enableSuppression, writableStackTrace); } }還有一個控制器
package com.gkatzioura.hibernatelog.controller; import java.util.Optional; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import com.gkatzioura.hibernatelog.dao.ApplicationUser; import com.gkatzioura.hibernatelog.dao.ApplicationUserRepository; @RestController public class ApplicationUserController { private final ApplicationUserRepository applicationUserRepository; public ApplicationUserController(ApplicationUserRepository applicationUserRepository) { this .applicationUserRepository = applicationUserRepository; } @GetMapping ( "/user/{id}" ) @ResponseBody public ApplicationUser getApplicationUser( @PathVariable Long id) { Optional applicationUser = applicationUserRepository.findById(id); if (applicationUser.isPresent()) { return applicationUser.get(); } else { throw new ApplicationUserNotFoundException(); } } }通過將以下內容添加到application.yaml中,我們確保通過休眠創建表,查詢記錄,所記錄的sql查詢的格式以及顯示的實際參數值。
spring: jpa: hibernate: ddl-auto: create properties: hibernate: show_sql: true use_sql_comments: true format_sql: true logging: level: org: hibernate: type: trace只是
curl http: //localhost :8080 /user/1然后您得到了日志。
翻譯自: https://www.javacodegeeks.com/2019/08/spring-boot-hibernate-print-queries-variables.html
總結
以上是生活随笔為你收集整理的Spring Boot和Hibernate:打印查询和变量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1升等于多少克 等于1000克
- 下一篇: restful json_Dropwiz