学习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. }  
package infoweb.dao;
import java.util.List;
import java.util.Iterator;
import infoweb.pojo.Board;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import org.springframework.orm.hibernate.HibernateCallback;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
/**
 * <p>Title: 版块分类DAOImpl</p>
 * <p>Description: 用树型结构实现</p>
 * <p>Copyright: Copyright (c); 2004</p>
 * <p>Company: </p>
 * @author 段洪杰
 * @version 1.0
 */
public class BoardTreeDAOImpl extends HibernateDaoSupport implements
    IBoardTreeDAO {
  /**
   * 构造函数
   */
  public BoardTreeDAOImpl(); {
    super();;
  }
  /**
   * 通过ID取得版块
   * @param id String
   * @return Board
   */
  public Board getBoardById(String id); {
    Board board = (Board); getHibernateTemplate();.load(Board.class, id);;
    return board;
  }
  /**
   * 取根叶
   * @return Iterator
   */
  public Iterator getRoots(); throws HibernateException {
    String queryString =
        "select board from Board as board where board.parentId='root' order by board.id desc";
    List roots = getHibernateTemplate();.find(queryString);;
    return roots.iterator();;
  }
  /**
   * 存根叶
   * @param board Board
   */
  public void setRoot(Board board); {
    board.setParentId("root");;
    getHibernateTemplate();.save(board);;
  }
  /**
   * 取子叶
   * @param  parentid String
   * @return List
   */
  public Iterator getChildren(String parentid); {
    /*
         String queryString =
     "select board as Board where board.parent_id='parentid' order by board.id desc";
         List children = getHibernateTemplate();.find(queryString);;
         return children;
     */
    Board parent = (Board); getHibernateTemplate();.load(Board.class, parentid);;
    return parent.getChildren();.iterator();;
  }
  /**
   * 取子叶数
   * @param parentid String
   * @return int
   */
  public int getChildrenCount(String parentid); {
    /*
         String queryString =
     "select count(*); Board where board.parent_id='parentid' order by board.id desc";
         List children = getHibernateTemplate();.find(queryString);;
         int count = ((Integer); children.iterator();.next(););.intValue();;
         return count;
     */
    Board parent = (Board); getHibernateTemplate();.load(Board.class, parentid);;
    int count = parent.getChildren();.size();;
    return count;
  }
  /**
   * 存子叶
   * @param parentLeaf Leaf
   */
  public void setChild(Board board, String parentid); {
    board.setParentId(parentid);;
    getHibernateTemplate();.save(board);;
  }
  /**
   *
   * 删除该叶和它的子叶
   * @param board Board
   */
  public void deleteBranch(Board board); {
      getHibernateTemplate();.delete(board);;
  }
  /**
   * 根据子叶得到父叶
   * @param child Board
   * @return Board
   */
  public Board getParentByChild(Board child); {
    String parentId = child.getParentId();;
    Board parent = (Board); getHibernateTemplate();.load(Board.class, parentId);;
    return parent;
  }
  /**
   * 通过子ID得到父叶
   * @param id String
   * @return Board
   */
  public Board getParentByChildId(String id); {
    Board child = (Board); getHibernateTemplate();.load(Board.class, id);;
    Board parent = (Board); getHibernateTemplate();.load(Board.class,child.getParentId(););;
    return parent;
  }
}


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. }  
package infoweb.service;
import java.util.List;
import java.util.Iterator;
import infoweb.dao.BoardTreeDAOImpl;
import infoweb.dao.IBoardTreeDAO;
import infoweb.pojo.Board;
import infoweb.exception.BoardException;
import net.sf.hibernate.HibernateException;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c); 2004</p>
 * <p>Company: </p>
 * @author 段洪杰
 * @version 1.0
 */
public class BoardServiceSpringImpl implements IBoardService {
    private IBoardTreeDAO boardTreeDAO;
    public BoardServiceSpringImpl(); {
        super();;
    }
    /**
     * 取所有roots版块
     * @return Iterator
     */
    public Iterator getRoots(); throws BoardException {
        Iterator roots = null;
        try {
            roots = boardTreeDAO.getRoots();;
        } catch (Exception ex); {
            throw new BoardException("取ROOT版块时出错! " + ex.toString(););;
        }
        return roots;
    }
    /**
     * 增加Root新版块
     * @param board Board
     */
    public void setRoot(Board board); throws BoardException {
        try {
            boardTreeDAO.setRoot(board);;
        } catch (Exception ex); {
            throw new BoardException("增加ROOT版块时出错! " + ex.toString(););;
        }
    }
    /**
     * 删除版块 (包含下级版块);
     * @param board Board
     */
    public void removeBoard(Board board); throws BoardException {
        try {
            boardTreeDAO.deleteBranch(board);;
        } catch (Exception ex); {
            throw new BoardException("删除版块时出错! " + ex.toString(););;
        }
    }
    /**
     *
     * @return IBoardTreeDAO
     */
    public IBoardTreeDAO getBoardTreeDAO(); {
        return boardTreeDAO;
    }
    /**
     *
     * @param boardTreeDAO IBoardTreeDAO
     */
    public void setBoardTreeDAO(IBoardTreeDAO boardTreeDAO); {
        this.boardTreeDAO = boardTreeDAO;
    }
}


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. }  
package infoweb.web;
import javax.servlet.ServletContext;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionServlet;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import infoweb.service.IBoardService;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c); 2004</p>
 * <p>Company: </p>
 * @author 段洪杰
 * @version 1.0
 */
public class BaseAction extends Action {
  private IBoardService boardService;
  public void setServlet(ActionServlet actionServlet); {
    super.setServlet(actionServlet);;
    ServletContext servletContext = actionServlet.getServletContext();;
    WebApplicationContext wac =
        WebApplicationContextUtils.getRequiredWebApplicationContext(
        servletContext);;
    this.boardService = (IBoardService); wac.getBean("boardService");;
  }
  protected IBoardService getBoardService(); {
    return boardService;
  }
}


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. }  

你可能感兴趣的:(Hibernate,职场,休闲)