【发布时间】:2020-07-17 23:49:31
【问题描述】:
我是 Spring Boot jpa 的新手。我想将返回类型设为 List,但我得到的只是 List
我的实体类
@Component
@Entity
@Table(name = "USERDB.USERS")
public class User() {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MY_SEQ")
@SequenceGenerator(sequenceName = "MY_SEQ_NAME", allocationSize = 1), name = "MY_SEQ")
@Column(name = "userId")
private long id;
@Column(name = "firstName")
private String fName;
@Column(name = "midName")
private String mName;
@Column(name = "lastName")
private String lName;
@Column(name = "email")
private String email;
@Column(name = "createdDate")
private Timestamp createdOn;
public User() {
this.createdOn = new Timestamp(System.currentTimeMillis()
}
//SETTERS & GETTERS
}
我的仓库;
public interface UserRepository extends JpaRepository<User, String> {
@Query("SELECT id fName, lastName, email FROM User u WHERE u.fName=(:fName)")
public List<User> findByEmail(@Param("fName") String fName);
}
我想要的只是得到一个 json 响应作为用户数组,其键值对如下所示
[
[
"id": 1001,
"fName": John",
"lName": "Doe",
"email": "johnd@example.com"
],
[
"id": 1002,
"fName": "John",
"lName": "Simmons",
"email": "johns@example.com"
],
]
但我得到了一个列表,其中只有以下值。
[
[
1001,
"John",
"Doe",
"johnd@example.com"
],
[
1002,
"John",
"Simmons",
"johns@example.com"
],
]
我不确定我在哪里做错了,或者这就是我应该得到的?这是我实际程序的一个假设示例。如有错误请见谅。
这是我的控制器类
@Restcontroller
public class UserController {
@Autowired
UserRepository repo;
@GetMapping("/user/{fname}")
public List<User> getUserByName(
@PathVariable("fname") String fname) {
return repo.findByEmail(fname);
}
}
【问题讨论】:
标签: spring spring-boot spring-data-jpa