Problems encountered in MybatisPlus paging operation

1. The emergence of problems

After the employee paging query is written, the test is run and an error is reported:数据库异常,操作失败!
Insert image description here

2. Check the SQL statement according to the prompts

First of all, suspect: the MySQL service is not started, or the server is down, or there is a problem with the spelling of the SQL statement.
I copied the SQL log in the console to Navicat, and the deletion LIMIT 10;was able to produce results, which basically eliminated this problem. (The importance of logs must be turned on!!!)

3. Check dependency versions and configurations

Then I suspected: There is something wrong with the MyBatisPlus configuration. After checking the MyBatisPlus version information and configuration content, I feel that there is little possibility of an error here.

4. Find specific error information

I wanted to check the error results in Idea and found that there was no error message in Idea.
Therefore, it is inferred that this error message comes from the global exception handling class written by myself. So I entered the global exception handling method and tried to print the exception information:

package com.sky.server.config.exception;

import com.sky.server.pojo.RespBean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理
 */
@RestControllerAdvice
public class GlobalException {
    
    
    @ExceptionHandler(SQLException.class)
    public RespBean mySqlException(SQLException e){
    
    
        if (e instanceof SQLIntegrityConstraintViolationException){
    
    
            return RespBean.error("SQL完整性约束违反异常: 该数据有关联数据,操作失败!");
        }
        /** 添加内容,打印异常信息 **/
        System.out.println("数据库异常具体信息为:");
        System.out.println(e.getMessage());
        /** 添加内容,打印异常信息 **/
        return RespBean.error("数据库异常,操作失败!");
    }
}

After running, the printed result is: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 10' at line 37
Analysis: There is a problem near LIMIT 10. Then I found that there was an extra semicolon " "
written after e.id at the end of the SQL statement. Just delete the semicolon.;
Insert image description here

5. Summary

This error is actually not a big problem, but there are still a few points that need attention:

  1. If you are not familiar with the SQL statement, you can actually locate the problem with the SQL statement from the beginning.
  2. If there is an error in the XML configuration field, an error message will be reported on the Idea console.
  3. When the result is an error but there is no error message on the console, it means that the exception has been caught and processed. Printing the exception message is often the most useful.
  4. I did not write " " when doing paging before , so this problem did not occur. I have always thought that it is OK to write or not write " " at the end of the SQL statement written in the XML file of MyBatisPlus . This incident reminds me not to write " " at the end of the SQL statement.

Guess you like

Origin blog.csdn.net/qq_38662733/article/details/127074190