网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  怎样利用hibernate开发blog实例分析     
  文章作者:未知  文章来源:水木森林  
  查看:98次  录入:管理员--2007-11-17  
 
  开发工具采用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(
 
 
上一篇: o/r mapping(hibernate)方法小结    下一篇: 发现hibernate中文问题又一解决方法
  相关文档
j2ee 应用程序中的数据管理和数据持久性 11-17
servlet和javaserverpages的集成应用 11-17
利用httpsessionlistener统计在线人数 11-17
java入门 Java中的日期处理 03-25
基于uml柔性开发模型之java设计 11-17
使用objectstream会出现的问题 11-17
高效j2me应用程序秘诀二十招 11-16
md5的java bean实现 11-17
jdbc专题介绍(2) 11-17
java学习从入门到精通 4方法篇 11-16
java nio chapter1 learning tips 11-17
用java在palm和pocket pc上编程 11-17
ajax开发-可扩展的xap 11-17
web services&xml:xml和j2ee的完美结合 (1) 10-22
深入浅出java clone技术 11-17
eclipse入门使用指南及开发eclipse插件(1) 11-17
用java api进行sort 11-17
我的java,现在和未来 11-17
collections api定制实现(一) 11-16
高级:使用异步servlet扩展ajax应用程序 01-09
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息