Für die API-Anwendungsentwicklung mit Spring Boot und Hibernate unter Java 8 müssen die Bean-Objektwerte (z. B. bookId, bookName, bookDescription) in db gespeichert werden. Bitte entnehmen Sie die BeispielklassenWelches ist der beste Weg, um Request-Bean-Objekte dem Hibernate-Domain-Model-Objekt zuzuordnen?
BeanObject.java
package com.example;
public class BeanObject {
private String bookId;
private String bookName;
private String bookDescription;
/**
* @return the bookId
*/
public String getBookId() {
return bookId;
}
/**
* @param bookId the bookId to set
*/
public void setBookId(String bookId) {
this.bookId = bookId;
}
/**
* @return the bookName
*/
public String getBookName() {
return bookName;
}
/**
* @param bookName the bookName to set
*/
public void setBookName(String bookName) {
this.bookName = bookName;
}
/**
* @return the bookDescription
*/
public String getBookDescription() {
return bookDescription;
}
/**
* @param bookDescription the bookDescription to set
*/
public void setBookDescription(String bookDescription) {
this.bookDescription = bookDescription;
}
}
DomainObject.java
/**
*
*/
package com.example;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
* @author pandiaraj
*
*/
@Entity
@Table(name = "BOOKS")
public class DomainObject {
private String bookId;
private String bookName;
private String bookDescription;
/**
* @return the bookId
*/
@ID
@Column(name = "ID", unique = true, nullable = false, length = 100)
public String getBookId() {
return bookId;
}
/**
* @param bookId the bookId to set
*/
public void setBookId(String bookId) {
this.bookId = bookId;
}
/**
* @return the bookName
*/
@Column(name = "NAME", length = 30)
public String getBookName() {
return bookName;
}
/**
* @param bookName the bookName to set
*/
public void setBookName(String bookName) {
this.bookName = bookName;
}
/**
* @return the bookDescription
*/
@Column(name = "DESCRIPTION", length = 30)
public String getBookDescription() {
return bookDescription;
}
/**
* @param bookDescription the bookDescription to set
*/
public void setBookDescription(String bookDescription) {
this.bookDescription = bookDescription;
}
}
die einfach ist und beste Weg, um die Anfrage Objektwerte abbildet DAO-Objekt (Domänenmodell) in den Ruhezustand, Eine Einrückung ist der schnellste Weg, um eine Codezeile auszuführen. Dies bedeutet eine Verkürzung der API-Antwortzeit. Wir folgten unter Möglichkeiten
- Verwenden von Getter und Setter-Methoden.
- Verwenden von Apache Commons - BeanUtils.copyProperties (toBean, fromBean);
Gibt es noch andere Möglichkeiten, um die Objekte zuordnen!