QueryWrapper类和 UpdateWrapper类,可以实现多条件复杂查询
package com.zhmsky.mybatisplus_02;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhmsky.mybatisplus_02.pojo.User;
import com.zhmsky.mybatisplus_02.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class WrapperTest {
@Autowired
private UserService userService;
@Test
void contextLoads() {
}
@Test
void selectUser1(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.isNotNull("email").isNotNull("name").ge("age",20);
List<User> userList = userService.list(wrapper);
for (User user : userList) {
System.out.println(user.toString());
}
}
@Test
void selectUser2(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.eq("name","嘻嘻嘻").ge("age",20);
User user = userService.getOne(wrapper);
System.out.println(user.toString());
}
@Test
void selectUser3(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.between("age",18,23);
List<User> userList = userService.list(wrapper);
userList.forEach(System.out::println);
}
@Test
void selectUser4(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.notLike("name","a").likeRight("email","test");
List<User> userList = userService.list(wrapper);
for (User user : userList) {
System.out.println(user.toString());
}
}
@Test
void selectUser5(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.inSql("id","select id from user where id<3");
List<User> userList = userService.list(wrapper);
userList.forEach(System.out::println);
}
@Test
void selectUser6(){
QueryWrapper<User> wrapper = new QueryWrapper<>();
wrapper.orderByDesc("age");
List<User> userList = userService.list(wrapper);
userList.forEach(System.out::println);
}
}
|