转:
码农必备linux技能
本专栏主要从系统安装、入门命令讲解,会不定期更新公司服务架构安装使用教程(如:Es、MQ、jenkins等等)。还不会linux? 赶紧跟我一起学习吧!
IT贱男
¥29.90
一、引言
那么那么那么今天来说下MP中强大的条件查询功能。
本章是基于上个案例来讲的:MyBaits-Plus 快速入门案例
二、具体操作
首先来说说基本的查询吧,根据主键或者简单的查询条件进行查询。
-
/**
-
* 通过单个ID主键进行查询
-
*/
-
-
public void selectById() {
-
User user = userMapper.selectById(1094592041087729666L);
-
System.out.println(user);
-
}
-
-
/**
-
* 通过多个ID主键查询
-
*/
-
-
public void selectByList() {
-
List<Long> longs = Arrays.asList(1094592041087729666L, 1094590409767661570L);
-
List<User> users = userMapper.selectBatchIds(longs);
-
users.forEach(System.out::println);
-
}
-
-
/**
-
* 通过Map参数进行查询
-
*/
-
-
public void selectByMap() {
-
Map<String, Object> params = new HashMap<>();
-
params.put("name", "张雨琪");
-
List<User> users = userMapper.selectByMap(params);
-
users.forEach(System.out::println);
-
}
MyBatis-Plus还提供了Wrapper条件构造器,具体使用看如下代码:
-
/**
-
* 名字包含雨并且年龄小于40
-
* <p>
-
* WHERE name LIKE '%雨%' AND age < 40
-
*/
-
-
public void selectByWrapperOne() {
-
QueryWrapper<User> wrapper = new QueryWrapper();
-
wrapper.like("name", "雨").lt("age", 40);
-
List<User> users = userMapper.selectList(wrapper);
-
users.forEach(System.out::println);
-
}
-
-
/**
-
* 名字包含雨
-
* 年龄大于20小于40
-
* 邮箱不能为空
-
* <p>
-
* WHERE name LIKE '%雨%' AND age BETWEEN 20 AND 40 AND email IS NOT NULL
-
*/
-
-
public void selectByWrapperTwo() {
-
QueryWrapper<User> wrapper = Wrappers.query();
-
wrapper.like("name", "雨").between("age", 20, 40).isNotNull("email");
-
List<User> users = userMapper.selectList(wrapper);
-
users.forEach(System.out::println);
-
}
-
-
/**
-
* 名字为王性
-
* 或者年龄大于等于25
-
* 按照年龄降序排序,年龄相同按照id升序排序
-
* <p>
-
* WHERE name LIKE '王%' OR age >= 25 ORDER BY age DESC , id ASC
-
*/
-
-
public void selectByWrapperThree() {
-
QueryWrapper<User> wrapper = Wrappers.query();
-
wrapper.likeRight("name", "王").or()
-
.ge("age", 25).orderByDesc("age").orderByAsc("id");
-
List<User> users = userMapper.selectList(wrapper);
-
users.forEach(System.out::println);
-
}
-
-
/**
-
* 查询创建时间为2019年2月14
-
* 并且上级领导姓王
-
* <p>
-
* WHERE date_format(create_time,'%Y-%m-%d') = '2019-02-14' AND manager_id IN (select id from user where name like '王%')
-
*/
-
-
public void selectByWrapperFour() {
-
QueryWrapper<User> wrapper = Wrappers.query();
-
wrapper.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(wrapper);
-
users.forEach(System.out::println);
-
}
-
-
/**
-
* 查询王姓
-
* 并且年龄小于40或者邮箱不为空
-
* <p>
-
* WHERE name LIKE '王%' AND ( age < 40 OR email IS NOT NULL )
-
*/
-
-
public void selectByWrapperFive() {
-
QueryWrapper<User> wrapper = Wrappers.query();
-
wrapper.likeRight("name", "王").and(qw -> qw.lt("age", 40).or().isNotNull("email"));
-
List<User> users = userMapper.selectList(wrapper);
-
users.forEach(System.out::println);
-
}
-
-
/**
-
* 查询王姓
-
* 并且年龄大于20 、年龄小于40、邮箱不能为空
-
* <p>
-
* WHERE name LIKE ? OR ( age BETWEEN ? AND ? AND email IS NOT NULL )
-
*/
-
-
public void selectByWrapperSix() {
-
QueryWrapper<User> wrapper = Wrappers.query();
-
wrapper.likeRight("name", "王").or(
-
qw -> qw.between("age", 20, 40).isNotNull("email")
-
);
-
List<User> users = userMapper.selectList(wrapper);
-
users.forEach(System.out::println);
-
}
-
-
/**
-
* (年龄小于40或者邮箱不为空) 并且名字姓王
-
* WHERE ( age < 40 OR email IS NOT NULL ) AND name LIKE '王%'
-
*/
-
-
public void selectByWrapperSeven() {
-
QueryWrapper<User> wrapper = Wrappers.query();
-
wrapper.nested(qw -> qw.lt("age", 40).or().isNotNull("email"))
-
.likeRight("name", "王");
-
List<User> users = userMapper.selectList(wrapper);
-
users.forEach(System.out::println);
-
}
-
-
/**
-
* 查询年龄为30、31、32
-
* WHERE age IN (?,?,?)
-
*/
-
-
public void selectByWrapperEight() {
-
QueryWrapper<User> wrapper = Wrappers.query();
-
wrapper.in("age", Arrays.asList(30, 31, 32));
-
List<User> users = userMapper.selectList(wrapper);
-
users.forEach(System.out::println);
-
}
-
-
/**
-
* 查询一条数据
-
* limit 1
-
*/
-
-
public void selectByWrapperNine() {
-
QueryWrapper<User> wrapper = Wrappers.query();
-
wrapper.in("age", Arrays.asList(30, 31, 32)).last("limit 1");
-
List<User> users = userMapper.selectList(wrapper);
-
users.forEach(System.out::println);
-
}