[英]How to make phone as ID Spring-boot
How can I make sure that only the phone and message are sent to my database because now I also have the ID of each request that was successfully saved.我如何确保只有电话和消息发送到我的数据库,因为现在我还拥有每个成功保存的请求的 ID。 I want to make the phone a unique identifier by which the values for repeats will be compared in the database.
我想让电话成为唯一标识符,通过该标识符将在数据库中比较重复值。
How to make phone as ID.如何将手机设为身份证。
UserController用户控制器
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserRepo userRepo;
@PostMapping(consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
public String createUser(@Valid @RequestBody User requestUserDetails) {
userRepo.save(requestUserDetails);
return "The message delivered.";
}
}
User用户
@Entity
@Table(name = "ApiTable", schema = "TestApi")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String phone;
private String message;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getMessage() {
return message;
}
public void setLastName(String message) {
this.message = message;
}
}
Application应用
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
UserRepo用户回购
public interface UserRepo extends CrudRepository<User, Long> {
} }
Here is how to make phone
field an Id based on your example:以下是根据您的示例将
phone
字段设置为 Id 的方法:
@Entity
@Table(name = "ApiTable", schema = "TestApi")
public class User {
@Id
private String phone;
private String message;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getMessage() {
return message;
}
public void setLastName(String message) {
this.message = message;
}
}
And your repository will look like:您的存储库将如下所示:
public interface UserRepo extends CrudRepository<User, String> {
}
Simply use @UniqueConstraint in phone,message pair and create a new field for id.只需在电话、消息对中使用 @UniqueConstraint 并为 id 创建一个新字段。
@Entity
@Table(name = "ApiTable", schema = "TestApi",
uniqueConstraints={@UniqueConstraint(columnNames = {"phone" , "message"})
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique=true, nullable = false)
private Long id;
private String phone;
private String message;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getMessage() {
return message;
}
public void setLastName(String message) {
this.message = message;
}
}
You didn't add what are the columns for phone and message.您没有添加电话和消息的列。 Try with this.
试试这个。
@Embeddable
public class KeyUser implements Serializable {
@Column(name = "Phone", nullable = false)
private int phone;
@Column(name = "Message", nullable = false)
private int message;
/** getters and setters **/
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.