Mybatis-Plus实现foreach操作和条件查询功能

foreach操作

Mybatis-Plus本身是不具备foreach功能的,就相当于一个帮我们封装好sql的工具类,使用起来很方便,使用当我们需要foreach循环的时候,只需要自己写一个java的foreach操作,然后没遍历一条数据就执行一步操作就行

    //批量逻辑删除学习信息
    public int deletedStudentInfoPart(List<Student> students){
        final int[] i = {1};
        students.forEach(student -> {
            i[0] = studentMapper.deleteById(student);
        });
        return i[0];
    }

按条件查询功能

就是使用Mybatis-Plus自带的QueryWrapper类。根据条件不同执行每一步操作,QueryWrapper支持链式编程,根据查询条件不同查询不同sql就行

    public Page<Student> selectStudentAll(Pages pages, Student student) {
        Page<Student> page = new Page<>(pages.getPage(), pages.getLimit());
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        if (!student.getStuName().equals("")){
            wrapper.like("stu_name",student.getStuName());
        }
        if (!student.getStuSex().equals("")){
            wrapper.eq("stu_sex",student.getStuSex());
        }
        if (student.getStatus()==1){
            wrapper.eq("status",1);
        }
        if (student.getStatus()==0){
            wrapper.eq("status",0);
        }
        if (!student.getStuNative().equals("")){
            wrapper.like("stu_native",student.getStuNative());
        }
        Page<Student> studentPage = studentMapper.selectPage(page, wrapper);
        return studentPage;
    }
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