本文以實際專案為例,完整示範如何整合 Struts 與 Hibernate 開發部落格系統,涵蓋配置、核心工具類、DAO/Service 分層與 Struts 整合要點。
<hibernate-configuration>
<session-factory>
<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 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>
SessionFactory + ThreadLocal 管理 Session 生命週期,提供 currentSession() / closeSession() 靜態方法。ItemDAO、ReviewDAO、UserDAO 實例,解耦具體實作(Hibernate 版)。<controller processorClass="com.tonny.blog.struts.controller.IndexRequestProcessor"/>
<plug-in className="com.tonny.blog.struts.plugin.HibernatePlugin"/>
<plug-in className="org.apache.struts.tiles.TilesPlugin">...</plug-in>
FileUpload.upload() 處理附件。此架構為中小型 Struts + Hibernate 專案提供標準化啟動範本,可直接擴充為生產級應用骨架。