麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

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

2019-11-18 12:56:41
字體:
來源:轉載
供稿:網友

  開發工具采用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  tonny * @created
  2004年2月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(

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产乱色精品成人免费视频 | 精品久久久久久久久久久久久久久久久久久 | 精品人伦一区二区三区蜜桃网站 | 国产精品久久久久久影院8一贰佰 | 成人高清网站 | 欧美自拍三区 | 情侣啪啪网站 | 在线播放h | 精品一区二区在线视频 | 线观看免费完整aaa 欧美在线一级 | 国产在线精品一区二区不卡 | 精品国产一区二区三区蜜殿 | 成人毛片网站 | 高清国产午夜精品久久久久久 | 午夜精品成人一区二区 | julieann艳星激情办公室 | 91久久国产露脸精品国产 | 日本欧美一区二区三区在线观看 | 久久综合久久综合久久综合 | avav在线播放| 女人叉开腿让男人桶 | 国产精选电影免费在线观看 | 99精品视频在线免费观看 | a级毛片免费观看在线播放 日本aaa一级片 | 久久久久久久久亚洲精品 | 最新欧美精品一区二区三区 | 亚洲视频在线观看免费视频 | 一区二区三区日韩电影 | 国产一区二区三区精品在线观看 | 最新黄色电影网站 | 欧美三级欧美成人高清www | a视频在线免费观看 | 日韩激情 | 久久久无码精品亚洲日韩按摩 | 日本欧美一区二区三区在线观看 | 九九热视频这里只有精品 | 国产精品视频一区二区三区四 | 久久久久国产精品久久久久 | 爱草在线 | 2021国产精品 | 免费专区 - 91爱爱 |