|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
java比较简单,没有C++的烦琐,但学习时最好有C++为基础.与JSP和SQL起应用,功能强大.项目 开辟工具接纳MYECLIPS3.6,起首是创建项目,导进STRUTS+HIBERNATE包,然后设置SRC跟目次下的hibernate.cfg.xml.我接纳的是MYSQL数据库,以是设置以下:
- <hibernate-configuration>
- <session-factory>
- <!--properties-->
- <propertyname="connection.username">root</property>
- <propertyname="connection.url">jdbc:mysql://localhost:3306/tonnyblog</property>
- <propertyname="dialect">net.[u]sf[/u].hibernate.dialect.MySQLDialect</property>
- <propertyname="connection.password"></property>
- <propertyname="connection.driver_class">org.gjt.mm.mysql.Driver</property>
- <!--mappingfiles-->
- <mappingresource="com/tonny/blog/bean/User.hbm.xml"/>
- <mappingresource="com/tonny/blog/bean/Item.hbm.xml"/>
- <mappingresource="com/tonny/blog/bean/Review.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
复制代码 mapping为JAVABEAN所对应的映照。
上面我们持续HIBERNATE程序的下步编写
- importnet.sf.hibernate.HibernateException;
- importnet.sf.hibernate.Session;
- importnet.sf.hibernate.SessionFactory;
- importnet.sf.hibernate.cfg.Configuration;
- /**
- *DescriptionoftheClass
- *
- *@authortonny
- *@created2004年2月6日
- */
- publicclassHibernateUtil{
- privatefinalstaticSessionFactorysessionFactory;
- static{
- try{
- sessionFactory=
- newConfiguration().configure().buildSessionFactory();
- }catch(HibernateExceptionex){
- thrownewRuntimeException(
- "ExceptionbuildingSessionFactory:"+ex.getMessage(),ex);
- }
- }
- privateHibernateUtil(){
- }
- /**
- *DescriptionoftheField
- */
- privatefinalstaticThreadLocalsession=newThreadLocal();
- /**
- *DescriptionoftheMethod
- *
- *@returnDescriptionoftheReturnValue
- *@exceptionHibernateExceptionDescriptionoftheException
- */
- publicstaticSessioncurrentSession()throwsHibernateException{
- Sessions=(Session)session.get();
- if(s==null){
- s=sessionFactory.openSession();
- session.set(s);
- }
- returns;
- }
- /**
- *DescriptionoftheMethod
- *
- *@exceptionHibernateExceptionDescriptionoftheException
- */
- publicstaticvoidcloseSession()throwsHibernateException{
- Sessions=(Session)session.get();
- session.set(null);
- if(s!=null){
- s.close();
- }
- }
- publicstaticvoidinit(){
- }
- }
复制代码 创立sessionFactory
- importnet.sf.hibernate.HibernateException;
- importnet.sf.hibernate.SessionFactory;
- importnet.sf.hibernate.cfg.Configuration;
- importorg.apache.struts.action.ActionServlet;
- importorg.apache.struts.action.PlugIn;
- importorg.apache.struts.config.ModuleConfig;
- importcom.tonny.blog.dao.hibernate.HibernateUtil;
- publicclassHibernatePluginimplementsorg.apache.struts.action.PlugIn{
- publicvoidinit(ActionServletservlet,ModuleConfigconfig){
- HibernateUtil.init();
- }
- publicvoiddestroy(){
- try{
- HibernateUtil.closeSession();
- }
- catch(HibernateExceptionhex){
- hex.printStackTrace();
- }
- }
- }
复制代码 以上为HIBERNATE基础设置,对数据库操纵接纳DAO形式,增添设置以下:
- importcom.tonny.blog.dao.hibernate.*;
- publicclassDAOFactory{
- privatestaticDAOFactoryinstance;
- publicsynchronizedstaticDAOFactorygetInstance(){
- if(instance==null){
- instance=newDAOFactory();
- }
- returninstance;
- }
- privateDAOFactory(){
- }
- publicItemDAOgetItemDAO(){
- returnnewItemDAOHibernate();
- }
- publicReviewDAOgetReviewDAO(){
- returnnewReviewDAOHibernate();
- }
- publicUserDAOgetUserDAO(){
- returnnewUserDAOHibernate();
- }
- }
复制代码 struts.xml增添设置
- <controllercontentType="text/html"debug="3"locale="true"nocache="true"
- processorClass="com.tonny.blog.struts.controller.IndexRequestProcessor"/>
- <message-resourcesparameter="com.tonny.resource"/>
- <[u]plug[/u]-inclassName="com.tonny.blog.struts.plugin.HibernatePlugin"/>
- <plug-inclassName="org.apache.struts.tiles.TilesPlugin">
- <set-propertyproperty="moduleAware"value="true"/>
- <set-propertyproperty="definitions-debug"value="0"/>
- <set-propertyproperty="definitions-parser-details"value="0"/>
- <set-propertyproperty="definitions-parser-validate"value="false"/>
- <set-propertyproperty="definitions-config"value="/WEB-INF/title-def.xml"/>
- </plug-in>
复制代码 上面我们界说服务层:
- publicclassServiceFactory{
- privatestaticServiceFactoryinstance;
- publicsynchronizedstaticServiceFactorygetInstance(){
- if(instance==null){
- instance=newServiceFactory();
- }
- returninstance;
- }
- privateServiceFactory(){
- }
- publicIServicegetService(){
- returnnewServiceImp();
- }
- }
复制代码- importcom.tonny.blog.struts.form.*;
- importcom.tonny.blog.view.*;
- importcom.tonny.blog.bean.*;
- importjava.util.*;
- import[u]javax[/u].servlet.http.*;
- publicinterfaceIService{
- publicUserContainerlogin(UserFormuserForm);
- publicbooleanlogout(UserContaineruserContainer);
- publicbooleanaddBlog(BlogFormblogForm,StringfilePath);
- publicbooleanremoveBlog(Longid);
- publicbooleanaddReview(LongtopicId,ReviewFormreviewForm);
- publicbooleanupdateBlog(Longid,Stringconten,Stringtopic);
- publicbooleanremoveReview(Longid);
- public[u]List[/u]getItems();
- publicItemViewgetItem(Longid);
- publicItemViewgetEditItem(Longid);
- publicListsearch(SearchFormsearchForm);
- /**
- *@paramid
- *@paramuserForm
- */
- publicbooleanaddUser(UserFormuserForm);
- }
复制代码- importcom.tonny.blog.struts.form.*;
- importcom.tonny.blog.view.*;
- importcom.tonny.blog.dao.*;
- importcom.tonny.blog.bean.*;
- importjava.util.*;
- importjavax.servlet.http.*;
- importcom.tonny.blog.struts.util.FileUpload;
- publicclassServiceImpimplementsIService{
- publicUserContainerlogin(UserFormuserForm){
- UserDAOuserDAO=DAOFactory.getInstance().getUserDAO();
- Useruser=userDAO.loadUser(userForm.getName());
- if(user==null)returnnewUserContainer("",false);
- if(!user.getPassword().equals(userForm.getPassword()))returnnewUserContainer("",false);
- returnnewUserContainer(userForm.getName(),true);
- }
- publicbooleanlogout(UserContaineruserContainer){
- userContainer.setLogin(false);
- userContainer.setName("");
- returntrue;
- }
- publicbooleanaddBlog(BlogFormblogForm,Stringpath){
- ItemDAOitemDAO=DAOFactory.getInstance().getItemDAO();
- Itemitem=newItem(blogForm.getTopic(),blogForm.getContent(),
- FileUpload.upload(blogForm.getFile(),path),newDate());
- itemDAO.addItem(item);
- returntrue;
- }
- publicbooleanremoveBlog(Longid){
- ReviewDAOreviewDAO=DAOFactory.getInstance().getReviewDAO();
- ItemDAOitemDAO=DAOFactory.getInstance().getItemDAO();
- itemDAO.removeItem(id);
- returnreviewDAO.removeReviews(id);
- }
- publicbooleanaddReview(LongtopicId,ReviewFormreviewForm){
- ReviewDAOreviewDAO=DAOFactory.getInstance().getReviewDAO();
- Reviewreview=newReview(reviewForm.getName(),reviewForm.getContent(),
- topicId,newDate());
- returnreviewDAO.addReview(review);
- }
- publicbooleanupdateBlog(Longid,Stringcontent,Stringtopic){
- ItemDAOitemDAO=DAOFactory.getInstance().getItemDAO();
- Itemitem=newItem();
- item.setId(id);
- item.setContent(content);
- item.setTopic(topic);
- returnitemDAO.updatItem(item);
- }
- publicbooleanaddUser(UserFormuserForm){
- UserDAOuserDAO=(UserDAO)DAOFactory.getInstance().getUserDAO();
- Useruser=newUser(userForm.getName(),userForm.getPassword());
- returnuserDAO.addUser(user);
- }
- publicbooleanremoveReview(Longid){
- ReviewDAOreviewDAO=DAOFactory.getInstance().getReviewDAO();
- returnreviewDAO.removeReview(id);
- }
- publicListgetItems(){
- ItemDAOitemDAO=DAOFactory.getInstance().getItemDAO();
- Listitems=itemDAO.loadItems();
- ListitemViews=newArrayList();
- for(Iteratorit=items.iterator();it.hasNext();){
- Itemitem=(Item)it.next();
- ItemViewitemView=newItemView();
- itemView.setContent(item.getContent());
- itemView.setDate(item.getDate());
- itemView.setFile(item.getFile());
- itemView.setId(item.getId());
- itemView.setTopic(item.getTopic());
- itemViews.add(itemView);
- }
- returnitemViews;
- }
- publicItemViewgetEditItem(Longid){
- ItemDAOitemDAO=DAOFactory.getInstance().getItemDAO();
- Itemitem=itemDAO.loadItem(id);
- ItemViewitemView=newItemView();
- itemView.setContent(item.getContent());
- itemView.setDate(item.getDate());
- itemView.setFile(item.getFile());
- itemView.setId(item.getId());
- itemView.setTopic(item.getTopic());
- returnitemView;
- }
- publicListsearch(SearchFormsearchForm){
- ItemDAOitemDAO=DAOFactory.getInstance().getItemDAO();
- Listitems=itemDAO.loadItems(searchForm.getKeyword());
- ListitemViews=newArrayList();
- for(Iteratorit=items.iterator();it.hasNext();){
- Itemitem=(Item)it.next();
- ItemViewitemView=newItemView();
- itemView.setContent(item.getContent());
- itemView.setDate(item.getDate());
- itemView.setFile(item.getFile());
- itemView.setId(item.getId());
- itemView.setTopic(item.getTopic());
- itemViews.add(itemView);
- }
- returnitemViews;
- }
- }
复制代码 上面是ACTION怎样挪用以上个服务:
- importjava.io.*;
- importjavax.servlet.RequestDispatcher;
- importjavax.servlet.ServletException;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpSession;
- importjavax.servlet.http.HttpServletResponse;
- importorg.apache.struts.action.Action;
- importorg.apache.struts.action.ActionError;
- importorg.apache.struts.action.ActionErrors;
- importorg.apache.struts.action.ActionForm;
- importorg.apache.struts.action.ActionForward;
- importorg.apache.struts.action.ActionMapping;
- importorg.apache.struts.action.ActionServlet;
- importorg.apache.struts.util.MessageResources;
- importcom.tonny.blog.struts.form.*;
- publicclassAddBlogextendsBlogBaseAction{
- //------------------------------------------------------------LocalForwards
- staticfinalprivateStringFORWARD_success="success";
- staticfinalprivateStringFORWARD_failure="failure";
- //------------------------------------------------------------ActionMethods
- publicActionForwardexecute(ActionMappingmapping,ActionFormform,
- HttpServletRequestrequest,HttpServletResponseresponse)
- throwsException{
- if(!isLogin(request))returnmapping.findForward(FORWARD_failure);
- service.addBlog((BlogForm)form,((BlogForm)form).getFile().getFileName());
- returnmapping.findForward(FORWARD_success);
- }
- }
复制代码 下一步为DAO层来操纵数据库:
- importcom.tonny.blog.bean.*;
- importjava.util.List;
- publicinterfaceItemDAO{
- publicbooleanaddItem(Itemitem);
- publicbooleanremoveItem(Longid);
- publicListloadItems();
- publicListloadItems(Stringtopic);
- publicItemloadItem(Longid);
- publicbooleanupdatItem(Itemitem);
- }
复制代码 DAOFACTORY挪用气力化办法:
这里完成了对数据库查询,修正,删除操纵,没有MANY-TO-MANY操纵。
还是要自己一点一点写代码,然后编译,改错再编译好那。还有最重要的是.net的编译环境非常好,你甚是不需要了解太多工具,对于简单的系统,你可以之了解一些语法就哦了。 |
|