學習Struts+spring+hibernate的筆記(2)

3.做DAO代碼,

Java代碼
  1. package infoweb.dao;  
  2.   
  3. import java.util.List;  
  4. import java.util.Iterator;  
  5.   
  6. import infoweb.pojo.Board;  
  7.   
  8.   
  9. import net.sf.hibernate.HibernateException;  
  10. import net.sf.hibernate.Query;  
  11. import net.sf.hibernate.Session;  
  12.   
  13. import org.springframework.orm.hibernate.HibernateCallback;  
  14. import org.springframework.orm.hibernate.support.HibernateDaoSupport;  
  15.   
  16.   
  17. /** 
  18.  * <p>Title: 版塊分類DAOImpl</p> 
  19.  * <p>Description: 用樹型結構實現</p> 
  20.  * <p>Copyright: Copyright (c); 2004</p> 
  21.  * <p>Company: </p> 
  22.  * @author 段洪傑 
  23.  * @version 1.0 
  24.  */  
  25.   
  26.   
  27. public class BoardTreeDAOImpl extends HibernateDaoSupport implements  
  28.     IBoardTreeDAO {  
  29.   
  30.   
  31.   /** 
  32.    * 構造函數 
  33.    */  
  34.   public BoardTreeDAOImpl(); {  
  35.     super();;  
  36.   }  
  37.   
  38.   
  39.   /** 
  40.    * 通過ID取得版塊 
  41.    * @param id String 
  42.    * @return Board 
  43.    */  
  44.   
  45.   public Board getBoardById(String id); {  
  46.     Board board = (Board); getHibernateTemplate();.load(Board.class, id);;  
  47.     return board;  
  48.   }  
  49.   
  50.   
  51.   /** 
  52.    * 取根葉 
  53.    * @return Iterator 
  54.    */  
  55.   public Iterator getRoots(); throws HibernateException {  
  56.     String queryString =  
  57.         "select board from Board as board where board.parentId='root' order by board.id desc";  
  58.     List roots = getHibernateTemplate();.find(queryString);;  
  59.     return roots.iterator();;  
  60.   }  
  61.   
  62.   
  63.   /** 
  64.    * 存根葉 
  65.    * @param board Board 
  66.    */  
  67.   public void setRoot(Board board); {  
  68.     board.setParentId("root");;  
  69.     getHibernateTemplate();.save(board);;  
  70.   }  
  71.   
  72.   
  73.   /** 
  74.    * 取子葉 
  75.    * @param  parentid String 
  76.    * @return List 
  77.    */  
  78.   public Iterator getChildren(String parentid); {  
  79.     /* 
  80.          String queryString = 
  81.      "select board as Board where board.parent_id='parentid' order by board.id desc"; 
  82.          List children = getHibernateTemplate();.find(queryString);; 
  83.          return children; 
  84.      */  
  85.     Board parent = (Board); getHibernateTemplate();.load(Board.class, parentid);;  
  86.     return parent.getChildren();.iterator();;  
  87.   }  
  88.   
  89.   
  90.   /** 
  91.    * 取子葉數 
  92.    * @param parentid String 
  93.    * @return int 
  94.    */  
  95.   
  96.   public int getChildrenCount(String parentid); {  
  97.     /* 
  98.          String queryString = 
  99.      "select count(*); Board where board.parent_id='parentid' order by board.id desc"; 
  100.          List children = getHibernateTemplate();.find(queryString);; 
  101.          int count = ((Integer); children.iterator();.next(););.intValue();; 
  102.          return count; 
  103.      */  
  104.     Board parent = (Board); getHibernateTemplate();.load(Board.class, parentid);;  
  105.     int count = parent.getChildren();.size();;  
  106.     return count;  
  107.   }  
  108.   
  109.   
  110.   /** 
  111.    * 存子葉 
  112.    * @param parentLeaf Leaf 
  113.    */  
  114.   public void setChild(Board board, String parentid); {  
  115.     board.setParentId(parentid);;  
  116.     getHibernateTemplate();.save(board);;  
  117.   }  
  118.   
  119.   
  120.   /** 
  121.    * 
  122.    * 刪除該葉和它的子葉 
  123.    * @param board Board 
  124.    */  
  125.   public void deleteBranch(Board board); {  
  126.       getHibernateTemplate();.delete(board);;  
  127.   }  
  128.   
  129.   
  130.   /** 
  131.    * 根據子葉得到父葉 
  132.    * @param child Board 
  133.    * @return Board 
  134.    */  
  135.   public Board getParentByChild(Board child); {  
  136.     String parentId = child.getParentId();;  
  137.     Board parent = (Board); getHibernateTemplate();.load(Board.class, parentId);;  
  138.     return parent;  
  139.   }  
  140.   
  141.   
  142.   /** 
  143.    * 通過子ID得到父葉 
  144.    * @param id String 
  145.    * @return Board 
  146.    */  
  147.   public Board getParentByChildId(String id); {  
  148.     Board child = (Board); getHibernateTemplate();.load(Board.class, id);;  
  149.     Board parent = (Board); getHibernateTemplate();.load(Board.class,child.getParentId(););;  
  150.     return parent;  
  151.   }  
  152. }  


