我试图在我的应用程序中将每个员工的图片保存在他/她的个人资料旁边,然后在任何用户打开此员工个人资料时检索此图片,因此我制作了以下类:
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="EMPLOYEE_ID")
private Long id;
.
//many other fields goes here...
.
@OneToOne(cascade={CascadeType.ALL})
@PrimaryKeyJoinColumn
private EmployeePicture employeepicture;
}
public class EmployeePicture {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="EMPPIC_ID")
private Long id;
@Column(name="EMPLOYEE_PIC")
@Lob
private Blob employeePicture;
}
然后我创建了以下 DAO 类,当然,除了我已经拥有的 EmployeeDAO 类......
@Repository
public class EmployeePictureDAO implements IEmployeePictureDAO {
@Autowired
SessionFactory sessionfactory;
public void saveEmployeePicture(EmployeePicture employeepicture) {
sessionfactory.getCurrentSession().save(employeepicture);
}
public void updateEmployeePicture(EmployeePicture employeepicture) {
sessionfactory.getCurrentSession().update(employeepicture);
}
public void deleteEmployeePicture(EmployeePicture employeepicture) {
sessionfactory.getCurrentSession().delete(employeepicture);
}
public EmployeePicture getEmployeePictureByPK(Long id) {
return (EmployeePicture)sessionfactory.getCurrentSession().get(EmployeePicture.class,id);
}
}
至于服务层类,我只有 EmployeeService 类,我相信它会同时调用 EmployeeDAO 和 EmployeePictureDAO 方法,因为数据和图片将同时保存/更新和删除。但不幸的是,我无法弄清楚/找到(在搜索网络之后)如何从 JSP 中保存/检索图像。那么有人可以通过给我一个关于如何在服务/控制器类和 JSP 中保存/检索员工图像的示例代码来帮助我吗?
谢谢你的时间