在 Spring Boot REST 中使用自定义实体自定义派生方法
我想使用自定义派生方法从自定义存储库访问列表。 我分享了代码并进行了如下解释:
UserWalletHistory.java - 实体
package com.ayvids.api.model.user;
import java.time.LocalDateTime;
import javax.persistence.*;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Entity(name = "users_wallet_history")
public class UserWalletHistory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, columnDefinition = "int (11)", name = "rowid")
private int rowid;
@ManyToOne
@JoinColumn(nullable = false, columnDefinition = "int (11)", name = "userid")
private User userId;
@Column(nullable = false, columnDefinition = "varchar (255)", name = "remarks")
private String remarks;
@Column(nullable = false, columnDefinition = "float(10,2) default '0.00' ", name = "credit")
private float credit;
@Column(nullable = false, columnDefinition = "float(10,2) default '0.00' ", name = "debit")
private float debit;
@Column(nullable = false, columnDefinition = "float(10,2) default '0.00' ", name = "balance")
private float balance;
@Column(nullable = false, columnDefinition = "datetime default current_timestamp", name = "datetime")
private LocalDateTime datetime;
}
UserWalletHistoryRepo.java - 自定义存储库
public interface UserWalletHistoryRepo extends CrudRepository<UserWalletHistory, Integer> {
public List<UserWalletHistory> findByUserId(int userId);
}
UserWalletHistoryService.java - 服务层
public List<UserWalletHistory> getByUserId(int userId) {
return repo.findByUserId(userId);
}
UserWalletHistoryController.java - 控制器
@GetMapping("/get/{id}")
public ResponseEntity<Object> getByUserId(@PathVariable int id) {
List<UserWalletHistory> list = service.getByUserId(id);
if (list.size() > 0) {
return ResponseHandler.generateResponse(HttpStatus.OK, list);
} else {
return ResponseHandler.generateResponse(HttpStatus.NO_CONTENT);
}
}
点击 URL 时收到的错误
java.lang.IllegalArgumentException:参数值 [1] 执行了与预期类型不匹配 [com.ayvids.api.model.user.User (n/a)]
提前致谢!
I want to access the list from the Custom Repository using Custom Derived methdods.
I have shared the code with explaination below:
UserWalletHistory.java - Entity
package com.ayvids.api.model.user;
import java.time.LocalDateTime;
import javax.persistence.*;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Entity(name = "users_wallet_history")
public class UserWalletHistory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, columnDefinition = "int (11)", name = "rowid")
private int rowid;
@ManyToOne
@JoinColumn(nullable = false, columnDefinition = "int (11)", name = "userid")
private User userId;
@Column(nullable = false, columnDefinition = "varchar (255)", name = "remarks")
private String remarks;
@Column(nullable = false, columnDefinition = "float(10,2) default '0.00' ", name = "credit")
private float credit;
@Column(nullable = false, columnDefinition = "float(10,2) default '0.00' ", name = "debit")
private float debit;
@Column(nullable = false, columnDefinition = "float(10,2) default '0.00' ", name = "balance")
private float balance;
@Column(nullable = false, columnDefinition = "datetime default current_timestamp", name = "datetime")
private LocalDateTime datetime;
}
UserWalletHistoryRepo.java - Custom Repository
public interface UserWalletHistoryRepo extends CrudRepository<UserWalletHistory, Integer> {
public List<UserWalletHistory> findByUserId(int userId);
}
UserWalletHistoryService.java - Service Layer
public List<UserWalletHistory> getByUserId(int userId) {
return repo.findByUserId(userId);
}
UserWalletHistoryController.java - Controller
@GetMapping("/get/{id}")
public ResponseEntity<Object> getByUserId(@PathVariable int id) {
List<UserWalletHistory> list = service.getByUserId(id);
if (list.size() > 0) {
return ResponseHandler.generateResponse(HttpStatus.OK, list);
} else {
return ResponseHandler.generateResponse(HttpStatus.NO_CONTENT);
}
}
The Error I receive when hitting the URL
java.lang.IllegalArgumentException: Parameter value [1] did not match expected type [com.ayvids.api.model.user.User (n/a)]
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论