4.做service層代碼

Java代碼
  1. package infoweb.service;  
  2.   
  3. import java.util.List;  
  4. import java.util.Iterator;  
  5. import infoweb.dao.BoardTreeDAOImpl;  
  6. import infoweb.dao.IBoardTreeDAO;  
  7. import infoweb.pojo.Board;  
  8. import infoweb.exception.BoardException;  
  9. import net.sf.hibernate.HibernateException;  
  10.   
  11. /** 
  12.  * <p>Title: </p> 
  13.  * <p>Description: </p> 
  14.  * <p>Copyright: Copyright (c); 2004</p> 
  15.  * <p>Company: </p> 
  16.  * @author 段洪傑 
  17.  * @version 1.0 
  18.  */  
  19. public class BoardServiceSpringImpl implements IBoardService {  
  20.   
  21.     private IBoardTreeDAO boardTreeDAO;  
  22.   
  23.     public BoardServiceSpringImpl(); {  
  24.         super();;  
  25.     }  
  26.   
  27.     /** 
  28.      * 取所有roots版塊 
  29.      * @return Iterator 
  30.      */  
  31.     public Iterator getRoots(); throws BoardException {  
  32.         Iterator roots = null;  
  33.         try {  
  34.             roots = boardTreeDAO.getRoots();;  
  35.         } catch (Exception ex); {  
  36.             throw new BoardException("取ROOT版塊時出錯! " + ex.toString(););;  
  37.         }  
  38.         return roots;  
  39.     }  
  40.   
  41.     /** 
  42.      * 增加Root新版塊 
  43.      * @param board Board 
  44.      */  
  45.     public void setRoot(Board board); throws BoardException {  
  46.         try {  
  47.             boardTreeDAO.setRoot(board);;  
  48.         } catch (Exception ex); {  
  49.             throw new BoardException("增加ROOT版塊時出錯! " + ex.toString(););;  
  50.         }  
  51.     }  
  52.   
  53.     /** 
  54.      * 刪除版塊 (包含下級版塊); 
  55.      * @param board Board 
  56.      */  
  57.     public void removeBoard(Board board); throws BoardException {  
  58.         try {  
  59.             boardTreeDAO.deleteBranch(board);;  
  60.         } catch (Exception ex); {  
  61.             throw new BoardException("刪除版塊時出錯! " + ex.toString(););;  
  62.         }  
  63.     }  
  64.   
  65.     /** 
  66.      * 
  67.      * @return IBoardTreeDAO 
  68.      */  
  69.     public IBoardTreeDAO getBoardTreeDAO(); {  
  70.         return boardTreeDAO;  
  71.     }  
  72.   
  73.     /** 
  74.      * 
  75.      * @param boardTreeDAO IBoardTreeDAO 
  76.      */  
  77.     public void setBoardTreeDAO(IBoardTreeDAO boardTreeDAO); {  
  78.         this.boardTreeDAO = boardTreeDAO;  
  79.     }  
  80.   
  81. }  


