使用 Class 的 ArrayList 在 Spring 中实现 CRUD
Implement a CRUD in Spring using an ArrayList of a Class
我今天刚开始学习Spring(java框架),我想用class的ArrayList而不是数据库来实现所有CRUD方法。
我很容易地创建了 List 和 Add 方法,但是当 id 的问题出现时(对于 remove / select / update 方法),我真的很困惑...
这是我的宠物 Class :
public class Pet {
int id;
String name;
int age;
String owner;
public Pet() {}
public Pet(int id, String name, int age, String owner) {
this.id = id;
this.name = name;
this.age = age;
this.owner = owner;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
}
这是我的 arrayList 和 CRUD 方法:
private List<Pet> datas = new ArrayList<>();
@PostConstruct
private void initPetList() {
datas.add(new Pet(1, "Medor", 12, "Peter"));
datas.add(new Pet(2, "Mistigri", 5, "Jack"));
datas.add(new Pet(3, "Pepette", 8, "Sarah"));
}
@Override
public List<Pet> getPets() {
return datas;
}
@Override
public int addPet(Pet pet) throws PetAlreadyExistsException {
for (Pet _pet : datas) {
if (_pet.getId() == pet.getId()) {
throw new PetAlreadyExistsException();
}
}
datas.add(pet);
return pet.getId();
}
@Override
public Pet getPet(int petId) {
return datas.get(petId);
}
@Override
public Pet removePet(int petId) {
return datas.remove(petId);
}
@Override
public int updatePet(int petId, Pet pet) {
datas.set(petId, pet);
return pet.getId();
}
这是我的控制器:
@RequestMapping(value = "", method = RequestMethod.GET)
public ResponseEntity<List<Pet>> listPets() {
return new ResponseEntity<>(petService.getPets(), HttpStatus.OK);
}
@RequestMapping(value = "", method = RequestMethod.POST, consumes = "application/json")
public ResponseEntity<Integer> addPet(@RequestBody Pet pet) throws PetAlreadyExistsException {
return new ResponseEntity<>(petService.addPet(pet), HttpStatus.OK);
}
@RequestMapping(value = "/{petId}", method = RequestMethod.GET)
public ResponseEntity<Pet> findPetById(@PathVariable int petId) {
return new ResponseEntity<>(petService.getPet(petId), HttpStatus.OK);
}
@RequestMapping(value = "/{petId}", method = RequestMethod.PUT, consumes = "application/json")
public ResponseEntity<Integer> updatePetById(@RequestBody Pet pet) {
return new ResponseEntity<>(petService.updatePet(pet.getId(), pet), HttpStatus.OK);
}
@RequestMapping(value = "/{petId}", method = RequestMethod.DELETE)
public ResponseEntity<Pet> removePetById(@PathVariable int petId) {
return new ResponseEntity<>(petService.removePet(petId), HttpStatus.OK);
}
因此,正如您所看到的,我尝试创建我的移除方法,并捕获我的 class 的 ID。但问题是我的 ID 是我的 ArrayList 中的一行,当我创建一个名为 petId 的新 int 时,这个将 return 数组的索引所以如果在我的请求中我有:“ http://localhost:8080/pets/1 " 这将 return array[1] 其中有 id "2" !而且我不知道如何按 Id 而不是按索引进行过滤!
我需要的是当我请求 ID 号 1 时,我想要 ID 值为“1”的数组[0]。
如果您对此有什么建议。谢谢
您可以将 petid
与 Pet
的 id
进行比较。这允许从 ArrayList 中取出正确的 PPet。更改您的 crud 方法:
@Override
public Pet getPet(int petId) {
for (Pet _pet : datas)
if (_pet.getId() == petId)
return pet;
return null;
}
@Override
public Pet removePet(int petId) {
Pet pet =null;
if((pet= getPet(petId))!=null){
datas.remove(pet);
return pet;
}
return null;
}
@Override
public int updatePet(int petId, Pet pet) {
if(petId<datas.size())
datas.set(petid,pet);
return pet.getId();
}
我今天刚开始学习Spring(java框架),我想用class的ArrayList而不是数据库来实现所有CRUD方法。
我很容易地创建了 List 和 Add 方法,但是当 id 的问题出现时(对于 remove / select / update 方法),我真的很困惑...
这是我的宠物 Class :
public class Pet {
int id;
String name;
int age;
String owner;
public Pet() {}
public Pet(int id, String name, int age, String owner) {
this.id = id;
this.name = name;
this.age = age;
this.owner = owner;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
}
这是我的 arrayList 和 CRUD 方法:
private List<Pet> datas = new ArrayList<>();
@PostConstruct
private void initPetList() {
datas.add(new Pet(1, "Medor", 12, "Peter"));
datas.add(new Pet(2, "Mistigri", 5, "Jack"));
datas.add(new Pet(3, "Pepette", 8, "Sarah"));
}
@Override
public List<Pet> getPets() {
return datas;
}
@Override
public int addPet(Pet pet) throws PetAlreadyExistsException {
for (Pet _pet : datas) {
if (_pet.getId() == pet.getId()) {
throw new PetAlreadyExistsException();
}
}
datas.add(pet);
return pet.getId();
}
@Override
public Pet getPet(int petId) {
return datas.get(petId);
}
@Override
public Pet removePet(int petId) {
return datas.remove(petId);
}
@Override
public int updatePet(int petId, Pet pet) {
datas.set(petId, pet);
return pet.getId();
}
这是我的控制器:
@RequestMapping(value = "", method = RequestMethod.GET)
public ResponseEntity<List<Pet>> listPets() {
return new ResponseEntity<>(petService.getPets(), HttpStatus.OK);
}
@RequestMapping(value = "", method = RequestMethod.POST, consumes = "application/json")
public ResponseEntity<Integer> addPet(@RequestBody Pet pet) throws PetAlreadyExistsException {
return new ResponseEntity<>(petService.addPet(pet), HttpStatus.OK);
}
@RequestMapping(value = "/{petId}", method = RequestMethod.GET)
public ResponseEntity<Pet> findPetById(@PathVariable int petId) {
return new ResponseEntity<>(petService.getPet(petId), HttpStatus.OK);
}
@RequestMapping(value = "/{petId}", method = RequestMethod.PUT, consumes = "application/json")
public ResponseEntity<Integer> updatePetById(@RequestBody Pet pet) {
return new ResponseEntity<>(petService.updatePet(pet.getId(), pet), HttpStatus.OK);
}
@RequestMapping(value = "/{petId}", method = RequestMethod.DELETE)
public ResponseEntity<Pet> removePetById(@PathVariable int petId) {
return new ResponseEntity<>(petService.removePet(petId), HttpStatus.OK);
}
因此,正如您所看到的,我尝试创建我的移除方法,并捕获我的 class 的 ID。但问题是我的 ID 是我的 ArrayList 中的一行,当我创建一个名为 petId 的新 int 时,这个将 return 数组的索引所以如果在我的请求中我有:“ http://localhost:8080/pets/1 " 这将 return array[1] 其中有 id "2" !而且我不知道如何按 Id 而不是按索引进行过滤!
我需要的是当我请求 ID 号 1 时,我想要 ID 值为“1”的数组[0]。
如果您对此有什么建议。谢谢
您可以将 petid
与 Pet
的 id
进行比较。这允许从 ArrayList 中取出正确的 PPet。更改您的 crud 方法:
@Override
public Pet getPet(int petId) {
for (Pet _pet : datas)
if (_pet.getId() == petId)
return pet;
return null;
}
@Override
public Pet removePet(int petId) {
Pet pet =null;
if((pet= getPet(petId))!=null){
datas.remove(pet);
return pet;
}
return null;
}
@Override
public int updatePet(int petId, Pet pet) {
if(petId<datas.size())
datas.set(petid,pet);
return pet.getId();
}