案例40-層與層之間的解耦(面向接口編程)
阿新 • • 發佈:2018-02-28
sql 獲得 post turn 對象 轉發 詳情 hot axon
1 bean.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans> <!-- 配置AdminServiceImpl的清單 --> <bean id="adminService" class="www.test.service.impl.AdminServiceImpl"></bean> </beans>
2 BeanFactory工廠類代碼
package www.test.utils; import org.dom4j.Document;import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class BeanFactory { public static Object getBean(String id){ //生成對象--根據清單生產--配置文件--將每一個bean對象的生產細節配置到配置文件中 //使用dom4j的xml解析技術 導入兩個jar包 // dom4j-1.6.1.jar 和 jaxen-1.1-beta-6.jartry { // 1 創建解析器 SAXReader reader = new SAXReader(); // 2 解析文檔--bean.xml 在src下面 String path = BeanFactory.class.getClassLoader().getResource("bean.xml").getPath(); //讀取 Document doc = reader.read(path);// 3 獲得元素--參數是xpath規則 Element element = (Element) doc.selectSingleNode("//bean[@id=‘"+id+"‘]"); //<bean id="adminService" class="www.test.service.impl.AdminServiceImpl"></bean> String className = element.attributeValue("class"); //www.test.service.impl.AdminServiceImpl //使用反射創建對象 Class clazz = Class.forName(className); Object object = clazz.getDeclaredConstructor().newInstance(); return object; } catch (Exception e) { e.printStackTrace(); } return null; } }
3 AdminService接口代碼
package www.test.service; import java.sql.SQLException; import java.util.List; import java.util.Map; import www.test.dao.AdminDao; import www.test.domain.Category; import www.test.domain.Order; import www.test.domain.Product; public interface AdminService { // 獲取分類 public List<Category> findAllCategory(); // 添加商品 public void saveProduct(Product product) throws SQLException; // 獲取商品列表 public List<Product> findProductList() throws SQLException ; public void deleteProductByPid(String pid) throws SQLException; // 商品修改的回顯 public Map<String, Object> productBackShow(String pid) throws SQLException; // 獲取所有訂單 public List<Order> findAllOrdersList() throws SQLException; //查詢訂單詳情 public List<Map<String, Object>> findOrderInfoByOid(String oid) throws SQLException; }
4 AdminServiceImpl接口實現類代碼
package www.test.service.impl; import java.sql.SQLException; import java.util.List; import java.util.Map; import www.test.dao.AdminDao; import www.test.domain.Category; import www.test.domain.Order; import www.test.domain.Product; import www.test.service.AdminService; public class AdminServiceImpl implements AdminService { // 獲取分類 public List<Category> findAllCategory() { AdminDao dao = new AdminDao(); try { return dao.findAllCategory(); } catch (SQLException e) { e.printStackTrace(); return null; } } // 添加商品 public void saveProduct(Product product) throws SQLException { AdminDao dao = new AdminDao(); dao.saveProduct(product); } // 獲取商品列表 public List<Product> findProductList() throws SQLException { AdminDao dao = new AdminDao(); return dao.findProductList(); } public void deleteProductByPid(String pid) throws SQLException { AdminDao dao = new AdminDao(); dao.deleteProductByPid(pid); } // 商品修改的回顯 public Map<String, Object> productBackShow(String pid) throws SQLException { AdminDao dao = new AdminDao(); return dao.productBackShow(pid); } // 獲取所有訂單 public List<Order> findAllOrdersList() throws SQLException { AdminDao dao = new AdminDao(); return dao.findAllOrdersList(); } //查詢訂單詳情 public List<Map<String, Object>> findOrderInfoByOid(String oid) throws SQLException { AdminDao dao = new AdminDao(); return dao.findOrderInfoByOid(oid); } }
5 AdminServlet代碼
package www.test.web.servlet; import java.io.IOException; import java.sql.SQLException; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils; import com.google.gson.Gson; import www.test.domain.Category; import www.test.domain.Order; import www.test.domain.Product; import www.test.service.AdminService; import www.test.service.impl.AdminServiceImpl; import www.test.utils.BeanFactory; public class AdminServlet extends BaseServlet{ // 1 頁面加載完畢後異步獲得分類數據 public void findAllCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //提供一個List<Category>轉成json字符串 AdminService service = (AdminService) BeanFactory.getBean("adminService"); List<Category> categoryList = service.findAllCategory(); //將List<Category>轉換成json格式 Gson gson = new Gson(); String json = gson.toJson(categoryList); //解決亂碼並寫出 response.setContentType("text/html;charset=UTF-8"); response.getWriter().write(json); } // 2 商品列表的展示findAllProductList public void findAllProductList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { AdminService service = (AdminService) BeanFactory.getBean("adminService"); List<Product> productList = null; try { productList = service.findProductList(); } catch (SQLException e) { e.printStackTrace(); } // 轉發 request.setAttribute("productList", productList); request.getRequestDispatcher("/admin/product/list.jsp").forward(request, response); } // 3 刪除商品 delProduct public void delProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取要刪除商品的id String pid = request.getParameter("pid"); AdminService service = (AdminService) BeanFactory.getBean("adminService"); try { service.deleteProductByPid(pid); //刪除成功後回到商品列表頁面 List<Product> productList = service.findProductList(); request.setAttribute("productList", productList); request.getRequestDispatcher("/admin/product/list.jsp").forward(request, response); } catch (SQLException e) { e.printStackTrace(); } } // 4 修改商品的操作 editProduct public void editProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //修改商品的回顯部分 AdminService service = (AdminService) BeanFactory.getBean("adminService"); // 1獲取要修改的商品的pid String pid = request.getParameter("pid"); // 2 調用AdminService的方法 Map<String, Object> map = null; Product product =new Product(); try { map = service.productBackShow(pid); /*{shop_price=2298.0, market_price=2399.0, pflag=0, pimage=products/1/c_0014.jpg, pname=vivo X5Pro, is_hot=1, pdesc=移動聯通雙4G手機 3G運存版 極光白【購機送藍牙耳機+藍牙自拍桿】 新升級3G運行內存·雙2.5D弧面玻璃·眼球識別技術, pid=11, cid=1, pdate=2015-11-02}*/ // 將除了category的進行自動映射封裝 BeanUtils.populate(product, map); //private Category category; 進行手動封裝 Category category = new Category(); String cid =map.get("cid").toString(); category.setCid(cid); //將category封裝到product中 product.setCategory(category); } catch (Exception e) { e.printStackTrace(); } //存儲轉發 request.setAttribute("product", product); request.getRequestDispatcher("/admin/product/edit.jsp").forward(request, response); } // 5 獲得所有訂單 findAllOrders public void findAllOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 獲得所有的訂單信息---List<Order> AdminService service = (AdminService) BeanFactory.getBean("adminService"); List<Order> orderList = null; try { orderList = service.findAllOrdersList(); } catch (SQLException e) { e.printStackTrace(); } request.setAttribute("orderList", orderList); request.getRequestDispatcher("/admin/order/list.jsp").forward(request, response); } // 6根據訂單oid查詢訂單項和商品信息 findOrderInfoByOid public void findOrderInfoByOid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //模擬從服務器讀數據有一個延遲的效果 try { Thread.sleep(500); } catch (InterruptedException e1) { e1.printStackTrace(); } //獲得oid String oid = request.getParameter("oid"); //用解耦合的方式進行編碼-----解web層與service層 //使用工廠+反射+配置文件 AdminService service = (AdminService) BeanFactory.getBean("adminService"); List<Map<String,Object>> mapList = null; try { mapList = service.findOrderInfoByOid(oid); } catch (SQLException e) { e.printStackTrace(); } //格式轉換 Gson gson = new Gson(); String json = gson.toJson(mapList); //註意ajax提交回寫 System.out.println(json); /* * [ * {"shop_price":5999.0,"count":2,"pname":"微星(MSI)GE62 2QC-264XCN","pimage":"products/1/c_0042.jpg","subtotal":11998.0}, * {"shop_price":1299.0,"count":1,"pname":"小米 4c 標準版","pimage":"products/1/c_0001.jpg","subtotal":1299.0},{"shop_price":2699.0,"count":6,"pname":"中興 AXON","pimage":"products/1/c_0002.jpg","subtotal":16194.0}, * {"shop_price":2298.0,"count":1,"pname":"vivo X5Pro","pimage":"products/1/c_0014.jpg","subtotal":2298.0}] */ response.setContentType("text/html;charset=UTF-8"); response.getWriter().write(json); } }
案例40-層與層之間的解耦(面向接口編程)