Ang*_*gel 3 java interface spring-mvc spring-data-jpa spring-boot
我正在关注使用SpringBoot 1.3的教程。我正在使用SpringBoot 2.0的最新版本。我正在创建REST API。我需要帮助使1.3代码适应2.0规范,save()并且delete()现在期望对象超过long ids。我该如何重写接受接口long id' and 'ArrayList?
现在的代码如下:
@Entity
public class HotelBooking {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id; // could be Long
private String hotelName;
private double pricePerNight;
private int nbOfNights;
public HotelBooking(){}
public HotelBooking(String hotelName, double pricePerNight, int nbOfNights) {
this.hotelName = hotelName;
this.pricePerNight = pricePerNight;
this.nbOfNights = nbOfNights;
}
// getters omitted
}
Run Code Online (Sandbox Code Playgroud)
@Repository
public interface BookingRepository extends JpaRepository<HotelBooking, Long> {
data/jpa/docs/2.1.0.M1/reference/html/
List<HotelBooking> findByPricePerNightLessThan(double price);
}
Run Code Online (Sandbox Code Playgroud)
long id删除database@RequestMapping(value = "/delete/id")
public List<HotelBooking> remove(@PathVariable long id) {
bookingRepository.delete(id);
return bookingRepository.findAll();
}
Run Code Online (Sandbox Code Playgroud)
database @Override
public void run(String... strings) throws Exception {
List<HotelBooking> bookings = new ArrayList<>();
bookings.add(new HotelBooking("Marrior", 200.50, 3));
bookings.add(new HotelBooking("Ibis", 90, 4));
bookings.add(new HotelBooking("Novotel", 140.74, 1));
bookingRepository.save(bookings);
}
Run Code Online (Sandbox Code Playgroud)
以上内容在SpringBoot 2.0中不再起作用。尝试通过ID删除时,我收到一条错误消息- Error:(61, 34) java: incompatible types: long cannot be converted to romaniancoder.booking.HotelBooking
现在期望使用对象而不是long id数据库中的对象。
当试图保存一个ArrayList我得到这个错误 - Error:(40, 26) java: method save in interface org.springframework.data.repository.CrudRepository<T,ID> cannot be applied to given types;
required: S
found: java.util.List<romaniancoder.booking.HotelBooking>
reason: inferred type does not conform to upper bound(s)
inferred: java.util.List<romaniancoder.booking.HotelBooking>
upper bound(s): romaniancoder.booking.HotelBooking
这是因为如果它需要一个类扩展或什么工作?我不确定...
阅读Spring Docs之后,我不明白为什么会更改,以及如何编写代码以接受并long id在中找到内容database。我不明白如何适应save()接受ArrayLists。
| 归档时间: |
|
| 查看次数: |
5356 次 |
| 最近记录: |