MyBatisPlus-范围查询、模糊查询及排序查询

原文链接:https://blog.csdn.net/m0_61961937/article/details/125967684

一、范围查询

二、模糊查询

三、排序查询

一、范围查询

需求:对年龄进行范围查询,使用lt()、le()、gt()、between()进行范围查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@SpringBootTest
class Mybatisplus02DqlApplicationTests {

@Autowired
private UserDao userDao;

@Test
void testGetAll(){
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
lqw.between(User::getAge, 10, 30);
//SELECT id,name,password,age,tel FROM user WHERE (age BETWEEN ? AND ?)
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);
}
}
1
2
3
4
5
6
7
8
9
gt():大于(>)

ge():大于等于(>=)

lt():小于(<)

lte():小于等于(<=)

between():between ? and ?

二、模糊查询

需求:查询表中name属性的值以3开头的用户信息,使用like进行模糊查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@SpringBootTest
class Mybatisplus02DqlApplicationTests {

@Autowired
private UserDao userDao;

@Test
void testGetAll(){
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
lqw.likeLeft(User::getName, "J");
//SELECT id,name,password,age,tel FROM user WHERE (name LIKE ?)
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);
}
}
1
2
3
4
5
like():前后加百分号,如 %J%

likeLeft():前面加百分号,如 %J

likeRight():后面加百分号,如 J%

三、排序查询

需求:查询所有数据,然后按照id降序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@SpringBootTest
class Mybatisplus02DqlApplicationTests {

@Autowired
private UserDao userDao;

@Test
void testGetAll(){
LambdaQueryWrapper<User> lwq = new LambdaQueryWrapper<>();
/**
* condition :条件,返回boolean,
当condition为true,进行排序,如果为false,则不排序
* isAsc:是否为升序,true为升序,false为降序
* columns:需要操作的列
*/
lwq.orderBy(true,false, User::getId);
userDao.selectList(lwq);
}
}