一、Spring+Hibernate
1、拷贝jar包
2、配置web.xml
2.1、加载applicationContext.xml
<!-- 加载applicationContext.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2.2、添加和配置applicationContext.xml
首先在配置之前,我们需要数据库连接,先创建一个db.properties的资源文件。
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/admindb
jdbc.username=root
jdbc.password=123
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=update
配置applicationContext.xml
applicationContext.xml的配置循序为:
db.properties–>dataSource–>sessionFactory–>xxxDao–>xxxService–>xxxAction
<!-- 加载db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
/>
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
>
<!-- 加载hibernate的配置,方言,等信息 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
<!-- 引入映射文件 -->
<property name="mappingLocations" value="classpath:cn/wzk/domain/*.hbm.xml"/>
</bean>
<!-- 配置事务管理(做什么) -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="crudAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRES_NEW"/>
<tx:method name="add*" propagation="REQUIRES_NEW"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* cn.wzk.service.*Service.*(..))" id="txPoint"/>
<aop:advisor advice-ref="crudAdvice" pointcut-ref="txPoint"/>
</aop:config>
<!-- dao -->
<bean id="baseDao" abstract="true">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="personDao" class="cn.wzk.dao.impl.PersonDaoImpl" parent="baseDao"/>
<!-- service -->
<bean id="personService" class="cn.wzk.service.impl.PersonServiceImpl">
<property name="personDao" ref="personDao" />
</bean>
<!-- action -->
<bean id="personAction" class="cn.wzk.action.PersonAction">
<property name="service" ref="personService"/>
</bean>
在配置SessionFactory的时候我们可以保留hibernate.cfg.xml,然后通过configLocation进行加载。
二、Spring+Struts2
1、拷贝jar包
最后我们不要忘记了添加一个Struts和Spring的整合包
struts2-spring-plugin-2.3.16.1.jar
2、配置web.xml(Struts2核心过滤器)
<filter>
<filter-name>Struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2、添加struts.xml文件并配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="ssh" extends="struts-default" namespace="/">
<action name="person_*" class="personAction" method="{1}">
<result name="list">list.jsp</result>
<result name="del" type="redirectAction">person_getAll</result>
</action>
</package>
</struts>
注意class中填写的是由Spring管理的bean的id