Why does the list query statement selectPage assembled by MyBatis-Plus contain a WHERE clause?

Generally, the list query statement assembled by MyBatis-Plus is similar to the following:

SELECT id,name,gender,age,address,telephone,is_show FROM staff

But during this actual measurement, this shocking statement was output on the console:

2021-01-07 10:57:34.822 DEBUG 36624 --- [nio-7000-exec-1] c.e.b.***.dao.xxxDao.selectPage  : ==>  Preparing: 
SELECT id,name,gender,age,address,telephone,is_show FROM staff WHERE is_show=1

In addition, sentences such as UPDATE staff SET address=? WHERE id=? or UPDATE staff SET is_show=? WHERE id=?, which were originally good, were inadvertently transformed into the following style:

UPDATE staff SET address=? WHERE id=? AND is_show=1
UPDATE staff    WHERE id=? AND is_show=1 -- (这没有SET子句的UPDATE是什么鬼?——编者问)

After repeated experiments, it was discovered that the crux of the problem was the configuration of the tombstone field in application.yml. As shown below:

mybatis-plus:
  mapper-locations: classpath:/mapper/**/*.xml
  global-config:
    db-config:
      id-type: auto
      logic-delete-field: isShow  # 全局逻辑删除的实体字段名
      logic-delete-value: 0 # 逻辑已删除值(默认为 1)
      logic-not-delete-value: 1 # 逻辑未删除值(默认为 0)

------------------------------------------------ ---Unexpected dividing line---------------------------------------- -----------
If you need to set a tombstone field and don’t want to have the weird SQL statement above, you can refer to what I just wrote earlier "Possible serious consequences when Spring Cloud integrates MyBatis-Plus logical deletion configuration errors""Option 2".

In short, if the tombstone field is set improperly, not only may data be deleted by mistake, but you may also see unclear exceptions on the page and backend, such as:

1、前端控制台异常:
POST http://localhost:88/api/company/***/update 500 (Internal Server Error)
Uncaught (in promise) Error: Request failed with status code 500
    at createError (createError.js?16d0:16)
    at settle (settle.js?db52:18)
    at XMLHttpRequest.handleLoad (xhr.js?ec6c:77)
    
2、后端控制台异常:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; 
	nested exception is org.springframework.jdbc.BadSqlGrammarException: 
	### Error updating database.  Cause: java.sql.SQLSyntaxErrorException: 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 'WHERE id=7  AND is_show=1' at line 1
	### The error may exist in com/xxx/xxxx/company/dao/***Dao.java (best guess)
	### The error may involve com.xxx.xxxx.company.dao.***Dao.updateById-Inline
	### The error occurred while setting parameters
	### SQL: UPDATE staff    WHERE id=? AND is_show=1
	### Cause: java.sql.SQLSyntaxErrorException: 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 'WHERE id=7  AND is_show=1' at line 1;bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: 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 'WHERE id=7  AND is_show=1' at line 1] with root cause

Since the cause of the above problem is relatively hidden and has a huge impact, you need to think twice when using tombstone fields in MyBatis-Plus!

Guess you like

Origin blog.csdn.net/shinyolive/article/details/112305241