java中d怎样转换D,java-在Spring MVC应用程序中将DAO对象转换为D...

背景:我在一个教育环境中工作,去年夏天,我们的一位开发人员使用Spring MVC和Hibernate设计和构建了Java Web应用程序.它在9月份以新学期推出,因为它取代了一个尘土飞扬的旧版Blackboard插件,使用户倍感欣慰.应用程序的主要功能用于为学生设置目标,向他们留下消息并为学生创建报告.

很快过去了几个月,原始的开发人员继续前进,应用程序也遇到了一些麻烦.

用例场景:教师登录后,他们的主屏幕出现,该屏幕包含他们所教课程的列表,以及当前所选课程的目标,消息和报告的概述以及该课程的学生注册列表课程.如果课程包含少量目标等,则可以快速返回信息.但是随着信息量的增长,加载信息所需的时间似乎呈指数增长.

经过调查,我认为我已经找到原因.我选了一个示例课程,并查看了Reports,以查看发生了什么.我发现数据库以毫秒为单位返回了相关数据,浏览器以毫秒为单位呈现了该数据,但是浏览器等待从中返回数据的时间间隔为12秒.在数据库查询完成和接收响应的前端之间对对象完成的唯一事情就是转换为DTO.

代码:这就是DAO层中报表对象的外观

@Entity

@Table(name = "REPORTS")

public class Report implements Serializable

{

/**

*

*/

private static final long serialVersionUID = -7659637777880535914L;

@Id

@GeneratedValue

@Column(name = "REPORT_ID", insertable = true, updatable = false, nullable = false, unique=true)

private Integer reportID;

@Column(name = "DATE_CREATED", insertable = true, updatable = false, nullable = false)

private GregorianCalendar dateCreated;

@Column(name = "DATE_MODIFIED", insertable = true, updatable = true, nullable = true)

private GregorianCalendar dateModified;

@Column(name = "TITLE", insertable = true, updatable = true, nullable = false, length=1000)

private String title;

@Column(name = "CURRENT_PERFORMANCE_GRADE", insertable = true, updatable = true, nullable = false)

private String currentPerformanceGrade;

@Column(name = "TARGET_GRADE", insertable = true, updatable = true, nullable = false)

private String targetGrade;

//VARCHAR(MAX) as this is the main body of the tutor report comments. Here the tutor can write as much content as they like.

@Column(name = "TUTOR_COMMENTS", insertable = true, updatable = true, nullable = false, columnDefinition="VARCHAR(MAX)")

private String tutorComments;

//getters and setters below

}

那里还有其他字段,例如报告所链接的用户,课程,编写报告的导师等.但是为了简单起见,在这里我将其省略.

public class ReportDTO implements Serializable

{

/**

*

*/

private static final long serialVersionUID = 2795129355073929139L;

private Integer reportID;

private String dateCreated;

private String dateModified;

private String title;

private String currentPerformanceGrade;

private String targetGrade;

private String tutorComments;

//getters and setters below

}

因此,主要的区别在于日期对象已成为日期格式的字符串,而与GregorianCalendar对象相对,因此日期的前端显示采用了易于阅读的格式.这是转换为DTO涉及的示例.服务层中的Single方法获取DAO对象,从中获取相关字段,将其设置在新构造的DTO对象中,根据需要进行转换(例如,公历将日期格式化为String)并返回DTO:

public ReportDTO convertToDto(Report daoReport) throws Exception

{

ReportDTO dtoReport = new ReportDTO();

try

{

if(daoReport.getReportID() != null)

{

dtoReport.setReportID(daoReport.getReportID());

}

if(daoReport.getDateCreated() != null)

{

dtoReport.setDateCreated(ReportServiceImpl.ISO_DATE_TIME_FORMAT.format(daoReport.getDateCreated().getTime()));

}

if(daoReport.getDateModified() != null)

{

dtoReport.setDateModified(ReportServiceImpl.ISO_DATE_TIME_FORMAT.format(daoReport.getDateModified().getTime()));

}

if(daoReport.getTitle() != null)

{

dtoReport.setTitle(daoReport.getTitle());

}

if(daoReport.getCurrentPerformanceGrade() != null)

{

dtoReport.setCurrentPerformanceGrade(daoReport.getCurrentPerformanceGrade());

}

if(daoReport.getTargetGrade() != null)

{

dtoReport.setTargetGrade(daoReport.getTargetGrade());

}

if(daoReport.getTutorComments() != null)

{

dtoReport.setTutorComments(daoReport.getTutorComments());

}

return dtoReport;

}

catch(Exception e)

{

Exception myException = new Exception("Exception was thrown while converting a persistent Report object to it's data transport equivalent", e);

throw myException;

}

问题:毕竟,我的问题是,这是从DAO转换为DTO的正确方法吗?自从他离开以来,我一直在遵循他的代码,并且以相同的方式完成了所有新增操作.将对象返回到前端而不进行转换,我看到的结果是> 300毫秒而不是12秒.

我知道他从here开始为该项目学习了Spring MVC,所以他不是一个经验丰富的Spring开发人员,我也不是.根据我们看到如此多的请求时间,我们肯定做错了一个事实.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值