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方法
in("age",{1,2,3})`--->`age in (1,2,3)
between("age", 18, 30)`--->`age between 18 and 30
notBetween("age", 18, 30)`--->`age not between 18 and 30
likeLeft("name", "王")`--->`name like '%王'
likeRight("name", "王")--->name like '王%'
isNull("name")-->name is null
orderByAsc("id", "name")--->order by id ASC,name ASC
orderByDesc("id", "name")--->order by id DESC,name DESC
inSql("id", "select id from table where id < 3")--->id in (select id from table where id < 3)