运用三大框架,使用MyEclipse帮我们导入jar包会发现有些包冲突了或者有些包是多余的,让我们来手动配置三大框架吧!
1、下载三大框架的必备的jar包,下载地址:http://download.csdn.net/detail/harderxin/4420066
2、新建一个web工程SSHTest,将jar包导入到工程中,注意导入进去后在WEB-INFO目录的lib文件夹中要有相应的jar包,jar包如下
3、打开web.xml,在里面添加如下代码:
//Spring配置
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- <!-- default: /WEB-INF/applicationContext.xml -->
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> -->
- <param-value>classpath:beans.xml</param-value>
- </context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<!-- default: /WEB-INF/applicationContext.xml -->
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> -->
<param-value>classpath:beans.xml</param-value>
</context-param>
//Struts配置
- <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>
<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>
4、打开Struts.xml,配置Struts一些常量,并将stsruts交给Spring容器来管理
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
- <constant name="struts.configuration.xml.reload" value="true"/>
- <constant name="struts.action.extension" value="action,do,webwork" />
- <constant name="struts.enable.DynamicMethodInvocation" value="true" />
- <constant name="struts.devMode" value="true" />
- <constant name="struts.multipart.maxSize" value="100971520"></constant>
- <constant name="struts.i18n.encoding" value="UTF-8"></constant>
- <constant name="struts.objectFactory.spring.autoWire" value="name"></constant>
- <!-- 将struts2交给spring管理-->
- <constant name="struts.objectFactory" value="spring"></constant>
- <constant name="struts.custom.i18n.resources" value="messages"></constant>
- <include file="strutsConfig/common.xml"></include>
- <include file="strutsConfig/struts-test.xml"></include>
- <include file="strutsConfig/struts-admin.xml"></include>
- <include file="strutsConfig/struts-web.xml"></include>
- <include file="strutsConfig/struts-admin.xml"></include>
- <include file="strutsConfig/struts-consumer.xml"></include>
- <include file="strutsConfig/struts-restaurant.xml"></include>
- <include file="strutsConfig/struts-web.xml"></include>
- </struts>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
<constant name="struts.configuration.xml.reload" value="true"/>
<constant name="struts.action.extension" value="action,do,webwork" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<constant name="struts.multipart.maxSize" value="100971520"></constant>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.objectFactory.spring.autoWire" value="name"></constant>
<!-- 将struts2交给spring管理-->
<constant name="struts.objectFactory" value="spring"></constant>
<constant name="struts.custom.i18n.resources" value="messages"></constant>
<include file="strutsConfig/common.xml"></include>
<include file="strutsConfig/struts-test.xml"></include>
<include file="strutsConfig/struts-admin.xml"></include>
<include file="strutsConfig/struts-web.xml"></include>
<include file="strutsConfig/struts-admin.xml"></include>
<include file="strutsConfig/struts-consumer.xml"></include>
<include file="strutsConfig/struts-restaurant.xml"></include>
<include file="strutsConfig/struts-web.xml"></include>
</struts>
5、打开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:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- 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/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <context:annotation-config />
- <context:component-scan base-package="com.bjsxt" />
- <!--
- <bean id="dataSource"
- 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/spring" />
- <property name="username" value="root" />
- <property name="password" value="bjsxt" />
- </bean>
- -->
- <bean
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <value>classpath:jdbc.properties</value>
- </property>
- </bean>
- <bean id="dataSource" destroy-method="close"
- class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName"
- value="${jdbc.driverClassName}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </bean>
- <bean id="sf"
- class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <!--
- <property name="annotatedClasses">
- <list>
- <value>com.bjsxt.model.User</value>
- <value>com.bjsxt.model.Log</value>
- </list>
- </property>
- -->
- <property name="packagesToScan">
- <list>
- <value>com.bjsxt.registration.model</value>
- </list>
- </property>
- <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="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
- <property name="sessionFactory" ref="sf"></property>
- </bean>
- <bean id="txManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sf" />
- </bean>
- <aop:config>
- <aop:pointcut id="bussinessService"
- expression="execution(public * com.bjsxt.registration.service.*.*(..))" />
- <aop:advisor pointcut-ref="bussinessService"
- advice-ref="txAdvice" />
- </aop:config>
- <tx:advice id="txAdvice" transaction-manager="txManager">
- <tx:attributes>
- <tx:method name="exists" read-only="true" />
- <tx:method name="add*" propagation="REQUIRED"/>
- </tx:attributes>
- </tx:advice>
- </beans>
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.bjsxt" />
<!--
<bean id="dataSource"
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/spring" />
<property name="username" value="root" />
<property name="password" value="bjsxt" />
</bean>
-->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sf"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--
<property name="annotatedClasses">
<list>
<value>com.bjsxt.model.User</value>
<value>com.bjsxt.model.Log</value>
</list>
</property>
-->
<property name="packagesToScan">
<list>
<value>com.bjsxt.registration.model</value>
</list>
</property>
<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="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sf"></property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sf" />
</bean>
<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(public * com.bjsxt.registration.service.*.*(..))" />
<aop:advisor pointcut-ref="bussinessService"
advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="exists" read-only="true" />
<tx:method name="add*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
</beans>
6、添加相应实体类的XXX.hbm.xml文件映射
- <?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">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.jwjx.beans.DatePlanPri" table="DATEPLAN_PRI" schema="ADMIN">
- <id name="rjhmid" type="java.lang.Long">
- <column name="RJHMID" />
- <generator class="increment" />
- </id>
- <property name="jctype" type="java.lang.String">
- <column name="JCTYPE" length="20" />
- </property>
- <property name="jcnum" type="java.lang.String">
- <column name="JCNUM" length="20" />
- </property>
- <property name="fixfreque" type="java.lang.String">
- <column name="FIXFREQUE" length="20" />
- </property>
- <property name="kcsj" type="java.lang.String">
- <column name="KCSJ" length="20" />
- </property>
- <property name="jhqjsj" type="java.lang.String">
- <column name="JHQJSJ" length="20" />
- </property>
- </class>
- </hibernate-mapping>
<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.jwjx.beans.DatePlanPri" table="DATEPLAN_PRI" schema="ADMIN">
<id name="rjhmid" type="java.lang.Long">
<column name="RJHMID" />
<generator class="increment" />
</id>
<property name="jctype" type="java.lang.String">
<column name="JCTYPE" length="20" />
</property>
<property name="jcnum" type="java.lang.String">
<column name="JCNUM" length="20" />
</property>
<property name="fixfreque" type="java.lang.String">
<column name="FIXFREQUE" length="20" />
</property>
<property name="kcsj" type="java.lang.String">
<column name="KCSJ" length="20" />
</property>
<property name="jhqjsj" type="java.lang.String">
<column name="JHQJSJ" length="20" />
</property>
</class>
</hibernate-mapping>
7、将项目部署到Tomcat服务器,如果启动过程中没有报错,说明ssh三大框架配置OK!