JPA自定义返回对象
方法一:使用@Query
@Query(value = "select new com.test.demo.entity.SearchResult(c.phone,r.name,sum(c.money)
as sum) from Costs c, Rates r where c.phone =?1 GROUP BY r.name",nativeQuery=true)
方法二:使用QueryDSL实现
/**
* QueryDSL配置类
*/
@Configuration
public class QuerydslConfig {
@Autowired
@PersistenceContext
private EntityManager entityManager;
@Bean
public JPAQueryFactory queryFactory(){
return new JPAQueryFactory(entityManager);
}
}
public class Test {
private String name;
private Integer age;
public Test (String name, Integer age) {
this.name= name;
this.age= age;
}
//set get省略
}
@Autowired
private JPAQueryFactory jpaQueryFactory;
@Override
public List<Test> ListByTestByQuery()