開發工具采用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