웹개발 교육/Spring
[75일] Spring (23) - MyBatis 프로젝트 (삭제)
ewok
2022. 11. 11. 17:54
ProductCont.java
@RequestMapping("/delete")
public String delete(int product_code, HttpServletRequest req) {
String filename = productDao.filename(product_code);
if(filename != null && !filename.equals("-")) {
ServletContext application = req.getSession().getServletContext();
String path = application.getRealPath("/storage");
File file = new File(path+"\\"+filename);
if(file.exists()) {
file.delete();
}//if end
}//if end
productDao.delete(product_code);
return "redirect:/product/list";
}//delete() end
ProductDAO.java
public String filename(int product_code) {
return sqlSession.selectOne("product.filename", product_code);
}//filename() end
public void delete(int product_code) {
sqlSession.delete("product.delete", product_code);
}//delete() end
product.xml
<select id="filename" resultType="String">
SELECT filename
FROM product
WHERE product_code=#{product_code}
</select>
<delete id="delete">
DELETE FROM product
WHERE product_code=#{product_code}
</delete>