spring boot客户控制器
示例
package org.bookmytickets.controller;
import java.util.List;
import org.bookmytickets.model.Customer;
import org.bookmytickets.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value = "/customer")
public class CustomerController {
@Autowired
private CustomerRepository repository;
@GetMapping("")
public List<Customer> selectAll(){
List<Customer> customerList = repository.findAll();
return customerList;
}
@GetMapping("/{id}")
public List<Customer> getSpecificCustomer(@PathVariable String id){
return repository.findById(id);
}
@GetMapping("/search/lastName/{lastName}")
public List<Customer> searchByLastName(@PathVariable String lastName){
return repository.findByLasttName(lastName);
}
@GetMapping("/search/firstname/{firstname}")
public List<Customer> searchByFirstName(@PathVariable String firstName){
return repository.findByFirstName(firstName);
}
@PostMapping("")
public void insert(@RequestBody Customer customer) {
repository.save(customer);
}
@PatchMapping("/{id}")
public void update(@RequestParam String id, @RequestBody Customer customer) {
Customer oldCustomer = repository.finedById(id);
if(customer.getFirstName() != null) {
oldCustomer.setFristName(customer.getFirstName());
}
if(customer.getLastName() != null) {
oldCustomer.setLastName(customer.getLastName());
}
repository.save(oldCustomer);
}
@DeleteMapping("/{id}")
public void delete(@RequestParam String id) {
Customer deleteCustomer = repository.findById(id);
repository.delete(deleteCustomer);
}
}
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短