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

restful api

程序员文章站 2024-03-25 19:04:52
...
//----------------------------restful api设计----------------------------

    @Autowired
    private GirlRepository repository;

    /**
     * 获取列表
     *
     * @return
     */
    @GetMapping(value = "/girls")
    public List<Girl> query() {
        System.out.println("0987654321");
        return repository.findAll();
    }

    /**
     * 新增
     *
     * @param cupSize
     * @param age
     * @return
     */
    @PostMapping(value = "/girls")
    public Girl save(@RequestParam(value = "cupSize", required = false) String cupSize,
                     @RequestParam(value = "age", required = true) Integer age) {
        Girl girl = new Girl();
        girl.setAge(age);
        girl.setCupSize(cupSize);
        return repository.save(girl);
    }

    /**
     * 单项查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/girls/{id}")
    public Girl queryOne(@PathVariable("id") Integer id) {
        return repository.findOne(id);
    }

    /**
     * 单项修改
     * @param id
     * @param cupSize
     * @param age
     * @return
     */
    @PutMapping(value = "/girls/{id}")
    public Girl updateOne(@PathVariable("id") Integer id,
                          @RequestParam(value = "cupSize", required = false) String cupSize,
                          @RequestParam(value = "age", required = true) Integer age) {
        Girl girl = new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);
        return repository.save(girl);
    }

    /**
     * 单项删除
     * @param id
     */
    @DeleteMapping(value = "/girls/{id}")
    public void deleteOne(@PathVariable("id") Integer id){
        repository.delete(id);
    }

    /**
     * 通过年龄查询
     * @param age
     * @return
     */
    @GetMapping(value = "/girls/age/{age}")
    public List<Girl> findByAge(@PathVariable("age") Integer age){
        return repository.findByAge(age);
    }
package com.example.demo.Repository;

import com.example.demo.domain.Girl;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

/**
 * @author wuxk07
 */
public interface GirlRepository extends JpaRepository<Girl, Integer> {

    public List<Girl> findByAge(Integer age);

}