1.导入ssh的jar包
2.配置web.xml文件
a.配置session的管理
b.配置struts2的前端控制器
c.配置启动web的时候实例化spring容器
例子:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- 配置Hibernate的session的关闭时机 -->
<filter>
<filter-name>opensessionfilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>opensessionfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置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>
<!-- 配置tomcat启动的时候实例化spring容器 -->
<!-- 为ContextLoaderListner指定spring容器配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 启动tomcat实例化spring容器对象 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3.新建项目的结构package
4.src下新建applicationContext.xml配置文件
例子:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
</beans>
5.src下新建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="onepkg" extends="struts-default" namespace="/">
<action name="login">
<result name="success">
/WEB-INF/manage/login.jsp
</result>
</action>
<action name="tologin" class="com.chuanxiang.action.OneAction">
<result name="success">
\WEB-INF\manage\xinwen.jsp
</result>
</action>
</package>
</struts>
6.在spring配置文件中
配置整合hibernate需要的mydataSource的bean和sessionFactory的bean
7.在spring配置文件中
配置开启组件扫描
8.在spring配置文件中
配置开启aop扫描
9.在spring配置文件中
配置启用事务控制注解
在需要事务的action的bean下添加注解@Transactional即可
spring配置文件例子:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<bean id="mydataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/xiang">
</property>
<property name="username" value="root">
</property>
<property name="password" value="870304">
</property>
<property name="initialSize" value="2">
</property>
<property name="maxActive" value="15">
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入dataSource连接资源 -->
<property name="dataSource" ref="mydataSource">
</property>
<!-- 注入hibernate配置参数 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 注入hibernate映射描述文件 -->
<property name="mappingResources">
<list>
<value>com/cxtt/entity/Admin.hbm.xml</value>
<value>com/cxtt/entity/Xinwen.hbm.xml</value>
<value>com/cxtt/entity/Xwtype.hbm.xml</value>
</list>
</property>
</bean>
<!-- 开启组件扫描 -->
<context:component-scan base-package="com" />
<!-- 开启AOP注解 -->
<aop:aspectj-autoproxy />
<!-- 声明式事务管理,采用AOP形式切入 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven proxy-target-class="true" transaction-manager="txManager" />
</beans>
10.采用注解配置的形式把bean纳入spring容器
11.用myeclipse生成数据库表实体类和映射文件,拷贝到项目相关目录下
12.去掉映射文件中dtd前面的空格
13.在spring配置文件的sessionFactory的bean中加入hibernate的映射描述文件
14.编写dao接口
15.编写dao接口实现类
例子:
@Repository("infoDao")
publicclass HibernateAdminInfoDAOextends HibernateDaoSupport implements IAdminInfoDAO{
@Resource
publicvoid setMySessionFactory(SessionFactory sf){
super.setSessionFactory(sf);
}
publicAdminInfo findByCodeAndPwd(String adminCode, String pwd) {
String hql = "from AdminInfo where adminCode=? and password=?";
Object[] params = {adminCode,pwd};
List<AdminInfo> list = this.getHibernateTemplate().find(hql,params);
if(!list.isEmpty()){
returnlist.get(0);
}
returnnull;
}
}
16.编写jsp页面