springboot_表现层数据一致性处理

 

package com.example.demo07.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo07.controller.util.R;
import com.example.demo07.dto.Book;
import com.example.demo07.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    IBookService bookService;

    @GetMapping
    public R getAll(){
        //该方法继承自IService
        List<Book> bookList = bookService.list();
        R r = new R();
        r.setData(bookList);
        r.setFlag(true);
        return r;
    }

    @PostMapping
    //@RequestBody 接收的是请求体里面的数据(post方式参数在请求体中),@RequestBody以简单对象接收前端传过来的json数据
    public R save(@RequestBody Book book){
        //该方法继承自IService
        Boolean result = bookService.save(book);
        R r = new R(result);
        return r;
    }

    @PutMapping
    public R update(@RequestBody Book book){
        //该方法继承自IService
        Boolean result = bookService.updateById(book);
        R r = new R(result);
        return r;
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id){
        //该方法继承自IService
        Boolean result = bookService.removeById(id);
        return new R(result);
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id){
        //该方法继承自IService
        Book book = bookService.getById(id);
        return new R( true,book);
    }

    @GetMapping("/{currentPage}/{pageSize}")
    public R getPage( @PathVariable int currentPage, @PathVariable int pageSize ){
        IPage<Book> page = bookService.getPage(currentPage,pageSize);
        return new R(true,page);
    }
}

 service接口

package com.example.demo07.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo07.dto.Book;


public interface IBookService extends IService<Book> {

    IPage<Book> getPage(int currentPage, int pageSize);
}

service实现

package com.example.demo07.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo07.dao.BookDao;
import com.example.demo07.dto.Book;
import com.example.demo07.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {

    @Autowired
    private BookDao bookDao;

    @Override
    public IPage<Book> getPage(int currentPage, int pageSize) {
        IPage page = new Page(currentPage,pageSize);
        bookDao.selectPage(page,null);
        return page;
    }
}

测试

 

结果

{
    "flag": true,
    "data": [
        {
            "id": 1,
            "type": null,
            "name": "毛选",
            "description": null
        },
        {
            "id": 2,
            "type": "哲学",
            "name": "局外人",
            "description": null
        },
        {
            "id": 3,
            "type": "文学",
            "name": "葡萄牙的高山",
            "description": "苦难"
        },
        {
            "id": 4,
            "type": "文学",
            "name": "活着",
            "description": "苦难"
        },
        {
            "id": 5,
            "type": "文学",
            "name": "平凡的世界",
            "description": null
        },
        {
            "id": 6,
            "type": "文学",
            "name": "孩子王",
            "description": null
        },
        {
            "id": 7,
            "type": "文学",
            "name": "孔乙己",
            "description": null
        }
    ]
}

测试

结果

{
    "flag": true,
    "data": {
        "records": [
            {
                "id": 1,
                "type": null,
                "name": "毛选",
                "description": null
            },
            {
                "id": 2,
                "type": "哲学",
                "name": "局外人",
                "description": null
            },
            {
                "id": 3,
                "type": "文学",
                "name": "葡萄牙的高山",
                "description": "苦难"
            }
        ],
        "total": 7,
        "size": 3,
        "current": 1,
        "orders": [],
        "optimizeCountSql": true,
        "searchCount": true,
        "countId": null,
        "maxLimit": null,
        "pages": 3
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值