mybatisplus查询练习代码

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<Teacher> list = mapper.selectList(null);
        list.forEach(System.out::println);
    }

    //and查询方式
    @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);
    }

    //or查询方式
    @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);
    }

    //查询性别是女,并且年龄大于23的女性
    @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);
    }

    //查询性张,并且(性别是女年龄大于23的女性)用and包括
    @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);
    }

    //按照年龄排序,相同用id降序排序
    @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() {
        //需要在构造方法里传入2个参数,一个页码,一个每页数量
        Page<Student> page = new Page<>(1, 5);
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.likeRight("student_name", "李");
        //page里真正的数据
        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() +    //bollean
                "是否有下一页" + page.hasNext()         //bollean
        );

    }
    @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);

        //page里真正的数据
        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() +    //bollean
                "是否有下一页" + page.hasNext()         //bollean
        );
    }
    //自定义查询
    @Test
    public void selectCustomer(){
        List<Student> list = studentMapper.selectCustomer();
        list.forEach(System.out::println);
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值