Mybatis-Plus条件构造器查询

Mybatis-Plus使用条件构造器(QueryWrapper)进行条件查询的9个小例子

首先创建user表

CREATE TABLE user (
    id BIGINT ( 20 ) PRIMARY KEY NOT NULL COMMENT '主键',
    name VARCHAR ( 30 ) DEFAULT NULL COMMENT '姓名',
    age INT ( 11 ) DEFAULT NULL COMMENT '年龄',
    email VARCHAR ( 50 ) DEFAULT NULL COMMENT '邮箱',
    manager_id BIGINT ( 20 ) DEFAULT NULL COMMENT '直属上级id',
    create_time DATETIME DEFAULT NULL COMMENT '创建时间',
CONSTRAINT manager_fk FOREIGN KEY ( manager_id ) REFERENCES USER ( id ) 
) ENGINE = INNODB CHARSET = UTF8;

插入几条数据

INSERT INTO `user` ( id, name, age, email, manager_id, create_time )
VALUES
    ( 1087982257332887553, '大boss', 40, 'boss@baomidou.com', NULL, '2019-01-11 14:20:20' ),
    ( 1088248166370832385, '王天风', 25, 'wtf@baomidou.com', 1087982257332887553, '2019-02-05 11:12:22' ),
    ( 1088250446457389058, '李艺伟', 28, 'lyw@baomidou.com', 1088248166370832385, '2019-02-14 08:31:16' ),
    ( 1094590409767661570, '张雨琪', 31, 'zjq@baomidou.com', 1088248166370832385, '2019-01-14 09:15:15' ),
    ( 1094592041087729666, '刘红雨', 31, 'lhm@baomidou.com', 1088248166370832385, '2019-01-14 09:48:16' );
  1. 名字中包含“雨”并且年龄小于40
    name like '%雨%' and age<40
    @Test
    public void selectByWrapper1() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        // lt为小于的意思
        queryWrapper.like("name", "雨").lt("age", 40);
        List<User> users = userMapper.selectList(queryWrapper);
        users.forEach(System.out::println);
    }

日志输出sql:

DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND age < ?
DEBUG==> Parameters: %雨%(String), 40(Integer)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1094590409767661570, 张雨琪, 31, zjq@baomidou.com, 1088248166370832385, 2019-01-14 09:15:15
TRACE<== Row: 1094592041087729666, 刘红雨, 31, lhm@baomidou.com, 1088248166370832385, 2019-01-14 09:48:16
DEBUG<== Total: 2

  1. 名字中包含“雨”并且年龄大于等于20且小于等于40并且email不为空
    name like '%雨%' and age between 20 and 40 and email is not null
    @Test
    public void selectByWrapper2() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", "雨").between("age", 20, 40).isNotNull("email");
        List<User> users = userMapper.selectList(queryWrapper);
        users.forEach(System.out::println);
    }

日志输出sql:

DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL
DEBUG==> Parameters: %雨%(String), 20(Integer), 40(Integer)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1094590409767661570, 张雨琪, 31, zjq@baomidou.com, 1088248166370832385, 2019-01-14 09:15:15
TRACE<== Row: 1094592041087729666, 刘红雨, 31, lhm@baomidou.com, 1088248166370832385, 2019-01-14 09:48:16
DEBUG<== Total: 2

  1. 名字为“王”姓或者年龄大于等于25,按照年龄降序排列,年龄相同按照id升序排列
    name like '王%' or age>=25 order by age desc,id,asc
    @Test
    public void selectByWrapper3() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        // ge为大于等于的意思
        queryWrapper.likeRight("name", "王").or().ge("age", 25)
                .orderByDesc("age").orderByAsc("id");
        List<User> users = userMapper.selectList(queryWrapper);
        users.forEach(System.out::println);
    }

日志输出sql:

DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? OR age >= ? ORDER BY age DESC , id ASC
DEBUG==> Parameters: 王%(String), 25(Integer)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1087982257332887553, 大boss, 40, boss@baomidou.com, null, 2019-01-11 14:20:20
TRACE<== Row: 1094590409767661570, 张雨琪, 31, zjq@baomidou.com, 1088248166370832385, 2019-01-14 09:15:15
TRACE<== Row: 1094592041087729666, 刘红雨, 31, lhm@baomidou.com, 1088248166370832385, 2019-01-14 09:48:16
TRACE<== Row: 1088250446457389058, 李艺伟, 28, lyw@baomidou.com, 1088248166370832385, 2019-02-14 08:31:16
TRACE<== Row: 1088248166370832385, 王天风, 25, wtf@baomidou.com, 1087982257332887553, 2019-02-05 11:12:22
DEBUG<== Total: 5

  1. 创建日期为2019年2月14日并且直属上级名字为王姓
    date_format(create_time,'%Y-%m-%d') and manager_id in (select id from user where name like '王%')
    @Test
    public void selectByWrapper4() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply("date_format(create_time,'%Y-%m-%d')={0}", "2019-02-14")
                .inSql("manager_id", "select id from user where name like '王%'");
        List<User> users = userMapper.selectList(queryWrapper);
        users.forEach(System.out::println);
    }

