<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Topquan's Blog

    分享價值----成就你我----我的博客----你的家

    利用Hibernate開發Blog實例分析

    開發工具采用 MYECLIPS3.6 ,首先是建立項目,導入 STRUTS+HIBERNATE 包,然后配置 SRC 跟目錄下的 Hibernate.cfg.xml. 我采用的是 MYSQL數據庫 ,所以配置如下:
      
       <hibernate-configuration>
       <session-factory>
       <!-- properties -->
       <property name="connection.username">
       root
       </property>
       <property name="connection.url">
       jdbc:mysql://localhost:3306/tonnyblog
       </property>
       <property name="dialect">
       net.sf.hibernate.dialect.MySQLDialect
       </property>
       <property name="connection.password"></property>
       <property name="connection.driver_class">
       org.gjt.mm.mysql.Driver
       </property>
       <!-- mapping files -->
       <mapping resource="com/tonny/blog/bean/User.hbm.xml"/>
       <mapping resource="com/tonny/blog/bean/Item.hbm.xml"/>
       <mapping resource="com/tonny/blog/bean/Review.hbm.xml"/>
       </session-factory></hibernate-configuration>
      
       mapping JAVABEAN 所對應的映射
      
      下面我們繼續 HIBERNATE 程序的下步編寫:
      
       import net.sf.hibernate.HibernateException;
       import net.sf.hibernate.Session;
       import net.sf.hibernate.SessionFactory;
       import net.sf.hibernate.cfg.Configuration;
       /**   Description of the Class
            *
    @author  topquan
       
    * @created
    2006 5 6
       */public class HibernateUtil
       {
       private final static SessionFactory sessionFactory;
       static
       {
       try
       {
       sessionFactory =
       new Configuration().configure().buildSessionFactory();
       }
       catch (HibernateException ex)
       {
       throw new RuntimeException(
       "Exception building SessionFactory:
       " + ex.getMessage(),ex);
       }
       }
       private HibernateUtil(){    }
       /**    *   Description of the Field
       */
       private final static ThreadLocal
       session = new ThreadLocal();
       /** Description of the Method
       * @return
      * Description of the Return Value   
      * @exception   HibernateException
      * Description of the Exception   
            */

       public static Session currentSession()
       throws HibernateException
       {
       Session s = (Session) session.get();
       if (s == null)
       {
       s = sessionFactory.openSession();
       session.set(s);
       }      return s;
       }
       /**   
              *
      Description of the Method
        * @exception   HibernateException
       * Description of the Exception
       */
       public static void closeSession()
       throws HibernateException {
       Session s = (Session) session.get();
       session.set(null);
       if (s != null)
       {
       s.close();
       }
       }
       public static void init()
       {
       }
       }
      
      創建 sessionFactory
      
       import net.sf.hibernate.HibernateException;
       import net.sf.hibernate.SessionFactory;

           import net.sf.hibernate.cfg.Configuration;
       import org.apache.struts.action.ActionServlet;
       import org.apache.struts.action.PlugIn;
       import org.apache.struts.config.ModuleConfig;
       import com.tonny.blog.dao.hibernate.HibernateUtil;
       public class HibernatePlugin
       implements org.apache.struts.action.PlugIn
       {
       public void init(ActionServlet servlet,
       ModuleConfig config)
       {
       HibernateUtil.init();
       }
       public void destroy()
       {
       try
       {
       HibernateUtil.closeSession();
       }
       catch(HibernateException hex)
       {
       hex.printStackTrace();
       }
       }
       }
      
      以上為 HIBERNATE 基本配置,對 數據庫 操作采用 DAO 模式,增加配置如下:
      
       import com.tonny.blog.dao.hibernate.*;
       public class DAOFactory
       {
       private static DAOFactory instance;
       public synchronized static DAOFactory getInstance()
       {
       if
       (instance == null)
       {
       instance = new DAOFactory();
       }
       return instance;
       }
       private DAOFactory()
       {
       }
       public ItemDAO getItemDAO()
       {
       return new ItemDAOHibernate();
       }
       public ReviewDAO getReviewDAO()
       {
       return new ReviewDAOHibernate();
       }
       public UserDAO getUserDAO()
       {
       return new UserDAOHibernate();
       }
       }
      
       struts.xml 增加配置:
      
       <controller contentType="text/html"
       debug="3" locale="true"
       nocache="true"
       processorClass=
       "com.tonny.blog.struts.controller.IndexRequestProcessor"/>
       <message-resources parameter="com.tonny.resource"/>
       <plug-in className=
       "com.tonny.blog.struts.plugin.HibernatePlugin"/>
       <plug-in className="org.apache.struts.tiles.TilesPlugin">
       <set-property property="moduleAware" value="true"/>
       <set-property property="definitions-debug" value="0"/>
       <set-property property="definitions-parser-details"
       value="0"/>
       <set-property property="definitions-parser-validate"
       value="false"/>
       <set-property property="definitions-config"
       value="/WEB-INF/title-def.xml"/>
       </plug-in>
      
      下面我們定義服務層:
      
       public class ServiceFactory
       {
       private static ServiceFactory instance;
       public synchronized static ServiceFactory getInstance()
       {
       if (instance == null)
       {
       instance = new ServiceFactory();
       }
       return instance;
       }
       private ServiceFactory()
       {
       }
       public
       IService getService()
       {
       return new ServiceImp();
       }
       }
      
       import com.tonny.blog.struts.form.*;
       import com.tonny.blog.view.*;
       import com.tonny.blog.bean.*;
       import java.util.*;

           import javax.servlet.http.*;
       public interface IService
       {
       public UserContainer login(UserForm userForm);
       public boolean logout(UserContainer userContainer);
       public boolean addBlog(BlogForm blogForm,String filePath);
       public boolean removeBlog(Long id);
       public boolean addReview(Long topicId,ReviewForm reviewForm);
       public boolean updateBlog(Long id,String conten,String topic);
       public boolean removeReview(Long id);
       public List getItems();
       public ItemView getItem(Long id);
       public ItemView getEditItem(Long id);
       public List search(SearchForm searchForm);
       /**    * @param id    * @param userForm    */
       public boolean addUser(UserForm userForm);
       }
      
       import com.tonny.blog.struts.form.*;
       import com.tonny.blog.view.*;
       import com.tonny.blog.dao.*;
       import com.tonny.blog.bean.*;
       import java.util.*;import javax.servlet.http.*;
       import com.tonny.blog.struts.util.FileUpload;
       public class ServiceImp implements IService
       {
       public UserContainer login(UserForm userForm)
       {
       UserDAO userDAO=DAOFactory.getInstance().getUserDAO();
       User user=userDAO.loadUser(userForm.getName());
       if(user==null)return new UserContainer("",false);
       if(!user.getPassword().equals(userForm.getPassword()))
       return new UserContainer("",false);
       return new UserContainer(userForm.getName(),true);
       }
       public boolean logout(UserContainer userContainer)
       {
       userContainer.setLogin(false);
       userContainer.setName("");
       return true;
       }
       public boolean addBlog(BlogForm blogForm,String path)
       {
       ItemDAO itemDAO=DAOFactory.getInstance().getItemDAO();
       Item item=new Item(blogForm.getTopic(),
       blogForm.getContent(),
       FileUpload.upload(blogForm.getFile(),path),new Date());
       itemDAO.addItem(item);
       return true;
       }
       public boolean removeBlog(Long id)
       {
       ReviewDAO reviewDAO=DAOFactory.getInstance()
            //...
           }

    posted on 2006-05-08 15:39 topquan 閱讀(288) 評論(0)  編輯  收藏 所屬分類: Hibernate

    主站蜘蛛池模板: 免费人成网站在线播放| 久久精品国产精品亚洲艾草网| 美女一级毛片免费观看| 亚洲无人区午夜福利码高清完整版| 一区二区三区观看免费中文视频在线播放| 亚洲国产综合自在线另类| 免费在线观看黄色毛片| 亚洲电影免费在线观看| 久久亚洲中文无码咪咪爱| 亚洲乱亚洲乱妇无码麻豆| 成人黄动漫画免费网站视频| 国产在线播放线91免费| 国内精品久久久久影院亚洲| 亚洲色成人WWW永久网站| 女人18毛片水最多免费观看| 永久免费av无码入口国语片| 亚洲AV无码AV日韩AV网站| 亚洲国产天堂久久综合网站 | 成人永久免费高清| 久久国产精品免费专区| 精品视频免费在线| 亚洲免费视频播放| 久久精品国产69国产精品亚洲| 四虎影院在线免费播放| 在线播放免费人成毛片乱码| 免费精品视频在线| 国产AV旡码专区亚洲AV苍井空| 亚洲AV无码精品色午夜果冻不卡| 四虎影在线永久免费观看| 又粗又大又黑又长的免费视频| 国产永久免费高清在线| 理论片在线观看免费| 亚洲综合av一区二区三区不卡| 亚洲综合区图片小说区| 亚洲日韩精品A∨片无码| 亚洲AⅤ无码一区二区三区在线| 在线观看成人免费视频| 四虎永久在线观看免费网站网址 | 免费亚洲视频在线观看| 亚洲综合小说另类图片动图| 亚洲熟妇色自偷自拍另类|