import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/transportation") public class TransportationController<TransportationRepository> { @Autowired private TransportationRepository transportationRepository; @PostMapping("/create") public ResponseEntity<?> createTransportation(@RequestBody TransportationDto transportationDto) { Transportation transportation = new Transportation(); transportation.setFrom(transportationDto.getFrom()); transportation.setTo(transportationDto.getTo()); transportation.setDepartureTime(transportationDto.getDepartureTime()); transportation.setArrivalTime(transportationDto.getArrivalTime()); transportationRepository.save(transportation); return ResponseEntity.ok("Transportation created successfully"); } @GetMapping("/all") public ResponseEntity<?> getAllTransportations() { List<Transportation> transportations = transportationRepository.findAll(); return ResponseEntity.ok(transportations); } @GetMapping("/{id}") public ResponseEntity<?> getTransportationById(@PathVariable Long id) { Transportation transportation = transportationRepository.findById(id).orElse(null); if (transportation == null) { return ResponseEntity.notFound().build(); } return ResponseEntity.ok(transportation); } }这段代码中transportation的所有方法报错,比如Cannot resolve method 'setFrom' in 'Transportation',请帮我修改
时间: 2024-02-13 09:01:54 浏览: 42
根据代码和错误信息看,Transportation类中没有setFrom()方法,因此需要在Transportation类中添加该方法。同样的,Transportation类中也需要添加setTo()、setDepartureTime()和setArrivalTime()方法。以下是修改后的Transportation类代码:
```
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDateTime;
@Entity
public class Transportation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String from;
private String to;
private LocalDateTime departureTime;
private LocalDateTime arrivalTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public LocalDateTime getDepartureTime() {
return departureTime;
}
public void setDepartureTime(LocalDateTime departureTime) {
this.departureTime = departureTime;
}
public LocalDateTime getArrivalTime() {
return arrivalTime;
}
public void setArrivalTime(LocalDateTime arrivalTime) {
this.arrivalTime = arrivalTime;
}
}
```
修改后的Transportation类已经包含了setFrom()、setTo()、setDepartureTime()和setArrivalTime()方法。
阅读全文