mybatisplus----条件查询wrapper的基本操作
直接上代码:
package com.hui; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.hui.mapper.UserMapper; import com.hui.pojo.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; import java.util.Map; @SpringBootTest public class WrapperTest { @Autowired UserMapper userMapper; //下面这是条件查询 //wrapper对象其实就是封装了一个条件,然后语句根据这个条件去执行 @Test void testWrapper1() { QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.isNotNull("age") .isNotNull("id") .eq("age", 20); userMapper.selectList(wrapper).forEach(System.out::println); } @Test void testWrapper2() { QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.eq("name", "瓜瓜"); System.out.println(userMapper.selectOne(wrapper)); } @Test void testWrapper3() { QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.eq("name", "本杰明"); Integer integer = userMapper.selectCount(wrapper); System.out.println(integer); } @Test void testWrapper4() { QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.likeRight("email", "t"); List<Map<String, Object>> maps = userMapper.selectMaps(wrapper); maps.forEach(System.out::println); } @Test void testWrapper5() { QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.inSql("id", "select id from user where id=2"); List<Object> objects = userMapper.selectObjs(wrapper); objects.forEach(System.out::println); } @Test void testWrapper6() { QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.orderByDesc("id"); List<User> users = userMapper.selectList(wrapper); users.forEach(System.out::println); } }
迎风少年