【发布时间】:2019-10-15 13:16:50
【问题描述】:
我正在尝试为需要 2 种方法的购买构建简单的 REST。第一种方法应显示按日期排序的所有购买。第二个删除指定日期的所有购买我做了一个方法来添加和获取所有购买。现在我被困住了。
@Entity
@Table (name="purchase")
public class Purchase {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@CreationTimestamp
@Temporal(TemporalType.DATE)
@Column(name="createat")
private Date created;
@Column(name="price")
private BigDecimal price;
@Column(name="currency")
private String currency;
@Repository
public interface PurchaseRepository extends JpaRepository<Purchase, Long>
{
}
@Service
public class PurchaseService {
@Autowired
private PurchaseRepository purchaseRepository;
public void addPurchase(Purchase purchase) {
purchaseRepository.save(purchase);
}
public List<Purchase> getAllPurchase() {
List<Purchase> purchase = new ArrayList<>();
purchaseRepository.findAll().forEach(purchase::add);
return purchase;
}
}
@RestController
public class PurchaseController {
@Autowired
private PurchaseService purchaseService;
@PostMapping("/purchase")
public void addPurchase(@RequestBody Purchase purchase) {
purchaseService.addPurchase(purchase);
}
@RequestMapping("/purchase")
public List<Purchase> getAllTopics() {
return purchaseService.getAllPurchase();
}
}
我需要什么: 1.按日期排序我的列表的方法 2. 删除指定日期所有购买的方法
【问题讨论】:
标签: java hibernate spring-boot crud