用maven搭建struts2+spring+hibernate

14年前

今天抽空练习下用maven2+myeclipse搭建一个ssh框架,把搭建过程简单记录一下,以便以后方便查阅
1.准备工作:jdk1.6,maven2.0.9,myeclipse6.0.1,mysql数据库

2.jdk的安装配置,以及maven2在eclipse里的配置网上的例子很多就不在重复

3.用m2搭建主项目工程s2shdemo,在myeclipse中点击new-other-maven project-create a simple project输入groupId,ArtifactId,在这里别的地方都可以根据自己的爱好填写,但打包方式packaging必须选择pom选项。点下一步后点击完成即可。

4.完成web层:我在这里用的是m2的命令行搭建一个web工程,进入cmd运行如下命令:
mvn archetype:create -DgroupId=com.lcsssh.web.action -DartifactId=s2shdemo -DarchetypeGroupId=org.apache.struts -DarchetypeArtifactId=struts2-archetype-starter -DarchetypeVersion=2.0.11.2-SNAPSHOT -DremoteRepositories=http://people.apache.org/repo/m2-snapshot-repository 
其中有标志的地方可以根据自己需要修改,这样一个struts2和spring的web工程就搭建成功。
5.完成service层:在myeclipse中选择file-other-maven module填写module name并选择父工程,也就是第3部你所建立起来的工程。进入下一步就如第三步一样填写所需选项,记住这里packaging应选择jar或其他不能用 pom。

6.如第五步搭建dao层。

7.整合:以上步骤完成后开始配置,我把配置有关的文件全部放到web模块下的resources目录下
    配置web.xml文件,加入如下代码
