SpringBoot集成Mybatis-plus(二)---条件构造器

Mybatis-plus条件构造器:https://mp.baomidou.com/guide/wrapper.html#abstractwrapper

1、测试QueryWrapper对象isNotNul()ge()

  @Test
  void testWrapper01(){
      //查询name、email不为空,并且年龄大于3岁
      QueryWrapper<User> wrapper = new QueryWrapper<>();
      wrapper.isNotNull("name")
              .isNotNull("email")
              .ge("age",12);
      List<User> users = userMapper.selectList(wrapper);
      users.forEach(System.out::println);
  }

2、测试QueryWrapper对象eq()

@Test
void test02(){
    //查询年龄在20~30岁之间的用户
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.eq("name","Jone");
    User user = userMapper.selectOne(wrapper);
    System.out.println(user);
}

3、测试QueryWrapper对象between()

@Test
void test03(){
    //查询年龄在20~30岁之间的用户
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.between("age",20,30);
    Integer count = userMapper.selectCount(wrapper);
    System.out.println(count);
}

SpringBoot集成Mybatis-plus(二)---条件构造器
4、测试like()


@Test
void test04(){
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    //likeRight  t%
    wrapper
            .like("name","e")
            .likeRight("email","t");
    List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
    maps.forEach(System.out::println);
}

SpringBoot集成Mybatis-plus(二)---条件构造器
5、嵌套查询

@Test
void test05(){
     QueryWrapper<User> wrapper = new QueryWrapper<>();
     wrapper.inSql("id","select id from user where id<3");
     List<Object> objects = userMapper.selectObjs(wrapper);
     objects.forEach(System.out::println);
 }

SpringBoot集成Mybatis-plus(二)---条件构造器
6、排序

@Test
void test06(){
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.orderByDesc("id");
    List<User> users = userMapper.selectList(wrapper);
    users.forEach(System.out::println);
}

SpringBoot集成Mybatis-plus(二)---条件构造器

上一篇:Mybatis-Plus学习.,简化你的开发,提升开发效率.


下一篇:mybatis-plus