尚医通学习笔记 Day2--Mybatis_plus简单条件全查询,物理删除和逻辑删除的运用

MP查询

多值查询
利用SelectBatchIds方法传入List集合

//多值查询
    @Test
    public void testSelectBatch(){
//        通过工具类Array中的list集合传入多个参数值
        List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
        System.out.println(users);
        

    }

特别注意在sql语句,使用了in作为连接条件

 Preparing: SELECT id,name,age,email,create_time,update_time,version FROM user WHERE id IN ( ? , ? , ? ) 
==> Parameters: 1(Integer), 2(Integer), 3(Integer)
<==    Columns: id, name, age, email, create_time, update_time, version
<==        Row: 1, zhangsan, 12, 2323231@qq.com, null, null, null
<==        Row: 2, lisi, 123, 123123@qq.com, null, null, null
<==      Total: 2

使用map做一个简单的条件组合查询(比较少用)

 //简单条件查询
    @Test
    public void testSelectByC(){
//        用map构建条件,再用
        Map<String,Object> columnMap=new HashMap<>();
        columnMap.put("name","niuzai");
        columnMap.put("age",12);
        List<User> users = userMapper.selectByMap(columnMap);
        System.out.println(users);


    }

分页查询

  1. 首先配置分页插件
//配置分页查询插件
   @Bean
   public PaginationInterceptor paginationInterceptor(){
       return new PaginationInterceptor();

   }
  1. 编写分页代码,获取分页相关参数,例如当前页数,每页显示几条,或者判断是否存在上下 一页
/    分页查询
    @Test
    public void  testSelectByPages(){
//        设置当前页码,每页显示
        Page<User> page=new Page(1,3);
       Page<User> userPage=userMapper.selectPage(page,null);
//       返回对象得到分页所有数据
        long pages = userPage.getPages();//总页数
        long current = userPage.getCurrent();//当前页数
        List<User> records = userPage.getRecords();//查询数据集合
        long size = userPage.getSize();//每页长度
        long total = userPage.getTotal();//总记录数
        boolean hasNext = userPage.hasNext();//是否存下一页
        boolean hasPrevious = userPage.hasPrevious();//是否存在上一页

        System.out.println(pages);
        System.out.println(current);
        System.out.println(records);
        System.out.println(size);
        System.out.println(total);
        System.out.println(hasNext);
        System.out.println(hasPrevious);


    }
 

删除(物理删除)

根据id删除或者ids删除多条数据

/根据id删除
    @Test
    public void testDelectById(){
        int i = userMapper.deleteById(1385609705744887809L);
        System.out.println(i);
    }
//根据多值删除数据
    @Test
    public void testDelectByBatchId(){
        int i = userMapper.deleteBatchIds(Arrays.asList(1, 2, 3));
        System.out.println(i);
    }

根据条件删除

//根据条件删除
    @Test
    public void testDelectByC(){
        Map<String,Object> columnMap=new HashMap<>();
        columnMap.put("name","niuPP");
//        获取受影响行数
        int i = userMapper.deleteByMap(columnMap);
        System.out.println(i);
    }

删除(逻辑)

  1. 在数据库中的表添加 deleted 字段作为标识,1 代表删除,0代表未删除
  2. 在实体类中添加该字段,并且注入TableLogic设置插入时,该字段默认为0
//  通过标识位进行逻辑上删除,配置信息是默认为0为未删除,1为已删除
    @TableLogic
    @TableField(fill = FieldFill.INSERT )
    private Integer deleted;
@Override
    public void insertFill(MetaObject metaObject) {
        //mp执行添加操作时自动填充往create_time字段中添加时间
        this.setFieldValByName("createTime",new Date(),metaObject);
        //添加数据时默认数据版本为1
        this.setFieldValByName("version",1,metaObject);
        this.setFieldValByName("deleted",0,metaObject);
    }
  1. 进行测试,发现sql语句写的是更新语句,并且执行逻辑删除后无法查找到deleted为1的数据,则完成逻辑删除
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值