【问题标题】:How to update a table column in Spring Boot?如何在 Spring Boot 中更新表列?
【发布时间】:2022-01-06 15:34:27
【问题描述】:

我正在尝试使用 @PostMapping 更新 Mysql 中的列。

实体表有5列

customer_id, customer_name, total_amount, amount_paid, amount_debt

我们为customer_name, total_amount, amount_paid 提供参数。

amount_debt 将计算为 amount_total - amount_paid 并写回每个 POSTamount_debt 列。

等价的SQL好像是

update emi_details 
set amount_debt = amount_total - amount_paid;

我试过了,使用

getAmount_debt() { return amount_total - amount_paid; }
setAmount_debt(Integer amount_debt) { this.amount_debt = amount_total - amount_debt; }

实体类中的方法。

但是,在POST 请求中,我似乎需要提供"amount_debt": 0 参数,否则它不会被写入MySql。

当我尝试删除 getset 方法并在构造函数中执行 minus,然后在没有 "amount_debt": 0 的情况下执行 POST 请求时,它显示 amount_debt 具有响应值,但没有amount_debt 列中的值。

看来,我之前可能需要在@PostMapping里面写一些@Query

return emiDetailsRepository.save(emiDetails);

但我不确定。

请指导

我尝试的事情如下:

  1. 实体 - EmiDetails.java
@Entity
@Table(name = "emi_details")
public class EmiDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer customer_id;
    
    @Column(name = "customer_name")
    private String customer_name;
    
    @Column(name = "amount_total")
    private Integer amount_total;
    
    @Column(name = "amount_paid")
    private Integer amount_paid;
    
    @Column(name = "amount_debt")
    private Integer amount_debt;;

    public EmiDetails() {}
    
    public EmiDetails(String customer_name, Integer amount_total, Integer amount_paid, Integer amount_debt) {
        super();
        this.customer_name = customer_name;
        this.amount_total = amount_total;
        this.amount_paid = amount_paid;
        this.amount_debt = amount_total - amount_paid;
    }

    public Integer getCustomer_id() {
        return customer_id;
    }

    public void setCustomer_id(Integer customer_id) {
        this.customer_id = customer_id;
    }

    public String getCustomer_name() {
        return customer_name;
    }

    public void setCustomer_name(String customer_name) {
        this.customer_name = customer_name;
    }

    public Integer getAmount_total() {
        return amount_total;
    }

    public void setAmount_total(Integer amount_total) {
        this.amount_total = amount_total;
    }

    public Integer getAmount_paid() {
        return amount_paid;
    }

    public void setAmount_paid(Integer amount_paid) {
        this.amount_paid = amount_paid;
    }

    public Integer getAmount_debt() {
        return amount_total - amount_paid;
    }

    public void setAmount_debt(Integer amount_debt) {
        this.amount_debt = amount_total - amount_debt;
    }   
}
  1. 存储库EmiDetailsRepository.java
public interface EmiDetailsRepository extends JpaRepository<EmiDetails, Integer> {}
  1. 控制器 - EmiDetailsContoller.java
@RestController
@RequestMapping("/api/v1")
public class EmiDetailsContoller {
    @Autowired
    private EmiDetailsRepository emiDetailsRepository;
    
    //get all data
    @GetMapping("/emi_details")
    public List<EmiDetails> getAllEmiDetails() {
        return emiDetailsRepository.findAll();
    }
    
    //post data
    @PostMapping("/emi_details")
    public EmiDetails createEmiRecord(@RequestBody EmiDetails emiDetails) {
        return emiDetailsRepository.save(emiDetails);
    }
}
  1. POST 请求 - 在每一列中写入数据
{
    "customer_name": "Sandeep Roy",
    "amount_total": 75,
    "amount_paid": 8,
    "amount_debt": 0
}
  1. POST 请求 - 写入数据但在 amount_debt 列中
{
  "customer_name": "Sandeep Roy",
  "amount_total": 75,
  "amount_paid": 8,
}

很抱歉写了关于问题本身的所有内容,还没有尝试过 Heroku。

【问题讨论】:

    标签: mysql spring-boot


    【解决方案1】:

    简答

    计算createEmiRecord 方法中的债务金额。

    只需将您的 @PostMapping 方法更新为以下代码

    @PostMapping("/emi_details")
    public EmiDetails createEmiRecord(@RequestBody EmiDetails emiDetails) {
        
        //Calculate your debt here using the values in the provided emiDetails
        
        return emiDetailsRepository.save(emiDetails);
    }
    

    长答案

    首先修复您的实体类。实体类是普通的 POJO,不应该有任何业务逻辑。

    @Entity
    @Table(name = "emi_details")
    public class EmiDetails {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer customer_id;
        
        @Column(name = "customer_name")
        private String customer_name;
        
        @Column(name = "amount_total")
        private Integer amount_total;
        
        @Column(name = "amount_paid")
        private Integer amount_paid;
        
        @Column(name = "amount_debt")
        private Integer amount_debt;
    
        .....SETTERS AND GETTERS
    
    }
    

    将您的 createEmiRecord 方法更新为以下内容

    @PostMapping("/emi_details")
    public EmiDetails createEmiRecord(@RequestBody EmiDetails emiDetails) {
        
        //Calculate Amount debt here
        Integer amount_debt = emiDetails.getAmount_total() - emiDetails.getAmount_paid();
        
        emiDetails.setAmount_debt(amount_debt);
        
        return emiDetailsRepository.save(emiDetails);
    
    }
    

    【讨论】:

    • 让我试试看。
    • 同时修复你的EmiDetails pojo。在构造函数中显示this.amount_debt = amount_total - amount_paid;,在setter setAmount_debt 中显示this.amount_debt = amount_total - amount_debt;
    • 如果对您有用,请告诉我。也接受答案并投票。
    • 我还在想办法,尝试用 Enity 对象的方法分配 RequestBody 对象,但它显示返回类型错误。我理解你的回答,但目前无法做到。如果你能多指导一点,会很有帮助的。
    • 检查更新的答案。
    猜你喜欢
    • 2020-05-10
    • 2020-07-22
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多