Spring与Hibernate整合(applicationContext.xml写法)
2010-01-13 10:01
441 查看
在进行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>
<?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>
相关文章推荐
- Hibernate与Spring整合,配置文件applicationContext.xml
- Spring 整合hibernate和mybatis的 applicationContext.xml的配置
- Hibernate与Spring整合,配置文件applicationContext.xml
- 使用MyEclipse整合Struts+Spring+Hibernate时,在“applicationContext.xml”中提示class"org.apache.commons.dbcp.Basi
- spring整合hibernate(注解、xml)applicationContext.xml配置
- 使用MyEclipse整合Struts+Spring+Hibernate时,在“applicationContext.xml”中提示class"org.apache.commons.dbcp.Basi
- spring、hibernate整合核心配置文件applicationContext.xml
- spring整合hibernate(注解、xml)applicationContext.xml配置
- spring与hibernate的整合问题—Spring的配置文档applicationContext.xml
- Spring与Hibernate的整合applicationContext.xml中配置
- spring整合hibernate(注解、xml)applicationContext.xml配置
- spring+hibernate整合web.xml、applicationContext.xml配置
- spring+hibernate applicationContext.xml配置文件写法
- Spring 整合 hibernate的 applicationContext.xml 数据源配置
- ssh整合!Struts.xml、hibernate与spring整合的applicationContext.xml、web.xml文件的配置!
- Spring整合Hibernate(1)-----applicationContext.xml 设值注入依赖方式
- 【Spring整合Struts2】Could not open ServletContext resource [/WEB-INF/application.xml]错误解决
- hibernate3-maven-plugin 使用 spring applicationContext.xml中的sessionFactory配置(2)
- Hibernate SQL方言 (hibernate.dialect) Spring配置文件applicationContext.xml
- Hibernate SQL方言 (hibernate.dialect) Spring配置文件applicationContext.xml