分享
 
 
 

怎样利用Hibernate开发Blog实例分析

王朝other·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

开发工具采用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(

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有