From the docs
To enrich a repository with custom functionality you first define an interface and an implementation for the custom functionality. Use the repository interface you provided to extend the custom interface.
Define an interface like so
public interface StudentRepositoryCustom {
List<String> nameByCourse(String coursename);
}
Then define a custom implementation of this interface like so
@Service
class StudentRepositoryImpl implements StudentRepositoryCustom {
@PersistenceContext
private EntityManager em;
public List<String> nameByCourse(String coursename) {
CriteriaBuilder cb = em.getCriteriaBuilder();
//Using criteria builder you can build your criteria queries.
}
}
Now you can extend this custom repository implementaion in your JPA repository like so.
public interface StudentRepository extends CrudRepository<StudentEntity, Integer>, StudentRepositoryCustom {
}
Learn more about criteria query and criteria builder here
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…