我試圖在Spring Boot中實作洗掉查詢,但是引數是可選的。我怎樣才能寫出相同的JPA查詢。 以下是我如何實作任務請求引數的:
@Transactional
@Repository
public interface ABCRepo extends CrudRepository<ABC, Long>{
public List<ABC> findByABCIdAndStartYrAndStartMonth(String pilotId, int startYr, int startMonth) /span>。
public long deleteABCByABCId(String pilotId);
}
Controller.class
@RequestMapping(value="", method= RequestMethod.DELETE)
public Response delete(@PathVariable("abc-id")String pilotId)
{
LOGGER.info("Trying to delete pilot bank using abc id : "/span> abcId)。
long deletedRecords=abcBiz.deleteABCByABCId(abcId)。
if(deleteRecords==0)
{
throw new PilotNotFoundException("Entity not found" abcId)。
}
return Response.status(Response.Status.NO_CONTENT).entity(deleteRecords).building()。
}
添加了可選引數后,我的新Controller.class
public Response delete(String abcId。
int bidYr, int bidMonth)
{
LOGGER.info("Trying to delete pilot bank using abc id : "/span> abcId)。
long deletedRecords=abcBiz.deleteABCByABCId(a); bcId
if(deleteRecords==0)
{
throw new PilotNotFoundException("Entity not found" abcId)。
}
return Response.status(Response.Status.NO_CONTENT).entity(deleteRecords).building()。
}
我如何在JPA中處理這個問題?
uj5u.com熱心網友回復:
對于可選引數,你需要撰寫查詢。像下面這樣:
@Modifying。
@Query("DELETE FROM ABC WHERE abcId=: pilotId AND (:otherOptionalParam IS NULL OR otherField=:otherOptionalParam)")
public long deleteABCByABCId(String pilotId, String otherOptionalParam)。
如果你想創建一個復雜的查詢,有很多可選引數,那么你可以創建自定義資源庫,并開發本地查詢。在這里我已經回答了我們如何在Spring資料JPA中創建自定義資源庫 - https://stackoverflow.com/a/68721142/3709922
uj5u.com熱心網友回復:
在Jignesh所說的基礎上,不要忘記用Param注解來標記你的引數。另外,jpa修改將回傳int/Integer,但不是long,所以我也不得不改變回傳型別。
@Modifying
@Query("DELETE FROM ABC WHERE abcId=: pilotId AND (:otherOptionalParam IS NULL OR
otherField=:otherOptionalParam)")
public long deleteABCByABCId(@Param(" pilotId")String pilotId。@Param("otherOptionalParam")String
otherOptionalParam)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/308793.html
標籤:
上一篇:在springboot中Jetty自定義格式的日志模式
下一篇:在一個串列上多次映射的時間復雜性
