学习完毕
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
package com.fjy.springboot.controller;
|
||||
|
||||
import com.fjy.springboot.domain.Result;
|
||||
import com.fjy.springboot.repository.PersonRepository;
|
||||
import com.fjy.springboot.domain.person;
|
||||
import com.fjy.springboot.service.PersonService;
|
||||
import com.fjy.springboot.untils.ResultUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
public class PersonController {
|
||||
|
||||
@Autowired
|
||||
private PersonRepository personRepository;
|
||||
@Autowired
|
||||
private PersonService personService;
|
||||
|
||||
@GetMapping(value = "/persons")
|
||||
public List<person> personList(){
|
||||
return personRepository.findAll();//返回所有数据
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加用户
|
||||
* @return
|
||||
*/
|
||||
@PostMapping(value = "/persons")
|
||||
public Result<person> personAdd(@Valid person per, BindingResult bindingResult){//数据校验
|
||||
if (bindingResult.hasErrors()){
|
||||
return ResultUtil.error(1,bindingResult.getFieldError().getDefaultMessage());
|
||||
}
|
||||
per.setAge(per.getAge());
|
||||
per.setName(per.getName());
|
||||
per.setContent(per.getContent());
|
||||
|
||||
return ResultUtil.success(personRepository.save(per));
|
||||
}
|
||||
//查询一个人
|
||||
@GetMapping(value = "/persons/{id}")
|
||||
public Object personFindOne(@PathVariable("id") Integer id){
|
||||
Optional per = personRepository.findById(id);
|
||||
return per.get();
|
||||
}
|
||||
//更新
|
||||
@PutMapping(value = "/persons/{id}")
|
||||
public person personUpdate(@PathVariable("id") Integer id, @RequestParam("name") String name,@RequestParam("age") Integer age,@RequestParam("content") String content){
|
||||
person per = new person();
|
||||
per.setId(id);
|
||||
per.setAge(age);
|
||||
per.setName(name);
|
||||
per.setContent(content);
|
||||
return personRepository.save(per);
|
||||
}
|
||||
//删除
|
||||
@DeleteMapping(value = "/persons/{id}")
|
||||
public void personDelete(@PathVariable("id") Integer id){
|
||||
personRepository.deleteById(id);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/persons/age/{id}")
|
||||
public void getAge(@PathVariable("id") Integer id)throws Exception{
|
||||
personService.getAge(id);
|
||||
}
|
||||
@GetMapping("/test")
|
||||
public String testURL(){
|
||||
return "hello spring boot";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user