![](/img/trans.png)
[英]How to print the sql query executing in mybatis mapper xml to the console
[英]How to print native query into console?
我正在為數據庫管理創建網絡應用程序。 功能之一是“解釋器 SQL”。
這是我用於執行查詢的代碼:
示例 1
1 public String executeSQL(String[] split){
2 SessionFactory hibernateFactory = someService.getHibernateFactory();
3 Session session = hibernateFactory.openSession();
4 String message = null;
5 for (int i = 0; i < split.length; i++) {
6 try{
7 String query = split[i];
8 session.doWork(connection -> connection.prepareStatement(query).execute());
9
10 message = "Success!";
11 }
12 catch(Exception e){
13 message = ((SQLGrammarException) e).getSQLException().getMessage();
14 }
15
16 }
17 session.close();
18 return message;
19 }
有人能告訴我如何調用“getResultList()” (第 8 行)嗎?
我知道如何在該示例中執行此操作(使用 createNativeQuery):
示例 2
1 public String executeSQL(String[] split){
2 SessionFactory hibernateFactory = someService.getHibernateFactory();
3 Session session = hibernateFactory.openSession();
4 String message = null;
5 for (int i = 0; i < split.length; i++) {
6 try{
7 String query = split[i];
8 EntityManager entityManager = hibernateFactory.createEntityManager();
9 List<Object[]> resultList = entityManager.createNativeQuery(query).getResultList();
10 resultList.stream().map(Arrays::toString).forEach(System.out::println);
11 message = "Success!";
12 }
13 catch(Exception e){
14 // i can't catch SQLException here :( i mean i don't know how...
15 }
16
17 }
18 session.close();
19 return message;
20 }
但是正如您所看到的(第 14 行),我不知道如何在此處捕獲 SQLException,因為我得到的不是 SQLException: 我無法訪問該字段以獲取消息(它是私有的):
我需要該消息將其打印到我的 html 中。 所以我正在嘗試另一種方法來執行查詢 prepareStatement()。
任何人都知道如何將該查詢打印到控制台(示例 1) ?
要將 SQL 查詢打印到控制台,請將以下內容添加到application.properties文件:
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.