9.MyBatis Plus条件构造器Wrapper

9.MyBatis Plus条件构造器Wrapper

​ 先上代码测试

package com.xuan;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuan.mapper.UserMapper;
import com.xuan.pojo.User;
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
public class WapperTest {

    @Autowired
    UserMapper userMapper;

    @Test
    public void testWapper(){
        //查询name不为空,邮箱不为空,年龄大于20的用户
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper
                .isNotNull("name")
                .isNotNull("email")
                .ge("age",20);
        List<User> users = userMapper.selectList(wrapper);
        users.forEach(System.out::println);
    }
}
-- sql语句
SELECT id,name,age,email,version,deleted,gmt_create,gmt_modified FROM user WHERE deleted=0 AND (name IS NOT NULL AND email IS NOT NULL AND age >= 20)

​ 接下来分析几个常用的wrapper方法

  • allEq :全部相等

  • eq:相等

  • ne:不等于<>

  • gt:大于

  • ge:大于等于

  • lt:小于

  • le:小于等于

  • in:在…里面

in("age",{1,2,3})`--->`age in (1,2,3)
  • between:

between("age", 18, 30)`--->`age between 18 and 30
  • notBetween:

notBetween("age", 18, 30)`--->`age not between 18 and 30
  • like:模糊查询 %值%

  • notLike:不是~~值

  • likeLeft:百分号在左边

likeLeft("name", "王")`--->`name like '%王'
  • likeRight:百分号在右边

likeRight("name", "王")--->name like '王%'
  • isNull:非空

isNull("name")-->name is null
  • orderByAsc:升序

orderByAsc("id", "name")--->order by id ASC,name ASC
  • orderByDesc:降序

orderByDesc("id", "name")--->order by id DESC,name DESC
  • inSql:子查询

inSql("id", "select id from table where id < 3")--->id in (select id from table where id < 3)

你可能感兴趣的:(MyBatis,Plus由浅入深(跟着官网走))