转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> JAVA开发 >> 正文
利用开源项目Hibernate开发Blog系统         ★★★★

利用开源项目Hibernate开发Blog系统

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1872 更新时间:2009/4/22 23:29:43

  开发工具采用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) {
      &

[1] [2] [3]  下一页


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台