Rest api via spring
I need to get all data from MySQL
in a table using a condition query select
where fields isdeleted = 0, location = 1. How could I implement this in repository and access manager.
public interface FoodCourtRepository extends JpaRepository<FoodCourtEntity, Long> {
List<FoodcaseEntity> findByIsdeleted(Boolean isDeleted);
}
In the access manager
public List<FoodcaseDO> getAllFoodCourt() {
List<FoodcaseEntity> foodCaseList = foodcourtRepository.findByIsdeleted(false);
}
source to share
JPA provides @Query for writing custom query. You can define another interface called FoodCourtRepositoryCustom and write your own request as shown below:
public interface FoodCourtRepositoryCustom {
@Query("SELECT fe FROM FoodcaseEntity fe WHERE fe.isdeleted=?1 AND fe.location=?2 ")
List<FoodcaseEntity> findByIsdeletedAndLocation(Boolean isDeleted, Integer location);
}
Then it extends this interface in your repository interface as shown below:
public interface FoodCourtRepository extends JpaRepository<FoodCourtEntity, Long>, FoodCourtRepositoryCustom{
List<FoodcaseEntity> findByIsdeleted(Boolean isDeleted);
}
This method is now available in your access manager.
public List<FoodcaseDO> getAllFoodCourt() {
List<FoodcaseEntity> foodCaseList = foodcourtRepository.findByIsdeleted(false);
List<FoodcaseEntity> foodCaseList = foodcourtRepository.findByIsdeletedAndLocation(false, 1);
}
source to share
You need to add one more condition for the location, for example:
public List<FoodcaseEntity> findByIsdeletedAndLocation(boolean deleted, int location);
And call it with false
and 1
as arguments, for example:
List<FoodcaseEntity> foodCaseList = foodcourtRepository.findByIsdeletedAndLocation(false, 1);
This will give you the desired result.
Updte
If you want to get data for multiple locations, you need to write a method that supports IN
, for example:
public List<FoodcaseEntity> findByIsdeletedAndLocationIn(boolean deleted, List<Integer> location);
And then call it like this:
List<FoodcaseEntity> foodCaseList = foodcourtRepository.findByIsdeletedAndLocation(false, Arrays.asList(2,3,4,5));
source to share