<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
 <display-name>s2shdemo</display-name>  
 <distributable />  
 <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>  
 <filter>  
  <filter-name>struts</filter-name>  
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  <init-param>  
   <param-name>actionPackages</param-name>  
   <param-value>cn.allwap.backend.action</param-value>  
  </init-param>  
 </filter>  
 <servlet>  
  <servlet-name>dwr</servlet-name>  
  <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>  
  <init-param>  
   <param-name>debug</param-name>  
   <param-value>true</param-value>  
  </init-param>  
 </servlet>    
 <filter>  
  <filter-name>openSession</filter-name>  
  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
  <init-param>  
   <param-name>singleSession</param-name>  
   <param-value>false</param-value>  
  </init-param>  
 </filter>  
 <filter>  
  <filter-name>struts-cleanup</filter-name>  
  <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>  
 </filter>  
 <filter>  
  <filter-name>encodingFilter</filter-name>  
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  <init-param>  
   <param-name>encoding</param-name>  
   <param-value>UTF-8</param-value>  
  </init-param>  
 </filter>  
 <filter-mapping>  
  <filter-name>encodingFilter</filter-name>  
  <url-pattern>/*</url-pattern>  
 </filter-mapping>  
 <filter-mapping>  
  <filter-name>openSession</filter-name>  
  <url-pattern>/*</url-pattern>  
 </filter-mapping>  
 <servlet-mapping>  
  <servlet-name>dwr</servlet-name>  
  <url-pattern>/dwr/*</url-pattern>  
 </servlet-mapping>  
 <filter-mapping>  
  <filter-name>struts-cleanup</filter-name>  
  <url-pattern>/*</url-pattern>  
 </filter-mapping>  
 <filter-mapping>  
  <filter-name>struts</filter-name>  
  <url-pattern>/*</url-pattern>  
 </filter-mapping>  
   <!-- Spring 刷新Introspector防止内存泄露 -->  
    <listener>  
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
    </listener>  
  
       
    <!-- session超时定义,单位为分钟 -->  
    <session-config>  
        <session-timeout>10</session-timeout>  
    </session-config>
 <welcome-file-list>  
  <welcome-file>index.jsp</welcome-file>  
  <welcome-file>default.jsp</welcome-file>  
  <welcome-file>index.html</welcome-file>  
 </welcome-file-list>  
</web-app>  
配置web action applicationContext-action.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: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/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">

     <bean id="helloWorldAction" class="com.lcsssh.web.action.HelloWorldAction"  scope="prototype"/>
      <bean id="adminInfoAction" class="com.lcsssh.web.action.AdminInfoAction"  scope="prototype">
       <property name="adminInfoServiceImpl" ref="adminInfoServiceImpl"/>
      </bean>
</beans>

配置业务层applicationContext-service.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:lang="http://www.springframework.org/schema/lang"  
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"  
 default-autowire="byName">  
  <bean id="adminInfoServiceImpl" class="com.lcsssh.service.impl.AdminInfoServiceImpl" scope="prototype">
   <property name="adminInfoDAO" ref="adminInfoDAO"></property>
  </bean> 
</beans>    
配置dao 层applicationContext-dao.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:lang="http://www.springframework.org/schema/lang"  
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"  
 default-autowire="byName">    
  <bean id="adminInfoDAO" class="com.lcsssh.dao.impl.AdminInfoDAO">
   <property name="sessionFactory">
    <ref bean="sessionFactory"/>
   </property>
  </bean>
</beans>
配置application.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:lang="http://www.springframework.org/schema/lang"  
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd">  
 
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  <property name="driverClassName">  
   <value>com.mysql.jdbc.Driver</value>  
  </property>  
  <property name="url">  
   <value>jdbc:mysql://localhost/test</value>  
  </property>  
  <property name="username">  
   <value>user</value>  
  </property>  
  <property name="password">  
   <value>123456</value>  
  </property>  
  <property name="maxActive">  
   <value>20</value>  
  </property>  
  <property name="maxIdle">  
   <value>5</value>  
  </property>  
  <property name="maxWait">  
   <value>-1</value>  
  </property>  
  <property name="defaultAutoCommit">  
   <value>true</value>  
  </property>  
 </bean>  
  
    
  
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  <property name="dataSource">  
   <ref local="dataSource" />  
  </property>     
  <property name="mappingDirectoryLocations">  
   <list>  
    <value>classpath:com/lcsssh/bo</value>  
   </list>  
  </property>     
  <property name="hibernateProperties">  
   <props>  
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
    <prop key="hibernate.show_sql">false</prop>  
    <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>  
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>  
    <prop key="hibernate.cache.use_query_cache">true</prop>  
   </props>  
  </property>  
 
 </bean>  
  <!-- dao object defintions -->
  <import resource="applicationContext-dao.xml"></import>
  <!-- business object defintions -->
  <import resource="applicationContext-service.xml"></import>
  <!-- web action object defintions -->
  <import resource="applicationContext-action.xml"></import>  
 <bean id="transactionManager"  
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  <property name="sessionFactory">  
   <ref bean="sessionFactory" />  
  </property>  
 </bean>  
  
 <bean id="transactionInterceptor"  
  class="org.springframework.transaction.interceptor.TransactionInterceptor">  
  <property name="transactionManager" ref="transactionManager"></property>  
  <property name="transactionAttributes">  
   <props>  
    <prop key="save*">PROPAGATION_REQUIRED</prop>  
    <prop key="add*">PROPAGATION_REQUIRED</prop>  
    <prop key="set*">PROPAGATION_REQUIRED</prop>  
    <prop key="update*">PROPAGATION_REQUIRED</prop>  
    <prop key="delete*">PROPAGATION_REQUIRED</prop>  
    <prop key="register">PROPAGATION_REQUIRED</prop>          
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>  
    <prop key="valid*">PROPAGATION_REQUIRED,readOnly</prop>  
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>  
   </props>  
  </property>  
 </bean>   
 <bean  
  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  <property name="beanNames">  
   <value>*Service</value>  
  </property>  
  <property name="interceptorNames">  
   <value>transactionInterceptor</value>  
  </property>  
 </bean>   
 <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">  
  <property name="transactionInterceptor" ref="transactionInterceptor">  
  </property>  
 </bean>   
</beans> 
所有配置文件配置完成。然后在父工程pom.xml下加入第4步创建的web模块工程,主要代码如下所示
<modules>
  <module>webProject</module>
  <module>serviceProject</module>
  <module>daoProject</module>
 </modules>
好了,到此为止所有的配置完成

8.根据数据库表生成dao 层代码,用myeclipse的生成工具生成即可。另外工程中所有依赖包都在web工程下的pom.xml中
为了节省篇幅,没有截图,希望大家体谅,本人水平有限,有不当之处敬请包含!!!
代码下载地址:http://www.blogjava.net/Files/lcs868/s2shdemo.zip