1.创建web工程
2.往工程的lib目录导入相关jar包
- struts2的jar包
- hibernate的jar包
- spring的jar包
- struts2与spring整合的jar包:struts2-spring-plugin-2.3.1.2.jar
3.创建3个src folder
src:存放项目源代码
config:存放配置文件
test:存放测试类
4.在src下建一个包存放持久化类和映射文件
person类的代码如下:package com.mbm.domain;</pre><pre name="code" class="java">public class Person {private Long id;private String name;</pre><pre name="code" class="java"> public Long getId() {return id;}</pre><pre name="code" class="java"> public void setId(Long id) {this.id = id;}</pre><pre name="code" class="java"> public String getName() {return name;}</pre><pre name="code" class="java"> public void setName(String name) {this.name = name;}}person.hbm.xml文件的代码如下:<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- 描述一个持久化类 name属性为持久化类的全名 table 该持久化类对应的表名 默认情况下为类名 catalog 为数据库的名称 --> <class name="com.mbm.domain.Person" table="Person"> <!-- id对应表中的主键 name为持久化类中属性的名称 length 为对应数据库表中相应字段的长度 column 属性的名称对应的表的字段名称 不写则默认和属性的名称一致 --> <id name="id" type="java.lang.Long" column="id"> <!-- 主键的生成器 --> <generator class="identity"></generator> </id> <property name="name" column="name" type="java.lang.String" length="20"> </property> </class> <p></hibernate-mapping></p><p> </p><p> </p><p> </p>
5.在config下创建一个包:hibernate,存放hibernate.cfg.xml文件
6.在config下创建一个包:spring,存放applicationContext.xml文件
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="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/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">
<import resource="applicationContext-db.xml"/>
<import resource="applicationCOntext-ssh.xml"/>
</beans>
其中applicationContext-db.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="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/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">
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate/hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes><tx:method name="save*" read-only="false"/></tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.mbm.service.impl.*.*(..))" id="perform"/>
<aop:advisor advice-ref="tx" pointcut-ref="perform"/>
</aop:config>
</beans>
本文详细介绍了一个基于Struts2、Spring及Hibernate(SSH)框架整合的Web应用开发过程,包括项目结构搭建、配置文件编写及核心代码实现。
762

被折叠的 条评论
为什么被折叠?



