Query creation in Spring Data - dynamic where clause

前端 未结 3 1517
没有蜡笔的小新
没有蜡笔的小新 2021-01-06 03:47

Is there a way in Spring data to dynamically form the where clause?

What I want to do is have a method (which is like the findBy / get method) which runs a WHERE and

3条回答
  •  误落风尘
    2021-01-06 04:10

    Another solution: You can extend your JPA repo interface using custom fragment interfaces.

    1. Define your custom methods on a new interface

      public interface PersonFragRepository {
          List findPersonByWhatever(
              String firstName, String lastName, String age, String gender);
      }
      
    2. Provide the implementation

      public class PersonFragRepositoryImpl implements PersonFragRepository {
          @PersistenceContext
          private EntityManager entityManager;
      
          @Override
          List findPersonByWhatever(
              String firstName, String lastName, String age, String gender) {
             ...
          }
      }
      
    3. Extends your JPA interface

      public interface PersonRepository
          extends JpaRepository, PersonFragRepository
      

提交回复
热议问题