QueryWrapper,LambdaQueryWrapper用法简介(MyBatis Plus进阶)

目录

  • 一、项目结构及数据库内容如图
  • 二、数据库内容
  • 三、代码示例

关于如何使用MyBatis Plus自动生成service,mapper,domain参考我的这篇博客使用MyBatis Plus自动生成service,mapper,domain

一、项目结构及数据库内容如图


service,mapper,domain均为MyBatis Plus自动生成。

二、数据库内容

create table mp.tb_user
(
    id        bigint auto_increment comment '主键ID'
        primary key,
    user_name varchar(20) not null comment '用户名',
    password  varchar(20) not null comment '密码',
    name      varchar(30) null comment '姓名',
    age       int         null comment '年龄',
    email     varchar(50) null comment '邮箱',
    dept_id   int         null,
    salary    double      null
)
    charset = utf8;
INSERT INTO mp.tb_user (id, user_name, password, name, age, email, dept_id, salary) VALUES (1, 'zhangsan', '123456', '张三', 18, '[email protected]', 222, 2000);
INSERT INTO mp.tb_user (id, user_name, password, name, age, email, dept_id, salary) VALUES (2, 'lisi', '123456', '李四', 20, '[email protected]', 333, 3000);
INSERT INTO mp.tb_user (id, user_name, password, name, age, email, dept_id, salary) VALUES (3, 'wangwu', '123456', '王五', 28, '[email protected]', 222, 4000);
INSERT INTO mp.tb_user (id, user_name, password, name, age, email, dept_id, salary) VALUES (4, 'zhaoliu', '123456', '赵六', 21, '[email protected]', 333, 5000);
INSERT INTO mp.tb_user (id, user_name, password, name, age, email, dept_id, salary) VALUES (5, 'sunqi', '123456', '孙七', 24, '[email protected]', 222, 2000);
INSERT INTO mp.tb_user (id, user_name, password, name, age, email, dept_id, salary) VALUES (6, 'sajkdl', '123456', 'sajkdl', 18, '[email protected]', 222, 4000);
INSERT INTO mp.tb_user (id, user_name, password, name, age, email, dept_id, salary) VALUES (7, 'dkfldk', '123456', 'dkfldk', 20, '[email protected]', 333, 3000);
INSERT INTO mp.tb_user (id, user_name, password, name, age, email, dept_id, salary) VALUES (8, 'rpewp', '123456', 'rpewp', 28, '[email protected]', 222, 2000);
INSERT INTO mp.tb_user (id, user_name, password, name, age, email, dept_id, salary) VALUES (9, 'ksjd', '123456', 'ksjd', 21, '[email protected]', 333, 2000);
INSERT INTO mp.tb_user (id, user_name, password, name, age, email, dept_id, salary) VALUES (10, 'kzzlk', '123456', 'kzzlk', 24, '[email protected]', 222, 3000);
INSERT INTO mp.tb_user (id, user_name, password, name, age, email, dept_id, salary) VALUES (11, 'weiro', '123456', 'weiro', null, '[email protected]', 222, 5000);

三、代码示例

下面是@Test注解的示例

