(二十八)分类信息的curd-分类信息删除
删除分类步骤分析: 1.在list.jsp上编写 删除连接 /store/adminCategory?method=delete&cid=?? 2.在delete方法中 获取cid 调用service的delete方法 页面重定向/store/adminCategory?method=findAll 3.service中delete 添加事务 通过cid先更新所有的商品分类信息 ,删除后分类信息为null 调用dao(商品表、分类表) 删除分类 事务控制 清空缓存
/store/WebContent/admin/category/list.jsp
/store/src/com/louis/web/servlet/AdminCategoryServlet.java
/** * 删除分类 * @param request * @param response * @return * @throws Exception */ public String delete(HttpServletRequest request, HttpServletResponse response) throws Exception { //1.获取cid String cid = request.getParameter("cid"); //2.调用service 完成删除 CategoryService cs=(CategoryService) BeanFactory.getBean("CategoryService"); cs.delete(cid); //3.重定向 response.sendRedirect(request.getContextPath()+"/adminCategory?method=findAll"); return null; }
/store/src/com/louis/service/impl/CategoryServiceImpl.java
public void delete(String cid) throws Exception{ try { //1.开启事务 DataSourceUtils.startTransaction(); //2.更新商品 ProductDao pd=(ProductDao) BeanFactory.getBean("ProductDao"); pd.updateCid(cid); //3.删除分类 CategoryDao cd=(CategoryDao) BeanFactory.getBean("CategoryDao"); cd.delete(cid); //4.事务控制 DataSourceUtils.commitAndClose(); //5.清空缓存 CacheManager cm = CacheManager.create(CategoryServiceImpl.class.getClassLoader().getResourceAsStream("ehcache.xml")); Cache cache = cm.getCache("categoryCache"); cache.remove("clist"); } catch (Exception e) { e.printStackTrace(); DataSourceUtils.rollbackAndClose(); throw e; } }
/store/src/com/louis/dao/impl/ProductDaoImpl.java
/** * 更新商品的cid 为删除分类的时候准备 */ @Override public void updateCid(String cid) throws Exception { QueryRunner qr = new QueryRunner(); String sql="update product set cid = null where cid = ?"; qr.update(DataSourceUtils.getConnection(), sql, cid); }
/store/src/com/louis/dao/impl/CategoryDaoImpl.java
/** * 删除分类 */ @Override public void delete(String cid) throws Exception { QueryRunner qr = new QueryRunner(); String sql="delete from category where cid = ?"; qr.update(DataSourceUtils.getConnection(), sql, cid); }
posted on 2017-10-16 11:06 Michael2397 阅读(...) 评论(...) 编辑 收藏