J2EE DAO模式解析(三)
我们先看一下界面
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <% String path=request.getContextPath(); String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <base href="<%=basePath %>"> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>添加流向单维护</title> </head> <body> <form name="flowCardAddForm" method="post" action="servlet/flowcard/FlowCardServlet" > <input type="hidden" name="command" value="add"> <input type=”submit” id=”btnAdd” value=”添加”/> </form> </body> </html>
下面看一下Servlet
public class FlowCardServlet extends HttpServlet { private FlowCardManager flowCardManager; public void init() throws ServletException { flowCardManager=(FlowCardManager)getBeanFactory().getServiceObject(FlowCardManager.class); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { add(request,response); } private void add(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { /** ***调用业务逻辑层代码之前的准备代码*** */ //通过工厂取得业务逻辑对象 FlowCardManager flowCardManager=(FlowCardManager)getBeanFactory().getServiceObject(FlowCardManager.class); flowCardManager.addFlowCard(flowCard); response.sendRedirect(request.getContextPath()+"/flowcard/flow_card_maint.jsp"); } protected BeanFactory getBeanFactory() { BeanFactory beanFactory=BeanFactory.getInstance(); return beanFactory; } }
下面看一下业务逻辑层
public interface FlowCardManager { public void addFlowCard(FlowCard flowCard) throws ApplicationException; }
public class FlowCardManagerImpl implements FlowCardManager { private FlowCardDao flowCardDao; public FlowCardManagerImpl() { this.flowCardDao=(FlowCardDao)BeanFactory.getInstance().getDaoObject(FlowCardDao.class); } public void addFlowCard(FlowCard flowCard) throws ApplicationException { Connection conn=null; try{ //取得Connection conn=ConnectionManager.getConnection(); //开始事务 ConnectionManager.beginTransaction(conn); //添加前的准备工作,给参数赋值***************省略 //添加流向单主信息 flowCardDao.addFlowCardMaster(flowCardVouNo, flowCard); //提交事务 ConnectionManager.commitTransaction(conn); }catch(DaoException e) { //回滚事务 ConnectionManager.rollbackTransaction(conn); throw new ApplicationException("添加流向单失败"); }finally { //关闭Connection并从ThreadLocal清除 ConnectionManager.closeConnection(); } } }
最后Dao层
public interface FlowCardDao { /** * 添加流向单主信息 * @param flowCardVouNo * @param flowCard */ public void addFlowCardMaster(String flowCardVouNo,FlowCard flowCard); }
public class FlowCardDaoImpl implements FlowCardDao { @Override public void addFlowCardMaster(String flowCardVouNo, FlowCard flowCard) { StringBuffer sbSql=new StringBuffer(); sbSql.append("……………省略………………….."); PreparedStatement pstmt=null; try{ Connection conn=ConnectionManager.getConnection(); pstmt=conn.prepareStatement(sbSql.toString()); pstmt.setString(…为sql语句中需要的参数赋值……….); pstmt.executeUpdate(); }catch(SQLException e){ e.printStackTrace(); System.out.println("FlowCardDaoImpl-->>addFlowCardMater,exception:"+e); throw new DaoException(e); }finally{ ConnectionManager.close(pstmt); //此处并没有关闭connection } } }
整个DAO模式就是这样子了。