5.做ACTION的父類

Java代碼
  1. package infoweb.web;  
  2.   
  3.   
  4. import javax.servlet.ServletContext;  
  5. import org.apache.struts.action.Action;  
  6. import org.apache.struts.action.ActionServlet;  
  7. import org.springframework.web.context.WebApplicationContext;  
  8. import org.springframework.web.context.support.WebApplicationContextUtils;  
  9.   
  10. import infoweb.service.IBoardService;  
  11.   
  12.   
  13. /** 
  14.  * <p>Title: </p> 
  15.  * <p>Description: </p> 
  16.  * <p>Copyright: Copyright (c); 2004</p> 
  17.  * <p>Company: </p> 
  18.  * @author 段洪傑 
  19.  * @version 1.0 
  20.  */  
  21.   
  22. public class BaseAction extends Action {  
  23.   
  24.   private IBoardService boardService;  
  25.   
  26.   public void setServlet(ActionServlet actionServlet); {  
  27.     super.setServlet(actionServlet);;  
  28.     ServletContext servletContext = actionServlet.getServletContext();;  
  29.     WebApplicationContext wac =  
  30.         WebApplicationContextUtils.getRequiredWebApplicationContext(  
  31.         servletContext);;  
  32.     this.boardService = (IBoardService); wac.getBean("boardService");;  
  33.   }  
  34.   
  35.   protected IBoardService getBoardService(); {  
  36.     return boardService;  
  37.   }  
  38.   
  39. }  


6.做action類
Java代碼
  1. package infoweb.web;  
  2.   
  3. import infoweb.pojo.Board;  
  4. import org.apache.commons.beanutils.PropertyUtils;  
  5. import org.apache.struts.action.*;  
  6. import org.apache.log4j.Logger;  
  7. import javax.servlet.http.*;  
  8. import java.util.Iterator;  
  9. import java.util.Date;  
  10.   
  11. /** 
  12.  * <p>Title: </p> 
  13.  * <p>Description: </p> 
  14.  * <p>Copyright: Copyright (c); 2004</p> 
  15.  * <p>Company: </p> 
  16.  * @author 段洪傑 
  17.  * @version 1.0 
  18.  */  
  19.   
  20.   
  21. public class SetBoardAction extends BaseAction {  
  22.   
  23.     private static Logger log = Logger.getLogger(SetBoardAction.class);;  
  24.   
  25.     public ActionForward execute(ActionMapping actionMapping,  
  26.                                  ActionForm actionForm,  
  27.                                  HttpServletRequest httpServletRequest,  
  28.                                  HttpServletResponse httpServletResponse); throws  
  29.             Exception {  
  30.   
  31.         // SessionBean sessionBean = (SessionBean); httpServletRequest.getSession();.getAttribute("sessionBean");;  
  32.         BoardForm boardForm = (BoardForm); actionForm;  
  33.         //String backURL = httpServletRequest.getHeader("Referer");;  
  34.         /* 
  35.         if (sessionBean==null||!sessionBean.getIsLogon();); { 
  36.             httpServletRequest.setAttribute("message", "系統超時,或者沒有登錄 .返回重新登錄!");; 
  37.             httpServletRequest.setAttribute("locationFile", 
  38.                                             "location='index.jsp';");; 
  39.             return actionMapping.findForward("message");; 
  40.         } 
  41.         */  
  42.         Board board = new Board();;  
  43.         boardForm.setCreateDate(new Date(););;  
  44.         PropertyUtils.copyProperties(board, boardForm);;  
  45.         getBoardService();.setRoot(board);;  
  46.   
  47.         httpServletRequest.setAttribute("message""版塊信息錄入完成!");;  
  48.         httpServletRequest.setAttribute("locationFile",  
  49.                                         "<A HREF=\"javascript:history.back();\">返回</A>");;  
  50.         return (actionMapping.findForward("success"););;  
  51.     }  
  52.   
  53. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章