@Test
void select() {
//1、查询所有,不加条件去查询
userMapper.selectList(null).forEach(System.out::println); //forEach遍历打印
//2、查询所有,加条件去查询
//2.1、new QueryWrapper
QueryWrapper queryWrapper1 = new QueryWrapper();
//2.2、设置条件
queryWrapper1.eq("age", 20);
/**
* lt #小于
* gt #大于
* ne #不等于
* eq #等于
* le #小于等于
* ge #大于等于
* between #between
* like like 模糊查询 #likeLeft 左模糊 likeRight 右模糊
* isNull
* isNotNull
* in #inSql in sql语句
* notIn
* orderBy #排序 ASC DESC 升序降序
* orderByDesc
*/
userMapper.selectList(queryWrapper1).forEach(System.out::println);
//3、多条件去查询
QueryWrapper queryWrapper2 = new QueryWrapper();
//3.1、设置多条件
Map<String,Object> map = new HashMap<>();
map.put("age",20);
map.put("name","张三");
//3.2、map放进queryWrapper
queryWrapper2.allEq(map);
//byId
User user = userMapper.selectById(1);
System.out.println(user);
//byBatchIds
userMapper.selectBatchIds(Arrays.asList(1,2,3)).forEach(System.out::println); //Arrays.asList(1,2,3)是一个数组,把数组转换成list集合
//通过map条件查询
//map只能是一个条件,不能是多个条件
Map<String,Object> map1 = new HashMap<>();
map1.put("age",20);
map1.put("name","张三");
userMapper.selectByMap(map).forEach(System.out::println);
//4、分组查询
QueryWrapper queryWrapper3 = new QueryWrapper();
queryWrapper3.gt("age",20);
System.out.println(userMapper.selectCount(queryWrapper3));
//将查询结果放入map中
userMapper.selectMaps(queryWrapper1).forEach(System.out::println);
//分页查询
//1、配置类paginationInterceptor
//2、设置分页参数
Page<User> page = new Page<>(1,3); //当前页,每页显示条数2
Page<User> userPage = userMapper.selectPage(page, null);//分页参数,查询条件
System.out.println(userPage.getCurrent()); //当前页
System.out.println(userPage.getSize()); //每页显示条数
userPage.getRecords().forEach(System.out::println); //查询结果
//封装到map中
Page<Map<String,Object>> mapPage = new Page<>(1,3); //分页参数,查询条件
userMapper.selectMapsPage(mapPage, null).getRecords().forEach(System.out::println); //查询结果
//查询所有,只输出id
userMapper.selectObjs(null).forEach(System.out::println); //查询结果
//查询一个
System.out.println(userMapper.selectOne(null)); //查询结果
}
评论