您的Interface方法可以使用本机SQL从多个表中选择列,并且该方法将返回对象数组的列表:
public interface MyRepository extends JpaRepository { @Query(name = [name], nativeQuery = true) List<Object[]> methodThatQueriesMultipleTables();}
列表中的每个项目都是对象数组,它是一行数据
您还可以创建自定义存储库实现:
如何向Spring Data JPA添加自定义方法
@NoRepositoryBeanpublic interface CustomRepository<[Your object]> { List<Object[]> methodThatQueriesMultipleTables();}public class MyRepositoryImpl<[Your object]> implements CustomRepository<[Your object] { @PersistenceContext private EntityManager entityManager; @Override public List<Object[]> methodThatQueriesMultipleTables() { //use JPA query to select columns from different tables Query nativeQuery = entityManager.createNativeQuery("query"); return query.getResultList(); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)