一、引言
那么那么那么今天来说下MP中强大的条件查询功能。
本章是基于上个案例来讲的:MyBaits-Plus 快速入门案例
二、具体操作
首先来说说基本的查询吧,根据主键或者简单的查询条件进行查询。
-
-
-
-
-
public void selectById() {
-
User user = userMapper.selectById(1094592041087729666L);
-
System.out.println(user);
-
-
-
-
-
-
-
public void selectByList() {
-
List<Long> longs = Arrays.asList(1094592041087729666L, 1094590409767661570L);
-
List<User> users = userMapper.selectBatchIds(longs);
-
users.forEach(System.out::println);
-
-
-
-
-
-
-
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条件构造器,具体使用看如下代码:
-
-
-
-
-
-
-
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);
-
-
-
-
-
-
-
-
-
-
-
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);
-
-
-
-
-
-
-
-
-
-
-
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);
-
-
-
-
-
-
-
-
-
-
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);
-
-
-
-
-
-
-
-
-
-
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);
-
-
-
-
-
-
-
-
-
-
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);
-
-
-
-
-
-
-
-
public void selectByWrapperSeven() {
-
QueryWrapper<User> wrapper = Wrappers.query();
-
wrapper.nested(qw -> qw.lt("age", 40).or().isNotNull("email"))
-
-
List<User> users = userMapper.selectList(wrapper);
-
users.forEach(System.out::println);
-
-
-
-
-
-
-
-
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);
-
-
-
-
-
-
-
-
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);
-