【发布时间】:2021-09-15 19:47:02
【问题描述】:
我正在尝试在 Spring Boot 中实现删除查询,但是参数是可选的。我如何编写相同的 JPA 查询。 以下是我为任务请求参数实施的方式:
@Transactional
@Repository
public interface ABCRepo extends CrudRepository<ABC, Long>{
public List<ABC> findByABCIdAndStartYrAndStartMonth(String pilotId, int startYr, int startMonth);
public long deleteABCByABCId(String pilotId);
}
控制器类
@RequestMapping(value="", method= RequestMethod.DELETE)
public Response delete(@PathVariable("abc-id")String pilotId)
{
LOGGER.info("Trying to delete pilot bank using abc id : "+ abcId);
long deletedRecords=abcBiz.deleteABCByABCId(abcId);
if(deletedRecords==0)
{
throw new PilotNotFoundException("Entity not found "+abcId);
}
return Response.status(Response.Status.NO_CONTENT).entity(deletedRecords).build();
}
添加可选参数后我的新 Controller.class
@RequestMapping(value="", method= RequestMethod.DELETE)
public Response delete(@PathVariable("abc-id")String abcId, @RequestParam(name = "bid-yr", required = false)
int bidYr, @RequestParam(name = "bid-month", required = false) int bidMonth)
{
LOGGER.info("Trying to delete pilot bank using abc id : "+ abcId);
long deletedRecords=abcBiz.deleteABCByABCId(a);bcId
if(deletedRecords==0)
{
throw new PilotNotFoundException("Entity not found "+abcId);
}
return Response.status(Response.Status.NO_CONTENT).entity(deletedRecords).build();
}
我如何在 JPA 处理这个问题?
【问题讨论】:
标签: java spring-boot jpa