欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Boot 设置联合主键

程序员文章站 2022-04-25 07:53:34
...

1.创建一个实体类

@Entity
@Table(name = "cmaq")

public class Cmaq implements Serializable {

    @Id
    @Column(nullable = false,name = "pred_date")
    private Date prediction_date;

    @Id
    @Column(nullable = false,name = "st_date")
    private Date start_date;

    @Id
    @Column(nullable = false,name = "id")
    private int city_id;

    @Column(nullable = false,name = "avg")
    private int avg;


    public Cmaq(){

    }

    public Cmaq(Date st_date, Date pred_date, int id) {
        this.st_date = st_date;
        this.pred_date = pred_date;
        this.id = id;
    }

    public Date getPred_date() { return pred_date; }

    public void setPred_date(Date pred_date) { this.pred_date = pred_date; }

    public Date getSt_date() { return st_date; }

    public void setSt_date(Date st_date) { this.st_date = st_date; }

    public int getId() { return id; }

    public void setId(int id) { this.id = id; }

    public int getAvg() { return avg; }

    public void setAvg(int avg) { this.avg = avg; }


}

2.创建主键实现类

public class Key implements Serializable  {//主键实现类

    @Column(nullable = false,name = "st_date")
    private Date prediction_date;

    @Column(nullable = false,name = "pred_date")
    private Date start_date;

    @Column(nullable = false,name = "id")
    private int id;

    public Key(){

    }
    public Key(Date pred_date, Date st_date, int id){
        this.pred_date = pred_date;
        this.st_date = st_date;
        this.id = id;

    }

    public Date getStt_date() {
        return st_date;
    }

    public void setSt_date(Date st_date) {
        this.st_date = st_date;
    }

    public Date getPred_date() {
        return pred_date;
    }

    public void setPred_date(Date pred_date) {
        this.pred_date = pred_date;
    }

    public int getId() { return id; }

    public void setId(int id) {
        this.id = id;
    }


}

3.在实体类里改动一点点

@Entity
@Table(name = "cmaq")
@IdClass(Key.class)  //增加了这个
public class Cmaq implements Serializable {

    @Id
    @Column(nullable = false,name = "pred_date")
    private Date prediction_date;

    @Id
    @Column(nullable = false,name = "st_date")
    private Date start_date;

    @Id
    @Column(nullable = false,name = "id")
    private int city_id;

    @Column(nullable = false,name = "avg")
    private int avg;


    public Cmaq(){

    }

    public Cmaq(Date st_date, Date pred_date, int id) {
        this.st_date = st_date;
        this.pred_date = pred_date;
        this.id = id;
    }

    public Date getPred_date() { return pred_date; }

    public void setPred_date(Date pred_date) { this.pred_date = pred_date; }

    public Date getSt_date() { return st_date; }

    public void setSt_date(Date st_date) { this.st_date = st_date; }

    public int getId() { return id; }

    public void setId(int id) { this.id = id; }

    public int getAvg() { return avg; }

    public void setAvg(int avg) { this.avg = avg; }


}