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

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

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

    Topquan's Blog

    分享價(jià)值----成就你我----我的博客----你的家

    利用Hibernate開(kāi)發(fā)Blog實(shí)例分析

    開(kāi)發(fā)工具采用 MYECLIPS3.6 ,首先是建立項(xiàng)目,導(dǎo)入 STRUTS+HIBERNATE 包,然后配置 SRC 跟目錄下的 Hibernate.cfg.xml. 我采用的是 MYSQL數(shù)據(jù)庫(kù) ,所以配置如下:
      
       <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 所對(duì)應(yīng)的映射
      
      下面我們繼續(xù) HIBERNATE 程序的下步編寫(xiě):
      
       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()
       {
       }
       }
      
      創(chuàng)建 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 基本配置,對(duì) 數(shù)據(jù)庫(kù) 操作采用 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>
      
      下面我們定義服務(wù)層:
      
       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) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Hibernate

    主站蜘蛛池模板: 无码av免费网站| 日本视频一区在线观看免费| 中文字幕免费在线观看| 拍拍拍又黄又爽无挡视频免费| 亚洲精品专区在线观看| 亚洲视频在线观看地址| 亚洲AV无码男人的天堂| 国产婷婷成人久久Av免费高清| 女人张腿给男人桶视频免费版| 亚洲色偷偷综合亚洲AV伊人| 亚洲乱码一二三四区麻豆| 一级做a爰片久久毛片免费陪 | 亚洲国产精品无码专区| 亚洲成_人网站图片| 精品无码国产污污污免费网站国产| 57PAO成人国产永久免费视频| 2022中文字字幕久亚洲| 亚洲AV无码一区二区三区牛牛| 国产做国产爱免费视频| 好男人看视频免费2019中文 | 四虎国产精品永久免费网址| 国产a不卡片精品免费观看| 亚洲一区二区成人| 无遮挡呻吟娇喘视频免费播放| 无码精品A∨在线观看免费| 国产日产亚洲系列| 亚洲AV无码国产剧情| 亚洲网站在线免费观看| 红杏亚洲影院一区二区三区| 亚洲乱妇老熟女爽到高潮的片 | 18禁美女裸体免费网站 | 亚洲成a人无码亚洲成www牛牛| 无码国产精品一区二区免费模式| 亚洲成年人啊啊aa在线观看| 亚洲三级高清免费| 91麻豆国产免费观看| 亚洲线精品一区二区三区| 日韩欧美亚洲国产精品字幕久久久| 最近中文字幕完整免费视频ww| 亚洲午夜久久久影院伊人| 免费无遮挡无遮羞在线看|