熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

怎樣利用Hibernate開發Blog實例分析

2013-11-23 20:20:08  來源: Java開源技術 

  開發工具采用MYECLIPS首先是建立項目導入STRUTS+HIBERNATE包然後配置SRC跟目錄下的Hibernatecfgxml我采用的是MYSQL數據庫所以配置如下
  
  <hibernateconfiguration>
  <sessionfactory>
  <! properties >
  <property name=connectionusername>
  root
  </property>
  <property name=connectionurl>
  jdbc:mysql://localhost:/tonnyblog
  </property>
  <property name=dialect>
  netsfhibernatedialectMySQLDialect
  </property>
  <property name=connectionpassword></property>
  <property name=connectiondriver_class>
  orggjtmmmysqlDriver
  </property>
  <! mapping files >
  <mapping resource=com/tonny/blog/bean/Userhbmxml/>
  <mapping resource=com/tonny/blog/bean/Itemhbmxml/>
  <mapping resource=com/tonny/blog/bean/Reviewhbmxml/>
  </sessionfactory></hibernateconfiguration>
  
  mapping為JAVABEAN所對應的映射
  
  下面我們繼續HIBERNATE程序的下步編寫
  
  import netsfhibernateHibernateException;
  import netsfhibernateSession;
  import netsfhibernateSessionFactory;
  import netsfhibernatecfgConfiguration;
  /** * Description of the Class * *
  @author  tonny * @created
  
  */public class HibernateUtil
  {
  private final static SessionFactory sessionFactory;
  static
  {
  try
  {
  sessionFactory =
  new Configuration(nfigure()buildSessionFactory();
  }
  catch (HibernateException ex)
  {
  throw new RuntimeException(
  Exception building SessionFactory:
   + exgetMessage()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) sessionget();
  if (s == null)
  {
  s = sessionFactoryopenSession();
  sessionset(s);
  }    return s;
  }
  /**   * Description of the Method
  *   * @exception HibernateException
  Description of the Exception   */
  public static void closeSession()
  throws HibernateException {
  Session s = (Session) sessionget();
  sessionset(null);
  if (s != null)
  {
  sclose();
  }
  }
  public static void init()
  {
  }
  }
  
  創建sessionFactory
  
  import netsfhibernateHibernateException;
  import netsfhibernateSessionFactory;
  import netsfhibernatecfgConfiguration;
  import orgapachestrutsactionActionServlet;
  import orgapachestrutsactionPlugIn;
  import onfigModuleConfig;
  import comtonnyblogdaohibernateHibernateUtil;
  public class HibernatePlugin
  implements orgapachestrutsactionPlugIn
  {
  public void init(ActionServlet servlet
  ModuleConfig config)
  {
  HibernateUtilinit();
  }
  public void destroy()
  {
  try
  {
  HibernateUtilcloseSession();
  }
  catch(HibernateException hex)
  {
  hexprintStackTrace();
  }
  }
  }
  
  以上為HIBERNATE基本配置對數據庫操作采用DAO模式增加配置如下
  
  import comtonnyblogdaohibernate*;
  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();
  }
  }
  
  strutsxml增加配置
  
  <controller contentType=text/html
  debug= locale=true
  nocache=true
  processorClass=
  comtntrollerIndexRequestProcessor/>
  <messageresources parameter=comtonnyresource/>
  <plugin className=
  comtonnyblogstrutspluginHibernatePlugin/>
  <plugin className=orgapachestrutstilesTilesPlugin>
  <setproperty property=moduleAware value=true/>
  <setproperty property=definitionsdebug value=/>
  <setproperty property=definitionsparserdetails
  value=/>
  <setproperty property=definitionsparservalidate
  value=false/>
  <setproperty property=definitionsconfig
  value=/WEBINF/titledefxml/>
  </plugin>
  
  下面我們定義服務層
  
  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 comtonnyblogstrutsform*;
  import comtonnyblogview*;
  import comtonnyblogbean*;
  import javautil*;
  import javaxservlethttp*;
  public interface IService
  {
  public UserContainer login(UserForm userForm);
  public boolean logout(UserContainer userContainer);
  public boolean addBlog(BlogForm blogFormString filePath);
  public boolean removeBlog(Long id);
  public boolean addReview(Long topicIdReviewForm reviewForm);
  public boolean updateBlog(Long idString contenString 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 comtonnyblogstrutsform*;
  import comtonnyblogview*;
  import comtonnyblogdao*;
  import comtonnyblogbean*;
  import javautil*;import javaxservlethttp*;
  import comtonnyblogstrutsutilFileUpload;
  public class ServiceImp implements IService
  {
  public UserContainer login(UserForm userForm)
  {
  UserDAO userDAO=DAOFactorygetInstance()getUserDAO();
  User user=userDAOloadUser(userFormgetName());
  if(user==null)return new UserContainer(false);
  if(!usergetPassword()equals(userFormgetPassword()))
  return new UserContainer(false);
  return new UserContainer(userFormgetName()true);
  }
  public boolean logout(UserContainer userContainer)
  {
  userContainersetLogin(false);
  userContainersetName();
  return true;
  }
  public boolean addBlog(BlogForm blogFormString path)
  {
  ItemDAO itemDAO=DAOFactorygetInstance()getItemDAO();
  Item item=new Item(blogFormgetTopic()
  blogFormgetContent()
  FileUploadupload(blogFormgetFile()path)new Date());
  itemDAOaddItem(item);
  return true;
  }
  public boolean removeBlog(Long id)
  {
  ReviewDAO reviewDAO=DAOFactorygetInstance(
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28319.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.