我正在使用CrudRepo开发Spring Boot项目 现在findAll()和save()方法工作得很好但是当我试图获取用户或删除用户时它没有返回
当我将常规字符串作为“jksahd”之类的电子邮件或其工作时,但是当我输入有效邮件如“hsdg2@dsj.com”时,删除()和findOne()时,我无法解决这个问题不起作用我的代码:
实体:
@Entity
@Table(name = "User")
public class User {
private long phoneNumber;
private String name;
private String lastName;
private String country;
private String countryCode;
private boolean isAdmin;
private String Email;
private String password;
private boolean isPaid;
@Basic(optional = false)
@Column(insertable = false,updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@ColumnDefault("1492944369375")
private Date timeStamp = new Date(System.currentTimeMillis());
private String langSpeak;
private String tradeLvl;
public User(long phoneNumber, String name, String lastName, String country, String countryCode, boolean isAdmin, String email, String password, boolean isPaid, String langSpeak, String tradeLvl) {
this.phoneNumber = phoneNumber;
this.name = name;
this.lastName = lastName;
this.country = country;
this.countryCode = countryCode;
this.isAdmin = isAdmin;
this.Email = email;
this.password = password;
this.isPaid = isPaid;
this.timeStamp = timeStamp;
//timeStamp = new Date();
//setTimeStamp(timeStamp);
this.langSpeak = langSpeak;
this.tradeLvl = tradeLvl;
}
public User(){}
@Id
public String getEmail() {
return Email;
}
然后是getters和setter
服务方法
public String newUser(User user) {
if (repo.exists(user.getEmail()))
return "already exsists";
repo.save(user);
return "ok";
}
public User getUserByMail(String mail){
//return userDao.getUserByMail(mail);
repo.findOne(mail);
return repo.findByEmail(mail);
}
public String deleteUserByMail(String mail) {
if (repo.exists(mail)) {
repo.delete(mail);
return "ok";
}
return "already deleted";
}
回购课程:
@Repository
public interface UsersRepo extends CrudRepository<User,String> {
public User findByEmail(String mail);
}
控制器:
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public String newUser(@RequestBody User user){
return userService.newUser(user);
}
@RequestMapping(value = "/{mail}",method = RequestMethod.GET)
public User getUserByMail(@PathVariable("mail") String mail){
return userService.getUserByMail(mail);
}
@RequestMapping(value = "/{mail}",method = RequestMethod.DELETE)
public String deleteUserById(@PathVariable ("mail") String mail){
return userService.deleteUserByMail(mail);
}