[转]Struts+spring+hibernate学习笔记

原帖:http://www.iteye.com/article/7925 

Struts+spring+hibernate学习笔记

(快刀浪子原创)下面是我学习Struts+spring+hibernate的笔记.

1.WEB.XML的配置:
首先在web.xml中加入:

代码
  1. <servlet>  
  2.   <servlet-name>SpringContextServletservlet-name>  
  3.   <servlet-class>org.springframework.web.context.ContextLoaderServletservlet-class>  
  4.   <load-on-startup>1load-on-startup>  
  5. servlet>  

<script>render_code();</script>

我的整个web.xml象这样:

代码
  1. <!---->xml version="1.0" encoding="UTF-8"?>  
  2. <!---->>  
  3. <web-app>  
  4.   <display-name>info_webdisplay-name>  
  5.   <context-param>  
  6.     <param-name>contextConfigLocationparam-name>  
  7.     <param-value>/WEB-INF/applicationContext-hibernate.xmlparam-value>  
  8.   context-param>  
  9.   <filter>  
  10.     <filter-name>SetCharacterEncodingfilter-name>  
  11.     <filter-class>iclass.util.SetEncodingFilterfilter-class>  
  12.     <init-param>  
  13.       <param-name>encodingparam-name>  
  14.       <param-value>GBKparam-value>  
  15.     init-param>  
  16.   filter>  
  17.   <filter>  
  18.     <filter-name>FilterRedirectorfilter-name>  
  19.     <filter-class>org.apache.cactus.server.FilterTestRedirectorfilter-class>  
  20.   filter>  
  21.   <filter-mapping>  
  22.     <filter-name>SetCharacterEncodingfilter-name>  
  23.     <url-pattern>/*url-pattern>  
  24.   filter-mapping>  
  25.   <filter-mapping>  
  26.     <filter-name>FilterRedirectorfilter-name>  
  27.     <url-pattern>/FilterRedirectorurl-pattern>  
  28.   filter-mapping>  
  29.   <servlet>  
  30.     <servlet-name>SpringContextServletservlet-name>  
  31.     <servlet-class>org.springframework.web.context.ContextLoaderServletservlet-class>  
  32.     <load-on-startup>1load-on-startup>  
  33.   servlet>  
  34.   <servlet>  
  35.     <servlet-name>ServletRedirectorservlet-name>  
  36.     <servlet-class>org.apache.cactus.server.ServletTestRedirectorservlet-class>  
  37.   servlet>  
  38.   <servlet>  
  39.     <servlet-name>JspRedirectorservlet-name>  
  40.     <jsp-file>/jspRedirector.jspjsp-file>  
  41.   servlet>  
  42.   <servlet>  
  43.     <servlet-name>actionservlet-name>  
  44.     <servlet-class>org.apache.struts.action.ActionServletservlet-class>  
  45.     <init-param>  
  46.       <param-name>configparam-name>  
  47.       <param-value>/WEB-INF/struts-config.xmlparam-value>  
  48.     init-param>  
  49.     <init-param>  
  50.       <param-name>debugparam-name>  
  51.       <param-value>2param-value>  
  52.     init-param>  
  53.     <init-param>  
  54.       <param-name>applicationparam-name>  
  55.       <param-value>ApplicationResourcesparam-value>  
  56.     init-param>  
  57.     <load-on-startup>2load-on-startup>  
  58.   servlet>  
  59.   <servlet-mapping>  
  60.     <servlet-name>ServletRedirectorservlet-name>  
  61.     <url-pattern>/ServletRedirectorurl-pattern>  
  62.   servlet-mapping>  
  63.   <servlet-mapping>  
  64.     <servlet-name>JspRedirectorservlet-name>  
  65.     <url-pattern>/JspRedirectorurl-pattern>  
  66.   servlet-mapping>  
  67.   <servlet-mapping>  
  68.     <servlet-name>actionservlet-name>  
  69.     <url-pattern>*.dourl-pattern>  
  70.   servlet-mapping>  
  71.   <welcome-file-list>  
  72.     <welcome-file>index.jspwelcome-file>  
  73.   welcome-file-list>  
  74.   <taglib>  
  75.     <taglib-uri>/tags/struts-nestedtaglib-uri>  
  76.     <taglib-location>/WEB-INF/struts-nested.tldtaglib-location>  
  77.   taglib>  
  78.   <taglib>  
  79.     <taglib-uri>/WEB-INF/struts-bean.tldtaglib-uri>  
  80.     <taglib-location>/WEB-INF/struts-bean.tldtaglib-location>  
  81.   taglib>  
  82.   <taglib>  
  83.     <taglib-uri>/WEB-INF/struts-html.tldtaglib-uri>  
  84.     <taglib-location>/WEB-INF/struts-html.tldtaglib-location>  
  85.   taglib>  
  86.   <taglib>  
  87.     <taglib-uri>/WEB-INF/struts-logic.tldtaglib-uri>  
  88.     <taglib-location>/WEB-INF/struts-logic.tldtaglib-location>  
  89.   taglib>  
  90.   <taglib>  
  91.     <taglib-uri>/WEB-INF/struts-tiles.tldtaglib-uri>  
  92.     <taglib-location>/WEB-INF/struts-tiles.tldtaglib-location>  
  93.   taglib>  
  94.   <taglib>  
  95.     <taglib-uri>/WEB-INF/struts-nested.tldtaglib-uri>  
  96.     <taglib-location>/WEB-INF/struts-nested.tldtaglib-location>  
  97.   taglib>  
  98. web-app>  

<script>render_code();</script>

 

2.spring配置
配置文件为applicationContext-hibernate.xml,在此配置文件中,配了POJO层,商业逻辑层,DAO层,和事务管理

代码
  1. <!---->xml version="1.0" encoding="UTF-8"?>  
  2. <!---->>  
  3.   
  4. <beans>    
  5.     <!---->     
  6.        
  7.     <!---->  
  8.   
  9.     <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  10.         <property name="driverClassName">  
  11.             <value>org.gjt.mm.mysql.Drivervalue>  
  12.         property>  
  13.         <property name="url">  
  14.             <value>jdbc:mysql://localhost:3306/info_web?useUnicode=true&characterEncoding=GBKvalue>  
  15.         property>  
  16.         <property name="username">  
  17.             <value>rootvalue>  
  18.         property>  
  19.         <property name="password">  
  20.             <value>123456value>  
  21.         property>  
  22.     bean>  
  23.        
  24.     <bean id="mySessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">  
  25.         <property name="mappingResources">  
  26.             <list>  
  27.                 <value>infoweb/pojo/Answer.hbm.xmlvalue>  
  28.                 <value>infoweb/pojo/Board.hbm.xmlvalue>  
  29.                 <value>infoweb/pojo/Image.hbm.xmlvalue>  
  30.                 <value>infoweb/pojo/Info.hbm.xmlvalue>  
  31.             list>  
  32.         property>        
  33.            
  34.         <property name="hibernateProperties">  
  35.             <props>  
  36.                 <prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialectprop>  
  37.                 <prop key="hibernate.show_sql">trueprop>  
  38.                 <prop key="hibernate.cglib.use_reflection_optimizer">trueprop>  
  39.             props>  
  40.         property>    
  41.   
  42.         <property name="dataSource"><ref bean="myDataSource"/>property>  
  43.     bean>  
  44.   
  45.     <!---->  
  46.     <bean id="myTransactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">  
  47.         <property name="sessionFactory"><ref local="mySessionFactory"/>property>  
  48.     bean>  
  49.        
  50.   
  51.     <!---->  
  52.     <bean id="boardService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">       
  53.         <property name="transactionManager"><ref local="myTransactionManager"/>property>  
  54.         <property name="target"><ref local="boardServiceSpring"/>property>  
  55.         <property name="transactionAttributes">  
  56.             <props>  
  57.                 <prop key="get*">PROPAGATION_REQUIRED,readOnly,-BoardExceptionprop>  
  58.                 <prop key="set*">PROPAGATION_REQUIRED,-BoardExceptionprop>  
  59.                 <prop key="modify*">PROPAGATION_REQUIRED,-BoardExceptionprop>  
  60.                 <prop key="remove*">PROPAGATION_REQUIRED,-BoardExceptionprop>  
  61.             props>  
  62.         property>  
  63.     bean>  
  64.   
  65.     <!---->  
  66.     <bean id="boardServiceSpring" class="infoweb.service.BoardServiceSpringImpl">  
  67.         <property name="boardTreeDAO"><ref local="boardTreeDAO"/>property>  
  68.     bean>  
  69.        
  70.   
  71.     <!---->  
  72.     <bean id="boardTreeDAO" class="infoweb.dao.BoardTreeDAOImpl">  
  73.         <property name="sessionFactory"><ref local="mySessionFactory"/>property>  
  74.     bean>  
  75.   
  76.     <!---->  
  77.     <bean id="infoDAO" class="infoweb.dao.InfoDAOImpl">  
  78.         <property name="sessionFactory"><ref local="mySessionFactory"/>property>  
  79.     bean>  
  80. beans>  
  81.   

<script>render_code();</script>

 

3.做DAO代码,

代码
  1. package infoweb.dao;   
  2.   
  3. import java.util.List;   
  4. import java.util.Iterator;   
  5.   
  6. import infoweb.pojo.Board;   
  7.   
  8. import net.sf.hibernate.HibernateException;   
  9. import net.sf.hibernate.Query;   
  10. import net.sf.hibernate.Session;   
  11.   
  12. import org.springframework.orm.hibernate.HibernateCallback;   
  13. import org.springframework.orm.hibernate.support.HibernateDaoSupport;   
  14.   
  15. /**   
  16.  * <p>Title: 版块分类DAOImplp>  
  17.  * <p>Description: 用树型结构实现p>  
  18.  * <p>Copyright: Copyright (c) 2004p>  
  19.  * <p>Company: p>  
  20.  * @author 段洪杰   
  21.  * @version 1.0   
  22.  */   
  23.   
  24. public class BoardTreeDAOImpl extends HibernateDaoSupport implements   
  25.     IBoardTreeDAO {   
  26.   
  27.   /**   
  28.    * 构造函数   
  29.    */   
  30.   public BoardTreeDAOImpl() {   
  31.     super();   
  32.   }   
  33.   
  34.   /**   
  35.    * 通过ID取得版块   
  36.    * @param id String   
  37.    * @return Board   
  38.    */   
  39.   
  40.   public Board getBoardById(String id) {   
  41.     Board board = (Board) getHibernateTemplate().load(Board.class, id);   
  42.     return board;   
  43.   }   
  44.   
  45.   /**   
  46.    * 取根叶   
  47.    * @return Iterator   
  48.    */   
  49.   public Iterator getRoots() throws HibernateException {   
  50.     String queryString =   
  51.         "select board from Board as board where board.parentId='root' order by board.id desc";   
  52.     List roots = getHibernateTemplate().find(queryString);   
  53.     return roots.iterator();   
  54.   }   
  55.   
  56.   /**   
  57.    * 存根叶   
  58.    * @param board Board   
  59.    */   
  60.   public void setRoot(Board board) {   
  61.     board.setParentId("root");   
  62.     getHibernateTemplate().save(board);   
  63.   }   
  64.   
  65.   /**   
  66.    * 取子叶   
  67.    * @param  parentid String   
  68.    * @return List   
  69.    */   
  70.   public Iterator getChildren(String parentid) {   
  71.     /*   
  72.          String queryString =   
  73.      "select board as Board where board.parent_id='parentid' order by board.id desc";   
  74.          List children = getHibernateTemplate().find(queryString);   
  75.          return children;   
  76.      */   
  77.     Board parent = (Board) getHibernateTemplate().load(Board.class, parentid);   
  78.     return parent.getChildren().iterator();   
  79.   }   
  80.   
  81.   /**   
  82.    * 取子叶数   
  83.    * @param parentid String   
  84.    * @return int   
  85.    */   
  86.   
  87.   public int getChildrenCount(String parentid) {   
  88.     /*   
  89.          String queryString =   
  90.      "select count(*) Board where board.parent_id='parentid' order by board.id desc";   
  91.          List children = getHibernateTemplate().find(queryString);   
  92.          int count = ((Integer) children.iterator().next()).intValue();   
  93.          return count;   
  94.      */   
  95.     Board parent = (Board) getHibernateTemplate().load(Board.class, parentid);   
  96.     int count = parent.getChildren().size();   
  97.     return count;   
  98.   }   
  99.   
  100.   /**   
  101.    * 存子叶   
  102.    * @param parentLeaf Leaf   
  103.    */   
  104.   public void setChild(Board board, String parentid) {   
  105.     board.setParentId(parentid);   
  106.     getHibernateTemplate().save(board);   
  107.   }   
  108.   
  109.   /**   
  110.    *   
  111.    * 删除该叶和它的子叶   
  112.    * @param board Board   
  113.    */   
  114.   public void deleteBranch(Board board) {   
  115.       getHibernateTemplate().delete(board);   
  116.   }   
  117.   
  118.   /**   
  119.    * 根据子叶得到父叶   
  120.    * @param child Board   
  121.    * @return Board   
  122.    */   
  123.   public Board getParentByChild(Board child) {   
  124.     String parentId = child.getParentId();    <li class="al

你可能感兴趣的:(spring,bean,Hibernate,struts,servlet)