[英]How to make structure in spring boot
I am using spring boot.In which I want if user_id is not available then save it else update data 我正在使用Spring Boot。如果用户名不可用,请保存在其中,否则请更新数据
@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;
}
} This is my DAO interface: 这是我的DAO界面:
public interface ActorDao {
public ActorProfile updateProfile(ActorProfile profile);
}
This is my DAOImpl: 这是我的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;
This is Service:- 这是服务:-
public ActorProfile updateProfile(ActorProfile profile);
This is ServiceImpl:- 这是ServiceImpl:
@Transactional(readOnly=false)
public ActorProfile updateProfile(ActorProfile profile) {
return actorDao.updateProfile(profile);
}
This is myController:- 这是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);
}
now I have to write a method which will do two function at a same time that if user_id is present then it will update data and if user_id is not present then it will save data. 现在,我必须编写一个可以同时执行两个功能的方法,如果存在user_id,则它将更新数据;如果没有user_id,则将保存数据。 Please anyone know how i will do that.
请任何人知道我将如何做。
If you're already using spring boot, why not use spring data? 如果您已经在使用spring boot,为什么不使用spring数据呢? It's easier and basically does what you're trying to achieve here.
这很容易,基本上可以完成您想要在这里实现的目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.