对于在Java 8上使用Spring Boot和Hibernate进行api应用程序开发,需要将bean对象值(例如bookId,bookName,bookDescription)存储到db中。请找到示例类
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;
}
}
这是将请求对象值映射到hibernate dao对象(域模型)的简单且最好的方法,缩进是执行代码行的最快方法,这意味着减少api响应时间。我们遵循以下方式
是否有其他方法来映射对象!
答案 0 :(得分:0)
据我所知,Java有多个映射框架。 我个人有Dozer Mapping的经验。 您也可以查看this article。但我想所有这些框架都使用反射,可能比setter / getter aproach慢。
但是所有这些框架都可以为您提供更好的可视化字段映射。并减少Builder或Mapper类的数量:)
答案 1 :(得分:0)
BeanUtils.copyProperties使用的反射对性能并不是很好。您可以在java文档中找到它:
通过反射填充JavaBeans属性的实用程序方法。
您可以做的是使用构建器模式而不是getter / setter。在性能方面会好得多。要了解更多相关信息,我强烈建议您阅读" Effective Java "来自Joshua Bloch。
文档:
反思表现:Java Reflection Performance
构建器模式:http://www.informit.com/articles/article.aspx?p=1216151&seqNum=2