1. 通过ById等方法查询出来并进行设值,最后进行保存更新操作
- 查:通过Repository对象把实体根据ID查询出来
- 改:往查出来的实体对象进行set各个字段
- 存:通过Repository接口的save方法进行保存
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class FruitServiceImpl implements FruitService {
@Autowired
private FruitRepository fruitRepository;
@Override
public void edit(Fruit fruit) {
// 把对象查出来,这个对象就有id主键
Fruit newFruit = fruitRepository.getById(fruit.getId());
// edit字段值
newFruit.setColor(fruit.getColor());
newFruit.setName(fruit.getName());
// 保存
fruitRepository.save(newFruit);
}
}