欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mybatis-plus增删查改与前端数据返回总结

程序员文章站 2022-09-05 14:54:36
概述使用mybatis-plus的增删查改,不免会需要想前端返回数据,在向前端返回数据的时候,会需要做一些判断,其实出来查的判断特殊外,其他的均可以参考if (iTbBrandService.removeById(id)){ return new Result("1","删除成功"); }else { return new Result("2","删除失败"); }进行具体@RequestMapping("/logi...

概述

使用mybatis-plus的增删查改,不免会需要想前端返回数据,在向前端返回数据的时候,会需要做一些判断,其实出来查的判断特殊外,其他的均可以参考

if (iTbBrandService.removeById(id)){

           return new Result("1","删除成功");

       }else {

           return new Result("2","删除失败");

       }

具体

@RequestMapping("/logicDelete")
    public Result logicDelete(int id){

       if (iTbBrandService.removeById(id)){

           return new Result("1","删除成功");

       }else {

           return new Result("2","删除失败");

       }

    }

    @RequestMapping("/findAll")
    public List<TbBrand> testcontroller(){

            return iTbBrandService.list();

    }


    //分页返回brand对象
    @RequestMapping("/findBrandPage")
    public Result findBrandPage(@RequestBody TbBrand tbBrand, Integer currentPage, Integer size){


        if (currentPage==null||size==null){

            return new Result("0","error");

        }

        QueryWrapper<TbBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("first_char","COUNT(*)").groupBy("first_char").having("count(*)>{0}",2);

        IPage<TbBrand> iPage = iTbBrandService.page(new Page<>(currentPage,size),queryWrapper);

        return new Result("1","good",new PageResult(iPage.getTotal(),iPage.getRecords()));

    }

    //增加
    @RequestMapping("/saveBrand")
    public Result saveBrand(@RequestBody TbBrand tbBrand){

        //iTbBrandService.save(tbBrand);

        if (iTbBrandService.save(tbBrand)){

            return new Result("1","保存成功");

        }else {

            return new Result("2","保存失败");

        }


    }

    //findOneById
    @RequestMapping("/findOneById")
    public Result findOneById(int id){
        QueryWrapper<TbBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        List<TbBrand> tbBrandlist = iTbBrandService.list(queryWrapper);

        if (tbBrandlist!=null&&tbBrandlist.size()>0){

            return new Result("1","查询成功",tbBrandlist);

        }else {

            return new Result("2","查询失败");

        }


    }

    //修改
    @RequestMapping("/updateBrand")
    public Result updateBrand(@RequestBody TbBrand tbBrand){

        //修改逻辑是,先查询,那么你应该先给我一个id,

        if (iTbBrandService.updateById(tbBrand)){

            return new Result("1","更新成功");

        }else {

            return new Result("2","更新失败");

        }

    }

看到相同点和不同点了嘛?

顺带提一句的是,其实调用iTbBrandService的增删查改方法,实际上是调用baseMapper的增删查改方法,只不过iTbBrandService对baseMapper进行了封装,在选择用哪个的时候,怎么方便怎么来吧

本文地址:https://blog.csdn.net/weixin_40598838/article/details/108986077

相关标签: mybatis-plus