史上最全SpringBoot教程,从零开始带你深入♂学习(十三)——整合mybatis-plus(三)

142 阅读2分钟

Springboot(十三)——整合mybatis-plus(三)

查询操作

根据ID查询用户

//根据ID查询用户
@Test
public void testSelectById(){
    User user = userMapper.selectById(1L);
    System.out.println(user);
}

领取资料

image

批量查询

@Test
public void testSelectByBatchId(){
    List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
    users.forEach(System.out::println);
}

领取资料

image

条件查询

@Test
public void testSelectByBatchIds(){
    HashMap<String, Object> map = new HashMap<>();
    //自定义条件查询,查询name=zhangsan并且age=19的用户
    map.put("name","zhangsan");
    map.put("age",19);
    List<User> users = userMapper.selectByMap(map);
    users.forEach(System.out::println);
}

领取资料

image

分页查询操作

领取资料

分页在网站使用十分之多!

  • 1、原始的limit进行分页
  • 2、pageHelper第三方插件
  • 3、Mybatis-plus内置插件

编写mybatis-plus配置类

 // 最新版
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
    return interceptor;
}

编写测试类

//测试分页查询
@Test
public void testPage(){
    //参数一:第一页   参数二:页面显示5条数据
    Page<User> page = new Page<>(1,5);
    userMapper.selectPage(page,null);

    page.getRecords().forEach(System.out::println);
}

测试结果 image 领取资料

删除操作

通过ID删除

@Test
public void testDeleteById(){
    userMapper.deleteById("1410758327595810819L");
}

控制台输出 image

查看数据库 image

批量删除

@Test
public void testDeleteBatchId(){
    userMapper.deleteBatchIds(Arrays.asList(1410758327595810818L,7L,6L));
}

控制台输出 image

查看数据库 image

条件删除

@Test
public void testDeleteMap(){
    HashMap<String, Object> map = new HashMap<>();
    map.put("name","zhangsan2");
    userMapper.selectByMap(map);
}

领取资料

控制台输出 image

查看数据库 image

逻辑删除

官方文档:mp.baomidou.com/guide/logic…

物理删除:从数据库中直接移除 逻辑删除:在数据库中没有被移除,而是通过一个变量来让他失效!deleted = 0 or deleted = 1

管理员可以查看被删除的记录!防止数据丢失,类似于回收站

1、在数据库中添加一个deleted字段

image

2、在实体类中添加属性

@TableLogic //逻辑删除注解
private Integer deleted;

3、配置

特别注意:mybatis-plus 3.3版本之后,不需要配置application配置文件,直接使用注解即可!不然会报错 mybatis-plus 3.3版本以上自行忽略一下配置

mybatis-plus:
  global-config:
    db-config:
      logic-delete-field: flag  # 全局逻辑删除的实体字段名(since 3.3.0,配置后属性可以不用添加注解)
      logic-delete-value: 1 # 逻辑已删除值(默认为 1)
      logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)

测试

控制台输出 image

查看数据库 image

条件构造器

测试一

@Test
void contextLoads(){
    // 查询name和邮箱不为空,并且年龄大于或等于12岁的用户
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.isNotNull("name")//名字不为空
            .isNotNull("email")//邮箱不为空
            .ge("age",12);//年龄大于或等于12岁
    userMapper.selectList(wrapper).forEach(System.out::println); //
}

测试二

@Test
void test2(){
    //查询名字叫zhangsan的用户
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.eq("name","zhangsan");
    User user = userMapper.selectOne(wrapper);
    System.out.println(user);
}

测试三

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

测试四

//模糊查询
@Test
void test4(){
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.notLike("name","e") //name不包含e   %e%
            .likeRight("email","t");//并且email是以t开头的  t%

    List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
    maps.forEach(System.out::println);
}

测试五

@Test
void test5(){
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    // id 在子查询中查出来
    wrapper.inSql("id","select id from mybatis_plus.user where id<3");

    List<Object> objects = userMapper.selectObjs(wrapper);
    objects.forEach(System.out::println);
}

测试六

@Test
void test6(){
    QueryWrapper<User> wrapper = new QueryWrapper<>();
    // 通过id进行降序排序
    wrapper.orderByDesc("id");
    List<User> users = userMapper.selectList(wrapper);
    users.forEach(System.out::println);
}

最后,祝大家早日学有所成,拿到满意offer,快速升职加薪,走上人生巅峰。 可以的话请给我一个三连支持一下我哟,我们下期再见

领取资料