java

位置:IT落伍者 >> java >> 浏览文章

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


发布日期:2022年07月18日
 
怎样利用Hibernate开发Blog实例分析

开发工具采用MYECLIPS首先是建立项目导入STRUTS+HIBERNATE包然后配置SRC跟目录下的Hibernatecfgxml我采用的是MYSQL数据库所以配置如下

<hibernateconfiguration>

<sessionfactory>

<! properties >

<property name=connectionusername>

root

</property>

<property name=connectionurl>

jdbc:mysql://localhost:/tonnyblog

</property>

<property name=dialect>

netsfhibernatedialectMySQLDialect

</property>

<property name=connectionpassword></property>

<property name=connectiondriver_class>

orggjtmmmysqlDriver

</property>

<! mapping files >

<mapping resource=com/tonny/blog/bean/Userhbmxml/>

<mapping resource=com/tonny/blog/bean/Itemhbmxml/>

<mapping resource=com/tonny/blog/bean/Reviewhbmxml/>

</sessionfactory></hibernateconfiguration>

mapping为JAVABEAN所对应的映射

下面我们继续HIBERNATE程序的下步编写

import netsfhibernateHibernateException;

import netsfhibernateSession;

import netsfhibernateSessionFactory;

import netsfhibernatecfgConfiguration;

/** *Description of the Class * *

@authortonny * @created

*/public class HibernateUtil

{

private final static SessionFactory sessionFactory;

static

{

try

{

sessionFactory =

new Configuration(nfigure()buildSessionFactory();

}

catch (HibernateException ex)

{

throw new RuntimeException(

Exception building SessionFactory:

+ exgetMessage()ex);

}

}

private HibernateUtil(){}

/** *Description of the Field

*/

private final static ThreadLocal

session = new ThreadLocal();

/** *Description of the Method

* * @return

Description of the Return Value *

@exceptionHibernateException

Description of the Exception */

public static Session currentSession()

throws HibernateException

{

Session s = (Session) sessionget();

if (s == null)

{

s = sessionFactoryopenSession();

sessionset(s);

}return s;

}

/** *Description of the Method

* * @exceptionHibernateException

Description of the Exception */

public static void closeSession()

throws HibernateException {

Session s = (Session) sessionget();

sessionset(null);

if (s != null)

{

sclose();

}

}

public static void init()

{

}

}

创建sessionFactory

import netsfhibernateHibernateException;

import netsfhibernateSessionFactory;

import netsfhibernatecfgConfiguration;

import orgapachestrutsactionActionServlet;

import orgapachestrutsactionPlugIn;

import onfigModuleConfig;

import comtonnyblogdaohibernateHibernateUtil;

public class HibernatePlugin

implements orgapachestrutsactionPlugIn

{

public void init(ActionServlet servlet

ModuleConfig config)

{

HibernateUtilinit();

}

public void destroy()

{

try

{

HibernateUtilcloseSession();

}

catch(HibernateException hex)

{

hexprintStackTrace();

}

}

}

以上为HIBERNATE基本配置对数据库操作采用DAO模式增加配置如下

import comtonnyblogdaohibernate*;

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();

}

}

strutsxml增加配置

<controller contentType=text/html

debug= locale=true

nocache=true

processorClass=

comtntrollerIndexRequestProcessor/>

<messageresources parameter=comtonnyresource/>

<plugin className=

comtonnyblogstrutspluginHibernatePlugin/>

<plugin className=orgapachestrutstilesTilesPlugin>

<setproperty property=moduleAware value=true/>

<setproperty property=definitionsdebug value=/>

<setproperty property=definitionsparserdetails

value=/>

<setproperty property=definitionsparservalidate

value=false/>

<setproperty property=definitionsconfig

value=/WEBINF/titledefxml/>

</plugin>

下面我们定义服务层

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 comtonnyblogstrutsform*;

import comtonnyblogview*;

import comtonnyblogbean*;

import javautil*;

import javaxservlethttp*;

public interface IService

{

public UserContainer login(UserForm userForm);

public boolean logout(UserContainer userContainer);

public boolean addBlog(BlogForm blogFormString filePath);

public boolean removeBlog(Long id);

public boolean addReview(Long topicIdReviewForm reviewForm);

public boolean updateBlog(Long idString contenString 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 comtonnyblogstrutsform*;

import comtonnyblogview*;

import comtonnyblogdao*;

import comtonnyblogbean*;

import javautil*;import javaxservlethttp*;

import comtonnyblogstrutsutilFileUpload;

public class ServiceImp implements IService

{

public UserContainer login(UserForm userForm)

{

UserDAO userDAO=DAOFactorygetInstance()getUserDAO();

User user=userDAOloadUser(userFormgetName());

if(user==null)return new UserContainer(false);

if(!usergetPassword()equals(userFormgetPassword()))

return new UserContainer(false);

return new UserContainer(userFormgetName()true);

}

public boolean logout(UserContainer userContainer)

{

userContainersetLogin(false);

userContainersetName();

return true;

}

public boolean addBlog(BlogForm blogFormString path)

{

ItemDAO itemDAO=DAOFactorygetInstance()getItemDAO();

Item item=new Item(blogFormgetTopic()

blogFormgetContent()

FileUploadupload(blogFormgetFile()path)new Date());

itemDAOaddItem(item);

return true;

}

public boolean removeBlog(Long id)

{

ReviewDAO reviewDAO=DAOFactorygetInstance(

上一篇:struts分发请求的过程与相关配置

下一篇:扩展Eclipse的Java开发工具(四)