在 Spring Boot REST 中使用自定义实体自定义派生方法

发布于 2025-01-11 01:59:22 字数 2244 浏览 0 评论 0原文

我想使用自定义派生方法从自定义存储库访问列表。 我分享了代码并进行了如下解释:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文