How to use parentheses to nest queries in mybatisplus where QueryWrapper
Most people do not understand the knowledge points of this article "mybatisplus where QueryWrapper how to add parentheses nested query", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "mybatisplus where QueryWrapper how to add parentheses nested query" article.
Where QueryWrapper nested query in parentheses
The previous code looks like this:
QueryWrapper wrapper = new QueryWrapper (); wrapper.eq ("phoneNumber", phone); if (StringUtils.isEmpty (scenetype)) {wrapper.isNull ("scenetype"). Or (). Eq ("scenetype", ");} wrapper.eq (" delFlag "," 0 ")
The corresponding SQL statement is as follows:
WHERE (phoneNumber = 156 million dollars) AND scenetype IS NULL OR scenetype = "" AND delFlag = 0)
This is not the sql statement I want, and the data for the query is incorrect. The query for scenetype should be enclosed and meet one of its conditions, that is:
WHERE (phoneNumber = 156 million dollars) 8888 AND (scenetype IS NULL OR scenetype = ") AND delFlag = 0)
The corresponding code should be written like this:
QueryWrapper wrapper = new QueryWrapper (); wrapper.eq ("phoneNumber", phone); if (StringUtils.isEmpty (scenetype)) {wrapper.and (e-> e.isNull ("scenetype") .or (). Eq ("scenetype", "));} wrapper.eq (" delFlag "," 0 "); mybatisplus query statement with parentheses (.or (), .and ())
Java Code:
QueryWrapper wrapper = new QueryWrapper (); wrapper.ne ("id", param.getId ()) wrapper.and (QueryWrapper-> QueryWrapper.eq ("name", name). Or (). Eq ("mark", mark); entityMapper.selectList (wrapper)
Equivalent sql
SELECT id FROM t_entity WHERE (id? AND (name =? OR mark =?))
Each and is equivalent to a parenthesis
The above is about "mybatisplus where QueryWrapper how to add parentheses nested query" this article content, I believe we all have a certain understanding, I hope the editor to share the content to help you, if you want to know more related knowledge, please pay attention to the industry information channel.