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