定义DTO对象
此处使用lombok,需要在pom.xml中引用
@Data
public class BookDTO{
private int id;
private String name;
## 重点在构造函数,需要让Query中实例化是有构造函数可以调用
public BookDTO(int id,String name){
this.id = id;
this.name = name;
}
}
定义@Query
query 需要使用jql查询语言,不能使用NativeQuery
@Query("select new BookDTO(id,name) from Book")
List<BookDTO> findBooks();
使用的包版本
spring-boot: 2.1.10.RELEASE
spring-boot-starter-jpa