Spring Data Jpa 查询返回自定义对象

时间:2025-03-30 22:32:49

转载请注明出处:http://www.wangyongkui.com/java-jpa-query。

今天使用Jpa遇到一个问题,发现查询多个字段时返回对象不能自动转换成自定义对象。代码如下:

//User 实体对象
@Entity
@Table(name="t_user")
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Strng getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.project.user.dto
//查询返回对象
public class UserDto{
private Integer count;
private String name;
public UserDto(Integer count, String name) {
this.count = count;
this.name = name;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Strng getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

好了,这回查询直接返回的就是UserDto对象了。