0

I am using Spring boot framework with hibernate. I want to show all data from database only certain conditions. Here is my query

SELECT * FROM `client_master` WHERE CLIENT_GROUP='S' 

I want to get data which CLIENT_GROUP data has only S. I have used bellow cod for spring boot..

  1. Model I have used bellow code..

     @Entity
    @Table(name = "client_master")
    public class ClientMasterModel {
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE)
      @Column(name= "ID")
      private int ID;
    
      @Column(name= "NAME")
      private String name;
    
      //getter or setter
     }
    
  2. My repository is bellow

      public interface Staff_Add_Repository extends JpaRepository<ClientMasterModel, Long> {
    
    }
    
  3. In service, I have used bellow code..

      @Autowired
    Staff_Add_Repository add_Repository;
    
    
     public List<ClientMasterModel> findAll(){
       return add_Repository.findAll();
     }
    

    Above method returns all data. I want to get only specific data . How to do it? Please help me..

Enamul Haque
  • 4,789
  • 1
  • 37
  • 50

2 Answers2

0

Try

List<ClientMasterModel> findByClientGroup(String clientGroup); 
Bor Laze
  • 2,458
  • 12
  • 20
0

Assuming you have a field named clientGroup in your ClientMasterModel you just need a correctly named method and possibly - if you wish - a default wrapper method in your repository as following:

public interface Staff_Add_Repository
            extends JpaRepository<ClientMasterModel, Long> {

    List<ClientMasterModel> findByClientGroup(String clientGroup);

    default List<ClientMasterModel> findWhereClientGroupIsS() {
        return findByClientGroup("S");
    }

}

Also the findAllBy is a synonym to findBy. See this question

pirho
  • 11,565
  • 12
  • 43
  • 70