springboot中表现层标准开发--基于Restful进行开发,使用Postman测试表现层接口功能

接口:

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.company.domain.Book;

//业务层快速开发
public interface IBookService extends IService {
//我们在数据层是extends BaseMapper 和这里不太一样 不要混了
//    如果我们需要的方法他们没有提供,我们可以自己在这里写,之后再IBookServiceImpl中进行实现
//    @Override
//  Boolean save(Book book);   如果这个地方报错了,就说明重名了,我们就换一个名字
//    这样就避免了我们手写方法和他们的方法重合了
    IPage getPage(int currentPage, int pageSize);
}

实体:

//@Getter所有的get方法
//@Setter所有的set方法
//@NoArgsConstructor 无参构造
//@AllArgsConstructor//全部的构造

@Data  //get+set+toString+hashcode+equals  但是没有构造方法
@TableName(value = "t_book")
public class Book {
    //    这里的属性名 要和数据库表中的属性名一致,要不然最终的查询结果是null
//    将数据库中的结果对此变量名进行注入
    @TableId(value="id",type = IdType.AUTO)  //代表自增算法
    @TableField(value = "id")
    private int id;
    @TableField(value = "bookName")
    private String bookName;
    @TableField(value = "statue")
    private String statue;

}

实现类:

import java.util.List;

//业务层快速开发
@Service  //定义bean   ; BookDao是通过这个实现数据库的操作  里面有数据库中各种各样的操作;Book是实体类
//IBookService 是提供的业务层接口
public class IBookServiceImpl extends ServiceImpl implements IBookService {
   @Autowired
   private BookDao bookDao;
    @Override
    public IPage getPage(int currentPage, int pageSize) {
        IPage page = new Page<>(1,5);
    bookDao.selectPage(page,null);
        return page;
    }


//    先比较与标准开发,这里不需要自己写基本的增删改查了(这就是一个优势,提高了效率)
//    如果我们需要的方法他们没有,我们仍然要在接口中定义,然后自己写
//    具体怎么写,就是回归到我们上面的标准开发了
//    但是这些方法要在接口中进行实现!!!!!否则就会报错!!!!!
//    @Autowired
//    private BookDao bookDao;
//    @Override
//    public Boolean save(Book book) {
//        //bookDao.insert(book)返回的是影响的行计数  我们让他>0,就说明有影响的数据,就返回true
//        return bookDao.insert(book) >0;
//    }
//
//    @Override
//    public Boolean update(Book book) {
//        return bookDao.updateById(book) >0;
//    }
//
//    @Override
//    public Boolean delete(Integer id) {
//        return bookDao.deleteById(id) >0;
//    }
//
//    @Override
//    public Book getById(Integer id) {
//        return bookDao.selectById(id);
//    }
//
//    @Override
//    public List getAll() {
//        return bookDao.selectList(null);// null说明查询的时候没有条件,那这就是 查询全部数据
//    }
//
//    @Override //分页操作
//    public IPage getPage(int currentPage, int pageSize) {
//        IPage iPage = new Page(currentPage,pageSize);
//        return bookDao.selectPage(iPage,null);  //null是查询条件
//    }

}

基于Restful开发:

import java.util.List;

//表现层开发:基于Restful进行开发
//使用REST风格对资源进行访问成为Restful
@RestController   //这个注释的作用就是@Controller+@ResponseBody
@RequestMapping("/books")  //描述模块的名称通常使用复数
//这两个注释在类上写了之后,相同的部分下面就不用写了
public class BookController {
    /*
    *       http://localhost/books      查询全部用户信息  Get 查询
    *     * http://localhost/books/id   查询指定用户信息  Get 查询
    *     * http://localhost/books      添加某个用户信息  Post (新增、保存)
    *     * http://localhost/books      修改用户用户信息  Put (修改、更新)
    *     * http://localhost/books/id   删除某个用户信息  Delete
    * */


//    我们要调用业务层,那就提前注入业务层
    @Autowired
    private IBookService iBookService;

//    return是return在页面上

//请求方式是get请求
    @GetMapping
    public List getAll(){
     return iBookService.list();  //在快速开发业务层中,list()方法表示得到全部的数据
//        这个最终会展示在页面上
    }

//    post提交   保存一个Book对象(添加一个对象)
    @PostMapping
    public  Boolean save(@RequestBody Book book){ //@RequestBody请求体参数  都是传JSON数据过来
        return iBookService.save(book);
    }

    //    put提交    修改一个Book对象
    @PutMapping
    public  Boolean update(@RequestBody Book book){ //@RequestBody 请求体参数 都是传JSON数据过来,接受JSON数据
        return iBookService.updateById(book);
    }
    //    post请求    保存一个Book对象
    @DeleteMapping("{id}")  //上面的id(这个地方不加/也可以,会自动加)值最终会赋值给Integer id中的id
    public  Boolean delete(@PathVariable Integer id){  //@PathVariable 这个注解表示形参注解,绑定路径参数与处理器方法形参减的关系;如果不写这个的话,获取不到网页传来的数据
        return iBookService.removeById(id);
    }

//    查询单个
    @GetMapping("{id}")   //此id(这个地方不加/也可以,会自动加)  最终恢会赋值给Integer id中的id
    public Book getById( @PathVariable Integer id){//@PathVariable 这个注解表示形参注解,绑定路径参数与处理器方法形参减的关系;如果不写这个的话,获取不到网页传来的数据
        System.out.println(iBookService.getById(id));
      return iBookService.getById(id);
    }

//查询分页
    @GetMapping("{currentPage}/{pageSize}")
    public IPage getPage(@PathVariable int currentPage,@PathVariable int pageSize){
        return iBookService.getPage(currentPage,pageSize);

    }

测试结果:

① 查询全部 

  public List getAll(){}

springboot中表现层标准开发--基于Restful进行开发,使用Postman测试表现层接口功能_第1张图片

②根据id查询  

public Book getById( @PathVariable Integer id){}springboot中表现层标准开发--基于Restful进行开发,使用Postman测试表现层接口功能_第2张图片

 ③保存一个数据 public  Boolean save(@RequestBody Book book){

因为是请求体传参 就选Body 然后类型选JSONspringboot中表现层标准开发--基于Restful进行开发,使用Postman测试表现层接口功能_第3张图片

 ④删除一个数据

public  Boolean delete(@PathVariable Integer id){}springboot中表现层标准开发--基于Restful进行开发,使用Postman测试表现层接口功能_第4张图片

⑤分页查询

    public IPage getPage(@PathVariable int currentPage,@PathVariable int pageSize){}

这个方法并不是技术封装的,而是我们自己封装的springboot中表现层标准开发--基于Restful进行开发,使用Postman测试表现层接口功能_第5张图片

你可能感兴趣的:(springboot,spring,boot)