SSMP integration case (5) Spring Boot integrates MyBatis-Plus to achieve conditional query

After talking about conditional query, the code of our entire data layer is finished.

You can see that our previous code query statement has a parameter QueryWrapper, which is the query condition.
insert image description here
In fact, we can directly write it like this

QueryWrapper<book> Query = new QueryWrapper<>();
bookDao.selectList(Query);

The QueryWrapper class needs to be manually imported import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
here we have QueryWrapper and then generic book our book entity class

This Query is the condition

Ok, let's look at the data book table
insert image description here
and then let's look at the data that contains cats in the query name

We write the code as follows

QueryWrapper<book> Query = new QueryWrapper<>();
Query.like("name","猫");
bookDao.selectList(Query);

The running results are as follows
insert image description here
Here is to convert all sql into Api functions,
for example equal to eq

QueryWrapper<book> Query = new QueryWrapper<>();
Query.eq("name","小猫猫");
bookDao.selectList(Query);

insert image description here
not equal to ne

QueryWrapper<book> Query = new QueryWrapper<>();
Query.ne("name","小猫猫");
bookDao.selectList(Query);

insert image description here
a lot here

queryWrapper.lt()——小于
queryWrapper.le()——小于等于
queryWrapper.gt()——大于
queryWrapper.ge()——大于等于
queryWrapper.eq()——等于
queryWrapper.ne()——不等于
queryWrapper.betweeen(“age”,10,20)——age在值1020之间
queryWrapper.notBetweeen(“age”,10,20)——age不在值1020之间
queryWrapper.like(“属性”,“值”)——模糊查询匹配值‘%%
queryWrapper.notLike(“属性”,“值”)——模糊查询不匹配值‘%%
queryWrapper.likeLeft(“属性”,“值”)——模糊查询匹配最后一位值‘%值’
queryWrapper.likeRight(“属性”,“值”)——模糊查询匹配第一位值‘值%
queryWrapper.isNull()——值为空或null
queryWrapper.isNotNull()——值不为空或null
queryWrapper.in(“属性”,条件,条件 )——符合多个条件的值
queryWrapper.notIn(“属性”,条件,条件 )——不符合多个条件的值
queryWrapper.or()——或者
queryWrapper.and()——和
queryWrapper.orderByAsc(“属性”)——根据属性升序排序
queryWrapper.orderByDesc(“属性”)——根据属性降序排序
queryWrapper.inSql(“sql语句”)——符合sql语句的值
queryWrapper.notSql(“sql语句”)——不符合SQL语句的值
queryWrapper.esists(“SQL语句”)——查询符合SQL语句的值
queryWrapper.notEsists(“SQL语句”)——查询不符合SQL语句的值

Then he has another way
we write the code as follows

LambdaQueryWrapper<book> Query = new LambdaQueryWrapper<>();
Query.ne(book::getName,"小猫猫");
bookDao.selectList(Query);

The advantage of this thing is that if you use QueryWrapper, there may be a wrong name of the first parameter field, so the whole function will be useless.
But LambdaQueryWrapper is class name:: field name, so it has a syntax editor to verify that it is difficult for you to make a mistake.

But there is still a problem at present. Like the second condition we write later, it is usually not to write the dead value directly, but to pass it to you through a variable.

Then there is likely to be a situation where the parameter is not null

String name = null;
LambdaQueryWrapper<book> Query = new LambdaQueryWrapper<>();
Query.eq(book::getName,name);
bookDao.selectList(Query);

Here we have simulated the running results as follows:
SQL is written out of shape and nothing can be found out.
insert image description here
According to our previous method, it is

String name = null;
LambdaQueryWrapper<book> Query = new LambdaQueryWrapper<>();
if(name != null)Query.eq(book::getName,name);
bookDao.selectList(Query);

The results of the operation are as follows,
insert image description hereand all of them are checked out. Because you judge that if there is no condition, you will not add this condition directly, so naturally there is no condition to check all of them.

In fact, we can change

Query.eq(true,book::getName,name);

This is the use of method rewriting, that is, it has another way of passing three parameters, the first value is boolean type
true, execute false, not execute, then we can change the code to this

String name = null;
LambdaQueryWrapper<book> Query = new LambdaQueryWrapper<>();
 Query.eq(name != null,book::getName,name);
 bookDao.selectList(Query);

The result of the operation is as follows.
insert image description here
We did not add this condition
because we used a judgment for the first value that the name is not equal to null, but the name is null, so the condition returns false,
and the statement condition is not executed.

Guess you like

Origin blog.csdn.net/weixin_45966674/article/details/131366769