首页 > 解决方案 > 如何从组合键中设置和获取值

问题描述

我正在尝试使用 如何在我的控制器中设置和获取值来设置cid学生类中的值。SubjectMark->private String cid;实体和控制器方法如下:

@Entity
public class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    private SubjectMark id;
    private String fullName;
    private Integer totalMarks;
    private Double percentage;
    private String grade;
    //Setters and getters
}


//Composit class
@Embeddable
public class SubjectMark implements Serializable {
//Composit key
private String cid;
//Setters and getters

}

在我的控制器中,我尝试像这样设置值:

@RequestMapping(value="getstdata",method=RequestMethod.GET)
@ResponseBody
public String getstdata(@RequestParam(value="cid")String cid){  
//Some code
try{

Student st=new Student();
st.getId().setCid(cid);//Set value like this but it is getting null pointer exception
//some code

//retuen some value
}

请帮我!

标签: javaspringspring-bootspring-data-jpadrools

解决方案


我猜你需要在你的类中编写一个get set方法......尝试编写类似的东西

私人字符串 cid;

公共字符串 Cid { 获取 => cid; 设置 => cid = 值;}


推荐阅读