mybatisplus查询练习代码
package com.yase;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yase.entity.Student;
import com.yase.entity.Teacher;
import com.yase.mapper.StudentMapper;
import com.yase.mapper.TeacherMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class TestSelect {
@Resource
private TeacherMapper mapper;
@Resource
private StudentMapper studentMapper;
@Test
public void selectById(){
Teacher teacher = mapper.selectById(105);
System.out.println(teacher);
}
@Test
public void selectByIds(){
List<Teacher> list = mapper.selectBatchIds(Arrays.asList(105, 106, 107));
list.forEach(System.out::println);
}
@Test
public void selectByMap(){
Map<String,Object> map = new HashMap<>();
map.put("teacher_name","李四");
map.put("gender","女");
List<Teacher> list = mapper.selectByMap(map);
list.forEach(System.out::println);
}
@Test
public void exists(){
QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
wrapper.eq("teacher","张三");
boolean exists = mapper.exists(wrapper);
System.out.println(exists);
}
@Test
public void count(){
QueryWrapper<Teacher> wrapper =new QueryWrapper<>();
wrapper.like("teacher_name","张");
Long aLong = mapper.selectCount(wrapper);
System.out.println(aLong);
}
@Test
public void selectMaps(){
QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
wrapper.like("teacher","张");
List<Map<String, Object>> list = mapper.selectMaps(wrapper);
list.forEach(map->map.forEach((key,value) -> System.out.println(key+" "+value)));
}
@Test
public void selectObjs(){
QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
wrapper.like("teacher_name","张");
List<Object> objects = mapper.selectObjs(wrapper);
objects.forEach(System.out::println);
}
@Test
public void selectList() {
List<Teacher> list = mapper.selectList(null);
list.forEach(System.out::println);
}
@Test
public void selectList1() {
QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
wrapper.like("teacher_name", "张")
.eq("gender", "女");
List<Teacher> list = mapper.selectList(wrapper);
list.forEach(System.out::println);
}
@Test
public void selectList2() {
QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
wrapper.like("teacher_name", "张")
.or()
.eq("gender", "女");
List<Teacher> list = mapper.selectList(wrapper);
list.forEach(System.out::println);
}
@Test
public void selectList3() {
QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
wrapper.like("teacher_name", "张")
.eq("gender", "女")
.or()
.gt("age", 23);
List<Teacher> list = mapper.selectList(wrapper);
list.forEach(System.out::println);
}
@Test
public void selectList4() {
QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
wrapper.likeRight("teacher_name", "张")
.and(i -> i.eq("gender", "女").or()
.gt("age", 23)
);
List<Teacher> list = mapper.selectList(wrapper);
list.forEach(System.out::println);
}
@Test
public void selectListOrderBy() {
QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
wrapper.orderByDesc("age")
.orderByDesc("id");
List<Teacher> list = mapper.selectList(wrapper);
list.forEach(System.out::println);
}
@Test
public void selectListSub() {
QueryWrapper<Student> wrapper = new QueryWrapper<>();
wrapper.inSql("tid", " select id from teacher where teacher_name = '张老师'");
List<Student> students = studentMapper.selectList(wrapper);
students.forEach(System.out::println);
}
@Test
public void selectPage() {
Page<Student> page = new Page<>(1, 5);
QueryWrapper<Student> wrapper = new QueryWrapper<>();
wrapper.likeRight("student_name", "李");
List<Student> records = page.getRecords();
records.forEach(System.out::println);
studentMapper.selectPage(page, wrapper);
System.out.println("总记录数:" + page.getTotal() +
"总页数" + page.getPages() +
"当前页数" + page.getCurrent() +
"每页显示" + page.getSize() + "条数据" +
"是否有上一页" + page.hasPrevious() +
"是否有下一页" + page.hasNext()
);
}
@Test
public void selectMapsPage(){
Page<Map<String,Object>> page = new Page<>(1, 5);
QueryWrapper<Student> wrapper = new QueryWrapper<>();
wrapper.likeRight("student_name", "李");
studentMapper.selectMapsPage(page, wrapper);
List<Map<String,Object>> records = page.getRecords();
records.forEach(map->map.forEach((key,value)-> System.out.println(key+" "+value)));
System.out.println("总记录数:" + page.getTotal() +
"总页数" + page.getPages() +
"当前页数" + page.getCurrent() +
"每页显示" + page.getSize() + "条数据" +
"是否有上一页" + page.hasPrevious() +
"是否有下一页" + page.hasNext()
);
}
@Test
public void selectCustomer(){
List<Student> list = studentMapper.selectCustomer();
list.forEach(System.out::println);
}
}