利用开源项目Hibernate开发Blog系统
2008-02-23 07:52:47来源:互联网 阅读 ()
<hibernate-configuration>mapping为JAVABEAN所对应的映射。
<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>
下面我们继续HIBERNATE程序的下步编写
import net.sf.hibernate.HibernateException;创建sessionFactory
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(){
}
}
import net.sf.hibernate.HibernateException;以上为HIBERNATE基本配置,对数据库操作采用DAO模式,增加配置如下:
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();
}
}
}
import com.tonny.blog.dao.hibernate.*;struts.xml增加配置
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();
}
}
<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"/>标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash