【问题标题】:Implement a CRUD in Spring using an ArrayList of a Class使用类的 ArrayList 在 Spring 中实现 CRUD
【发布时间】:2017-06-19 21:47:05
【问题描述】:

我今天刚开始学习 Spring(java 框架),我想通过使用类的 ArrayList 而不是使用数据库来实现所有 CRUD 方法。

我很轻松地创建了 List 和 Add 方法,但是当 id 的问题出现时(对于 remove / select / update 方法),我真的很困惑......

这是我的宠物班:

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);
}    

如您所见,我尝试创建例如我的 remove 方法并捕获我的班级的 id。但问题是我的 Id 是我的 ArrayList 中的一行,当我创建一个名为 petId 的新 int 时,这个将返回数组的索引,所以如果在我的请求中我有:“http://localhost:8080/pets/1”这将返回数组 [ 1] 的 id 为 "2" !而且我不知道如何按 Id 而不是按索引过滤!

我需要的是,当我请求 id 编号 1 时,我想要 ID 值为“1”的数组 [0]。

如果您对此有任何建议。谢谢

【问题讨论】:

  • 不要使用List&lt;Pet&gt;。使用Map&lt;Integer, Pet&gt;,以宠物 ID 为键。这样get-by-id 和remove-by-id 很快。您可以使用 Map 的values() 方法获取宠物列表。
  • 一种方法是,如果给定的 id 与对象的 id 匹配,您可以迭代并删除该对象。

标签: java arrays spring arraylist crud


【解决方案1】:

您可以将提供的petididPet 进行比较。这允许从 ArrayList 中取出正确的 PPe。改变你的 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();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多