import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; @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); } }这段代码中所有的transportationRepository的方法都报错,比如Cannot resolve method 'save' in 'TransportationRepository',请帮我修改
时间: 2024-02-13 14:02:27 浏览: 165
org.springframework.test-3.0.2.RELEASE.jar
根据您提供的代码,TransportationController类使用了泛型TransportationRepository,但是没有定义TransportationRepository接口或类,导致编译器无法识别相应的方法。您需要创建一个TransportationRepository接口,并定义相关的CRUD方法,类似于下面这样:
```
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface TransportationRepository extends JpaRepository<Transportation, Long> {
}
```
其中,Transportation是您的实体类,Long是实体类的ID类型。这里使用了Spring Data JPA提供的JpaRepository接口,它提供了一组标准的CRUD方法,包括save、findAll、findById等。
另外,TransportationController类中的泛型定义应该去掉,修改为:
```
@RestController
@RequestMapping("/transportation")
public class TransportationController {
@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);
}
}
```
这样,就可以正确地调用transportationRepository的方法了。
阅读全文