如何在春季靴子中制作结构

时间:2018-03-12 13:01:11

标签: java spring spring-boot

我正在使用spring boot。如果user_id不可用,我想要的是保存,否则更新数据

  @Entity
  @Table(name="actorprofile")
  public class ActorProfile {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  @Column(name="actor_back_profile_id")
  private int actor_back_profile_id;
  public int getActor_back_profile_id() {
    return actor_back_profile_id;
    }
  public void setActor_back_profile_id(int actor_back_profile_id) {
    this.actor_back_profile_id = actor_back_profile_id;
}
@Column(name="user_id")
public int user_id;
public int getUser_id() {
    return user_id;
}
public void setUser_id(int user_id) {
    this.user_id = user_id;
}
@Column(name="sub_profession")
private String subProfession;
@Column(name="other_profession")
private String otherProfession;
@Column(name="gender")
private String gender;
@Column(name="dob")
private String dob;
@Column(name="age")
private String age;
public String getSubProfession() {
    return subProfession;
}
public void setSubProfession(String subProfession) {
    this.subProfession = subProfession;
}
public String getOtherProfession() {
    return otherProfession;
}
public void setOtherProfession(String otherProfession) {
    this.otherProfession = otherProfession;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}
public String getDob() {
    return dob;
}
public void setDob(String dob) {
    this.dob = dob;
}
public String getAge() {
    return age;
}
public void setAge(String age) {
    this.age = age;
}

} 这是我的DAO界面:

    public interface ActorDao {
    public ActorProfile updateProfile(ActorProfile profile);
     }

这是我的DAOImpl:

     @Override
     public ActorProfile updateProfile(ActorProfile profile) {
     this.sessionFac.getCurrentSession()
     .createSQLQuery("UPDATE ActorProfile set 
      sub_profession=?,other_profession=?,gender=?,"
            + "dob=?,age=? WHERE user_id=?")
      .setParameter(0,profile.getSubProfession())
      .setParameter(1,profile.getOtherProfession())
      .setParameter(2,profile.getGender())
      .setParameter(3,profile.getDob())
      .setParameter(4,profile.getAge())
      .setParameter(21,profile.getUser_id()).executeUpdate();
       return profile;

这是服务: -

       public ActorProfile updateProfile(ActorProfile profile);

这是ServiceImpl: -

   @Transactional(readOnly=false)
   public ActorProfile updateProfile(ActorProfile profile) {
    return actorDao.updateProfile(profile);
      }

这是myController: -

     @RequestMapping(value = "/updateProfile/{user_id}", method = 
    RequestMethod.POST ,consumes = MediaType.APPLICATION_JSON_VALUE, 
    produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
   public ResponseEntity<ActorProfile>  updateUser1(@RequestBody 
   ActorProfile actor,@PathVariable("user_id") int user_id) throws 
   IOException {
        @SuppressWarnings("unused")
        String msg="";
        System.out.println("data is coming:"+actor.getAboutSelf());
        //Users user= new Users();

        if(actor!=null){

            actor.setUser_id(user_id);
            actorService.updateProfile(actor);
            msg="User registration Saved Successfully";
        }

        return new ResponseEntity<ActorProfile>(actor,HttpStatus.OK);
    }

现在我必须编写一个方法,它将同时执行两个功能,如果user_id存在,那么它将更新数据,如果user_id不存在,那么它将保存数据。   请任何人知道我将如何做到这一点。

1 个答案:

答案 0 :(得分:0)

如果您已经使用弹簧靴,为什么不使用弹簧数据?它更容易,基本上是你想要在这里实现的目标。