日志输出sql:

DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE date_format(create_time,'%Y-%m-%d')=? AND manager_id IN (select id from user where name like '王%')
DEBUG==> Parameters: 2019-02-14(String)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1088250446457389058, 李艺伟, 28, lyw@baomidou.com, 1088248166370832385, 2019-02-14 08:31:16
DEBUG<== Total: 1

  1. 名字为“王”姓并且(年龄小于40或邮箱不为空)
    name like '王%' and (age<40 or email is not null)
    @Test
    public void selectByWrapper5() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("name", "王")
                .and(wq -> wq.lt("age", 40).or().isNotNull("email"));
        List<User> users = userMapper.selectList(queryWrapper);
        users.forEach(System.out::println);
    }

日志输出sql:

DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? AND ( age < ? OR email IS NOT NULL )

DEBUG==> Parameters: 王%(String), 40(Integer)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1088248166370832385, 王天风, 25, wtf@baomidou.com, 1087982257332887553, 2019-02-05 11:12:22
DEBUG<== Total: 1

  1. 名字为“王”姓或者(年龄小于40并且年龄大于20并且邮箱不为空)
    name like '王%' or (age<40 and age>20 and email is not null)
    @Test
    public void selectByWrapper6() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("name", "王")
                .or(wq -> wq.lt("age", 40).gt("age", 20).isNotNull("email"));
        List<User> users = userMapper.selectList(queryWrapper);
        users.forEach(System.out::println);
    }

日志输出sql:

DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE name LIKE ? OR ( age < ? AND age > ? AND email IS NOT NULL )
DEBUG==> Parameters: 王%(String), 40(Integer), 20(Integer)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1088248166370832385, 王天风, 25, wtf@baomidou.com, 1087982257332887553, 2019-02-05 11:12:22
TRACE<== Row: 1088250446457389058, 李艺伟, 28, lyw@baomidou.com, 1088248166370832385, 2019-02-14 08:31:16

TRACE<== Row: 1094590409767661570, 张雨琪, 31, zjq@baomidou.com, 1088248166370832385, 2019-01-14 09:15:15
TRACE<== Row: 1094592041087729666, 刘红雨, 31, lhm@baomidou.com, 1088248166370832385, 2019-01-14 09:48:16
DEBUG<== Total: 4

  1. (年龄小于40或邮箱不为空)并且名字为王姓
    (age<40 or email is not null) and name like '王%'
    @Test
    public void selectByWrapper7() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.nested(wq -> wq.lt("age", 40).or().isNotNull("email"))
                .likeRight("name", "王");
        List<User> users = userMapper.selectList(queryWrapper);
        users.forEach(System.out::println);
    }

日志输出sql:

DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE ( age < ? OR email IS NOT NULL ) AND name LIKE ?
DEBUG==> Parameters: 40(Integer), 王%(String)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1088248166370832385, 王天风, 25, wtf@baomidou.com, 1087982257332887553, 2019-02-05 11:12:22
DEBUG<== Total: 1

  1. 年龄为30,31,34,35
    age in (30,31,34,35)
    @Test
    public void selectByWrapper8() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("age", Arrays.asList(30, 31, 34, 35));
        List<User> users = userMapper.selectList(queryWrapper);
        users.forEach(System.out::println);
    }

日志输出sql:

DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE age IN (?,?,?,?)
DEBUG==> Parameters: 30(Integer), 31(Integer), 34(Integer), 35(Integer)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1094590409767661570, 张雨琪, 31, zjq@baomidou.com, 1088248166370832385, 2019-01-14 09:15:15
TRACE<== Row: 1094592041087729666, 刘红雨, 31, lhm@baomidou.com, 1088248166370832385, 2019-01-14 09:48:16
DEBUG<== Total: 2

  1. 只返回满足条件的其中一条语句即可
    limit 1
    @Test
    public void selectByWrapper9() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        // 无视优化规则直接拼接到 sql 的最后
        // 注意事项:
        // 只能调用一次,多次调用以最后一次为准 有sql注入的风险,请谨慎使用
        queryWrapper.in("age", Arrays.asList(30, 31, 34, 35)).last("limit 1");
        List<User> users = userMapper.selectList(queryWrapper);
        users.forEach(System.out::println);
    }

日志输出sql:

DEBUG==> Preparing: SELECT id,name,age,email,manager_id,create_time FROM user WHERE age IN (?,?,?,?) limit 1
DEBUG==> Parameters: 30(Integer), 31(Integer), 34(Integer), 35(Integer)
TRACE<== Columns: id, name, age, email, manager_id, create_time
TRACE<== Row: 1094590409767661570, 张雨琪, 31, zjq@baomidou.com, 1088248166370832385, 2019-01-14 09:15:15
DEBUG<== Total: 1

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,732评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,496评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,264评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,807评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,806评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,675评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,029评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,683评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,704评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,666评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,773评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,413评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,016评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,204评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,083评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,503评论 2 343