bkumar
bkumar

Reputation: 51

Rest api via spring

I have to get all data from MySQL in a table using condition select query where fields is 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 access manager

public List<FoodcaseDO> getAllFoodCourt() {
    List<FoodcaseEntity> foodCaseList = foodcourtRepository.findByIsdeleted(false);
}

Upvotes: 3

Views: 90

Answers (3)

Darshan Mehta
Darshan Mehta

Reputation: 30849

You need to add another condition as well, for location, e.g.:

public List<FoodcaseEntity> findByIsdeletedAndLocation(boolean deleted, int location);

And call it with false and 1 as arguments, e.g.:

List<FoodcaseEntity> foodCaseList = foodcourtRepository.findByIsdeletedAndLocation(false, 1);

This should give you the required result.

Updte

If you want to fetch the data for multiple locations then you need to write a method that supports IN, e.g.:

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));

Upvotes: 4

Devram Kandhare
Devram Kandhare

Reputation: 781

JPA provides @Query to write custom query. You can define another interface called FoodCourtRepositoryCustom and write custom query as 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 extends this interface in your repository inteface as below:

public interface FoodCourtRepository  extends JpaRepository<FoodCourtEntity, Long>, FoodCourtRepositoryCustom{
List<FoodcaseEntity> findByIsdeleted(Boolean isDeleted);
}

Now method is available in your access manager.

public List<FoodcaseDO> getAllFoodCourt() {
List<FoodcaseEntity> foodCaseList = foodcourtRepository.findByIsdeleted(false);
List<FoodcaseEntity> foodCaseList = foodcourtRepository.findByIsdeletedAndLocation(false, 1);
}

Upvotes: 1

Balasubramanian
Balasubramanian

Reputation: 728

In a repository class add,

List<FoodcaseEntity> foodCaseList = foodcourtRepository.findByIsdeletedAndLocation(boolean isSelected,int location);

then u can send selected and location values.it will return list of values based on condition.

Upvotes: 0

Related Questions