package com.example.itcastmybatisplus;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.itcastmybatisplus.domain.TbUser;
import com.example.itcastmybatisplus.domain.query.UserQuery;
import com.example.itcastmybatisplus.mapper.TbUserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class ItcastMybatisPlusApplicationTests {

    @Test
    void contextLoads() {
    }
    @Autowired private TbUserMapper userMapper;

//    测试函数
    @Test public void A(){
        List<TbUser> tbUsers = userMapper.selectList(null);
        System.out.println(tbUsers);
    }
//    条件查询 查找所有年龄<22岁的user
    @Test public void B(){
        QueryWrapper<TbUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("age",22);
        List<TbUser> tbUsers = userMapper.selectList(queryWrapper);
        System.out.println(tbUsers);
    }
    //QueryWrapper的基础上使用lambda
    @Test public void C(){
        QueryWrapper<TbUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().lt(TbUser::getAge,22);
        List<TbUser> tbUsers = userMapper.selectList(queryWrapper);
        System.out.println(tbUsers);
    }
//    查询年龄小于22岁的user
    @Test public void D(){
        LambdaQueryWrapper<TbUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.lt(TbUser::getAge,22);
        List<TbUser> tbUsers = userMapper.selectList(queryWrapper);
        System.out.println(tbUsers);
    }
    //条件查询,查询年龄在19岁以上,25岁一下的user
    @Test public void E(){
        LambdaQueryWrapper<TbUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.lt(TbUser::getAge,25);
        wrapper.gt(TbUser::getAge,19);
        List<TbUser> tbUsers = userMapper.selectList(wrapper);
        System.out.println(tbUsers);
    }
//    查询年龄小于20 '或者' 大于25岁的user
    @Test public void F(){
        LambdaQueryWrapper<TbUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.gt(TbUser::getAge,25).or().lt(TbUser::getAge,20);
        System.out.println(userMapper.selectList(wrapper));
    }
    //比较的字段始终只有一个age,因此如果不注释uq.setAge(20);和uq.setAge(25);的话,就是选出age在20和25之间的
    //如果注释uq.setAge(20);就是选出age<25的
    //如果注释uq.setAge2(25);就是选出age>20的
    @Test public void G(){
        UserQuery uq = new UserQuery();
        uq.setAge(20);
//        uq.setAge2(25);
        LambdaQueryWrapper<TbUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.lt(uq.getAge2()!=null,TbUser::getAge,uq.getAge2());
        wrapper.gt(uq.getAge()!=null,TbUser::getAge,uq.getAge());
        System.out.println(userMapper.selectList(wrapper));
    }
//    查询投影,选择特定的字段输出,没选择的字段输出为NULL
    @Test public void H(){
        LambdaQueryWrapper<TbUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(TbUser::getId,TbUser::getUserName);
        System.out.println(userMapper.selectList(wrapper));
    }
//    聚合函数查询
    @Test public void I(){
        //不能使用LambdaQueryWrapper
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        wrapper.select("count(*) as cnt , max(age) as max_age");
//        wrapper.select("max(age) as max_age");
        System.out.println(userMapper.selectMaps(wrapper));
    }
    @Test public void J(){
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
//        wrapper.select("count(*) as cnt , max(age) as max_age");
//        wrapper.lambda().groupBy(TbUser::getDeptId);
        wrapper.select("count(*) as count,email");
        wrapper.groupBy("email");
        System.out.println(userMapper.selectMaps(wrapper));
    }
//    登录查询,等值查询
    @Test public void K(){
        LambdaQueryWrapper<TbUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbUser::getUserName,"zhangsan").eq(TbUser::getPassword,"123456");
        TbUser tbUser = userMapper.selectOne(wrapper);
        System.out.println(tbUser);
    }
//    范围查询,对年龄进行范围查询,使用lt()、le()、gt()、ge()、between()进行范围查询
//    gt():大于(>)
//    ge():大于等于(>=)
//    lt():小于(<)
//    lte():小于等于(<=)
//    between():between ? and ?
    @Test public void L(){
        LambdaQueryWrapper<TbUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(TbUser::getAge,20,25);
        System.out.println(userMapper.selectList(wrapper));
    }
//    :查询表中name属性的值以J开头的用户信息,使用like进行模糊查询
//    like():前后加百分号,如 %J%
//    likeLeft():前面加百分号,如 %J
//    likeRight():后面加百分号,如 J%
    @Test public void M(){
        LambdaQueryWrapper<TbUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(TbUser::getName,"s");
        System.out.println(userMapper.selectList(wrapper));
        System.out.println("--------------------------------------------------");
        wrapper = new LambdaQueryWrapper<>();
        wrapper.like(TbUser::getName,"s");
        System.out.println(userMapper.selectList(wrapper));
    }

    @Test public void N(){
//        orderBy排序
//        condition:条件,true则添加排序,false则不添加排序
//        isAsc:是否为升序,true升序,false降序
//        columns:排序字段,可以有多个
        LambdaQueryWrapper<TbUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(TbUser::getSalary);
        System.out.println(userMapper.selectList(wrapper));
        System.out.println("--------------------------------------------------");
        /**
         * orderByAsc/Desc(单个column):按照指定字段进行升序/降序
         * orderByAsc/Desc(多个column):按照多个字段进行升序/降序
         * orderByAsc/Desc
         * condition:条件,true添加排序,false不添加排序
         * 多个columns:按照多个字段进行排序
         */
        wrapper = new LambdaQueryWrapper<>();
        wrapper.orderBy(true,false,TbUser::getSalary);
        System.out.println(userMapper.selectList(wrapper));
    }
}

你可能感兴趣的:(#,MyBatis,spring-,boot,mybatis,oracle,数据库)