在进行SSH或者S2SH中,我们通常不会将struts(struts2)与数据库直接打交道。而是通过接口的方式将二者进行关联。struts(struts2)与一个service接口管理,然后将一个service类去实现service接口。在service类中,与一个Dao接口进行关联,再将一个Dao类实现Dao接口。在Dao类中实现Hibernate对数据库的操作。我们现在用spring对hibernate的配置文件进行托管。在此使用数据源来连接数据库。其applicationContext.xml文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dateSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/vote" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="1000" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dateSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="questionDao" class="com.vote.dao.impl.QuestionDAOImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="questionService" class="com.vote.service.impl.QuestionServiceImpl">
<property name="questionDao" ref="questionDao"/>
</bean>
<bean id="addQuestion" class="com.vote.action.question.AddQuestion">
</bean>
<bean id="addQuestion2" class="com.vote.action.question.AddQuestion2">
<property name="questionService" ref="questionService"/>
</bean>
</beans>