| ||||
【文章简介】 开发环境:Eclipse3.2+MyEclipse5.01GA;框架使用:Struts+Spring+Hibernate在Employee数据库中有三个表:EmployeeInfo(员工信息表)、Depts(部门表)、Business(职务表)EmployeeInfo表结构:emp_id主键emp_nameemp_sexemp_ageemp_dept存储dept_idemp_business存
【文章正文】 * @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String empId=request.getParameter("id");//获得页面传递的员工编号
EmployeedetialinfoDAO empInfoDAO=EmployeedetialinfoDAO
.getFromApplicationContext(getWebApplicationContext());
List list=empInfoDAO.findByProperty("id.empId", empId);
Employeedetialinfo emp=(Employeedetialinfo)(list.get(0));
EmployeedetialinfoId empInfo=emp.getId();
request.setAttribute("empinfo", empInfo);
return mapping.findForward("EmployeeDetial");
}
}
在实际应用中,我们需要根据一个已知的员工ID来得到他的详细信息,之前我使用empInfoDAO.findById方法,这个方法传递进去的参数是一个EmployeedetialinfoId对象,我实际化了EmployeedetialinfoId类后再setEmpId,得出来的结果都是Nullpoint;整整找了快一个月的资料都没能找出个所以然;后来我试了findByProperty方法,它在DAO类里的定义是这样的:
public List findByProperty(String propertyName, Object value)
第一个参数是类里的属性名,第二个参数是值;需要注意的是如果在设置参数的时候直接传empId这个属性将会报错,提示找不到这个属性,而是需要在empId前面加上父类名Id,即"id.empId",这个ID是何许人也?它的定义就在Employeedetialinfo.hbm.xml映射文件里,<composite-id name="id" class="com.terry.hibernate.modal.EmployeedetialinfoId">。
以上的操作就完成了对视图的查询,小小经验,希望对您有用。
电子商务辩论台: |
|
载入中…
|