Search code examples
javaspringboot

what I want is when I press the initial letter of each name to show me the name with its initial letter


I make a simple database of names. There are a couple of names:(for example) Alexander , Anthony , Amelia , Asher and Matin. And I want to get all names that contains letter "a" but only these which have first letter "a" not Matin because a is the second letter in the word . I have in my repository this method:

@Query(value = "select ID, F_NAME, L_NAME FROM TEACHERS WHERE F_NAME LIKE  %:firstName% ",nativeQuery = true )
List<Teacher>findByFirstname( @Param("firstName") String firstName);

I created an application with a list of professors with spring and when I click on the first letter of the name, the list shows all the names that contain this letter ,

what I want is when I press the initial letter of each name to show me the name with its initial letter


Solution

  • I think in your case you should delete the first % and make your code like this:

    @Query(value = "SELECT ID, F_NAME, L_NAME FROM TEACHERS WHERE F_NAME LIKE :firstName%", nativeQuery = true)
    List<Teacher> findByFirstname(@Param("firstName") String firstName);
    

    Then, you can call the method and pass in the letter you want to search for as a parameter, like this:

    List<Teacher> teachers = findByFirstname("a");
    

    This will return a list of all teachers whose first names start with the letter "a".