实例讲解如何利用Hibernate开发Blog

2016-02-19 20:29 1 1 收藏

今天图老师小编给大家展示的是实例讲解如何利用Hibernate开发Blog,精心挑选的内容希望大家多多支持、多多分享,喜欢就赶紧get哦!

【 tulaoshi.com - 编程语言 】

首先我们需要建立项目(开发工具采用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(

来源:https://www.tulaoshi.com/n/20160219/1623856.html

延伸阅读
标签: Java JAVA基础
环境: 开发的IDE:JBuilderX 使用的数据库:MS Sql Server 2000 使用的数据库驱动:JSQL Driver(JDBC 3.0) 说明: 1、hibernate在配置文件中明确说明“Microsoft Driver (not recommended!)”,因此先使用JSQL Driver。 2、JSQL Driver可以到http://www.jnetdirect...
实例讲解如何巧用延时自拍 延时自拍是现在相机都会提供的一项功能。通常我们认为延时自拍只是方便摄影师自拍,或者方便我们拍摄合影,其实延时自拍的作用不止于此。巧用延时自拍可以拍到很多意想不到的效果。 实例一:巧妙的自拍构想 日本的浮游少女大家肯定都很熟悉,在早些时候浮游简直红遍了整个网络。其实这些照片中的浮游...
4.改进程序 (1)记录历史步骤,以便可以悔棋: 记录历史步骤的方法是实现一个History类,这个类实际上是一个Vector的封装,用来保存每一步的走法,走法被定义为一个包含5个元素的数组,分别是 X,Y,width,height,direction. 这里需要注重的是,Java当中实际上是没有局部变量的,每一个局部变量都需要new...
1. 必须先安装 NetBeans IDE 4.0 和 NetBeans Mobility Pack 4.0,然后才能开始进行 J2ME MIDP 开发。有关下载和安装完整环境的说明,请参见 J2ME MIDP 开发下载页面http://www.netbeans.org/kb/articles/mobility_zh_CN.Html。 2. 创建 MIDP 应用程序 创建新的 J2ME MIDP 项目 2. 创建新的移动应用程序: (1).选择“文件”>...
(3).建立Draw类用来显示图形: public class Draw { /** Creates a new instance of Draw */ public Draw(Canvas canvas) { } public static boolean paint(Graphics g, byte img, int x, int y) { //在地图的x,y点绘制img指定的图片 try { paint(g, img, x, y, Ima...

经验教程

619

收藏

91
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部