maven搭建 struts2+spring+hibernate

           本次主要讲解使用maven搭建struts2+spring+hibernate框架。用的struts的版本是2.3.8,spring的版本是3.0.5(由于本人引用的依赖是struts2-spring-plugin,它本身依赖与spring3.0.5以及struts2.3.8,故本人使用了默认依赖,当然如果你想换spring版本的话可以使用maven的exclusion属性去掉spring的依赖,然后再加入你自己想使用的spring版本依赖。但本人不敢保证struts2-spring-plugin与你想使用的spring版本兼容,你可以自己试一下,若可以兼容@我一下,大家共同进步,谢谢),c3p0用的是0.9.1.2,下面开始项目搭建

            首先还是先创建一个maven项目,并且编辑pom将要引用的jar包依赖引入。编辑后的pom文件如下

Xml代码 

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  

  3.     <modelVersion>4.0.0</modelVersion>  

  4.     <groupId>com.qijian</groupId>  

  5.     <artifactId>SSH</artifactId>  

  6.     <packaging>war</packaging>  

  7.     <version>0.0.1-SNAPSHOT</version>  

  8.     <name>SSH Maven Webapp</name>  

  9.     <url>http://maven.apache.org</url>  

  10.     <!-- 构件的版本 -->  

  11.     <properties>  

  12.         <junit.version>3.8.1</junit.version>  

  13.         <logback.version>1.1.2</logback.version>  

  14.         <struts.version>2.3.8</struts.version>  

  15.         <spring.version>3.0.5.RELEASE</spring.version>  

  16.         <hibernate.annotations.version>3.5.6-Final</hibernate.annotations.version>  

  17.         <aspectj.version>1.8.1</aspectj.version>  

  18.         <c3p0.version>0.9.1.2</c3p0.version>  

  19.         <mysql.version>5.1.31</mysql.version>  

  20.     </properties>  

  21.     <dependencies>  

  22.         <dependency>  

  23.             <groupId>junit</groupId>  

  24.             <artifactId>junit</artifactId>  

  25.             <version>${junit.version}</version>  

  26.             <scope>test</scope>  

  27.         </dependency>  

  28.   

  29.         <dependency>  

  30.             <groupId>ch.qos.logback</groupId>  

  31.             <artifactId>logback-classic</artifactId>  

  32.             <version>${logback.version}</version>  

  33.         </dependency>  

  34.   

  35.         <dependency>  

  36.             <groupId>org.apache.struts</groupId>  

  37.             <artifactId>struts2-spring-plugin</artifactId>  

  38.             <version>${struts.version}</version>  

  39.         </dependency>  

  40.             <dependency>  

  41.             <groupId>org.apache.struts</groupId>  

  42.             <artifactId>struts2-json-plugin</artifactId>  

  43.             <version>${struts.version}</version>  

  44.         </dependency>  

  45.         <dependency>  

  46.             <groupId>org.apache.struts</groupId>  

  47.             <artifactId>struts2-convention-plugin</artifactId>  

  48.             <version>${struts.version}</version>  

  49.         </dependency>  

  50.           

  51.           

  52.   

  53.         <dependency>  

  54.             <groupId>org.aspectj</groupId>  

  55.             <artifactId>aspectjweaver</artifactId>  

  56.             <version>${aspectj.version}</version>  

  57.         </dependency>  

  58.         <dependency>  

  59.             <groupId>org.aspectj</groupId>  

  60.             <artifactId>aspectjrt</artifactId>  

  61.             <version>${aspectj.version}</version>  

  62.         </dependency>  

  63.   

  64.       

  65.         <dependency>  

  66.             <groupId>org.springframework</groupId>  

  67.             <artifactId>spring-orm</artifactId>  

  68.             <version>${spring.version}</version>  

  69.         </dependency>  

  70.   

  71.         <!-- 用c3p0连接池去管理数据库连接 -->  

  72.         <dependency>  

  73.             <groupId>c3p0</groupId>  

  74.             <artifactId>c3p0</artifactId>  

  75.             <version>${c3p0.version}</version>  

  76.         </dependency>  

  77.   

  78.         <dependency>  

  79.             <groupId>org.hibernate</groupId>  

  80.             <artifactId>hibernate-annotations</artifactId>  

  81.             <version>${hibernate.annotations.version}</version>  

  82.         </dependency>  

  83.           

  84.         <dependency>  

  85.             <groupId>mysql</groupId>  

  86.             <artifactId>mysql-connector-java</artifactId>  

  87.             <version>${mysql.version}</version>  

  88.         </dependency>  

  89.   

  90.     </dependencies>  

  91.     <build>  

  92.         <finalName>SSH</finalName>  

  93.     </build>  

  94. </project>  

 

      然后就是配置SSH了,首先编辑web.xml,配置struts2的过滤器

   

Xml代码 

  1. <filter>  

  2.         <filter-name>struts2</filter-name>  

  3.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  

  4.         <init-param>  

  5.             <param-name>struts.devMode</param-name>  

  6.             <param-value>true</param-value>  

  7.         </init-param>  

  8.         <init-param>  

  9.             <param-name>actionPackages</param-name>  

  10.             <param-value>com.qijian.action</param-value>  

  11.         </init-param>  

  12.     </filter>  

  13.     <filter-mapping>  

  14.         <filter-name>struts2</filter-name>  

  15.         <url-pattern>/*</url-pattern>  

  16.     </filter-mapping>  

          其中struts.devMode参数是将struts2的模式调为开发模式以便于调试(其实就是将错误信息打印到页面);参数actionPackages是指定你的controller的物理位置,注意在你创建controller的时候,其所在的包路径不能有用"controller"以单个词语的形式出现(例如:con.qijian.controller,这个就不行),否则的话会导致请求无法处理,会和struts2起冲突(具体冲突原因不明,未来得及看struts2的源代码)。其整个web.xml文件的源代码如下:

Xml代码 

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2.   

  3. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  

  5.                       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  

  6.     version="3.0" metadata-complete="true">  

  7.     <display-name>SSH</display-name>  

  8.     <context-param>  

  9.         <param-name>contextConfigLocation</param-name>  

  10.         <param-value>  

  11.             classpath*:spring*.xml  

  12.         </param-value>  

  13.     </context-param>  

  14.     <filter>  

  15.         <filter-name>characterEncodingFilter</filter-name>  

  16.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  

  17.         <init-param>  

  18.             <param-name>encoding</param-name>  

  19.             <param-value>UTF-8</param-value>  

  20.         </init-param>  

  21.         <init-param>  

  22.             <param-name>forceEncoding</param-name>  

  23.             <param-value>true</param-value>  

  24.         </init-param>  

  25.     </filter>  

  26.   

  27.     <filter-mapping>  

  28.         <filter-name>characterEncodingFilter</filter-name>  

  29.         <url-pattern>/*</url-pattern>  

  30.     </filter-mapping>  

  31.       

  32.     <filter>  

  33.         <filter-name>struts2</filter-name>  

  34.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  

  35.         <init-param>  

  36.             <param-name>struts.devMode</param-name>  

  37.             <param-value>true</param-value>  

  38.         </init-param>  

  39.         <init-param>  

  40.             <param-name>actionPackages</param-name>  

  41.             <param-value>com.dou.action</param-value>  

  42.         </init-param>  

  43.     </filter>  

  44.     <filter-mapping>  

  45.         <filter-name>struts2</filter-name>  

  46.         <url-pattern>/*</url-pattern>  

  47.     </filter-mapping>  

  48.     <listener>  

  49.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  

  50.     </listener>  

  51.     <welcome-file-list>  

  52.         <welcome-file>index.jsp</welcome-file>  

  53.     </welcome-file-list>  

  54. </web-app>  

    参数contextConfigLocation是指定了spring配置文件的位置,跟“maven搭建 springmvc+mybatis应用”一致,本人也将所有配置文件放在了src/main/resources目下,org.springframework.web.context.ContextLoaderListener是在启动web容器时装配spring配置信息。

         下面是spring的配置,主要就是启动了注解,其代码如下

Xml代码 

  1. <?xml version="1.0" encoding="UTF-8" ?>  

  2. <beans   

  3.     xmlns="http://www.springframework.org/schema/beans"  

  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   

  5.     xmlns:p="http://www.springframework.org/schema/p"  

  6.     xmlns:mvc="http://www.springframework.org/schema/mvc"  

  7.     xmlns:context="http://www.springframework.org/schema/context"  

  8.     xmlns:aop="http://www.springframework.org/schema/aop"   

  9.     xmlns:tx="http://www.springframework.org/schema/tx"  

  10.     xsi:schemaLocation="http://www.springframework.org/schema/beans   

  11.        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  

  12.        http://www.springframework.org/schema/context   

  13.        http://www.springframework.org/schema/context/spring-context-3.0.xsd  

  14.        http://www.springframework.org/schema/tx   

  15.        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  

  16.        http://www.springframework.org/schema/mvc   

  17.        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd      

  18.        http://www.springframework.org/schema/aop  

  19.        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  

  20.       <!--扫描项目目录启用注解-->  

  21.        <context:component-scan base-package="com.qijian"/>  

  22. </beans>  

 

         然后是hibernate的配置,主要是引入属性文件,配置数据源,配置事物管理,其代码如下:

  

Xml代码 

  1. <?xml version="1.0" encoding="UTF-8" ?>  

  2. <beans xmlns="http://www.springframework.org/schema/beans"  

  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  

  4.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"  

  5.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  

  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   

  7.        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  

  8.        http://www.springframework.org/schema/context   

  9.        http://www.springframework.org/schema/context/spring-context-3.0.xsd  

  10.        http://www.springframework.org/schema/tx   

  11.        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  

  12.        http://www.springframework.org/schema/mvc   

  13.        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd      

  14.        http://www.springframework.org/schema/aop  

  15.        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  

  16.   

  17.     <!-- 引入数据库属性文件 -->  

  18.     <context:property-placeholder location="classpath*:config.properties" />  

  19.     <!--c3p0数据库连接池 -->  

  20.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  

  21.         destroy-method="close">  

  22.         <property name="driverClass" value="${driverClassName}" />  

  23.         <property name="jdbcUrl" value="${jdbc_url}" />  

  24.         <property name="user" value="${jdbc_username}" />  

  25.         <property name="password" value="${jdbc_password}" />  

  26.         <property name="initialPoolSize" value="1" />  

  27.         <property name="minPoolSize" value="0" />  

  28.         <property name="maxPoolSize" value="50" />  

  29.         <property name="acquireIncrement" value="5" />  

  30.         <property name="maxIdleTime" value="10" />  

  31.         <property name="maxStatements" value="0" />  

  32.     </bean>  

  33.   

  34.     <bean id="sessionFactory"  

  35.         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  

  36.         <property name="hibernateProperties">  

  37.             <props>  

  38.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  

  39.                 <prop key="hibernate.show_sql">true</prop>  

  40.                 <prop key="hiberante.format_sql">true</prop>  

  41.                 <!-- 延迟加载 -->  

  42.                 <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>  

  43.                 <!--是否允许Hibernate用JDBC的可滚动的结果集,对分页时的设置非常有帮助 -->  

  44.                 <prop key="hibernate.jdbc.use_scrollable_resultset">false</prop>  

  45.             </props>  

  46.         </property>  

  47.         <property name="dataSource">  

  48.             <ref bean="dataSource" />  

  49.         </property>  

  50.         <property name="packagesToScan">  

  51.             <list>  

  52.                 <value>com.dou.dao.po</value>  

  53.             </list>  

  54.         </property>  

  55.         <!-- 设置数据源是否使用事务包装 -->  

  56.         <property name="useTransactionAwareDataSource">  

  57.             <value>true</value>  

  58.         </property>  

  59.     </bean>  

  60.   

  61.     <!-- hibernate事物开始 -->  

  62.     <bean id="hibernateTransactionManager"  

  63.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  

  64.         <property name="sessionFactory">  

  65.             <ref local="sessionFactory" />  

  66.         </property>  

  67.     </bean>  

  68.     <!-- 注解式事物 -->  

  69.     <!-- <tx:annotation-driven transaction-manager="hibernateTransactionManager"/> -->  

  70.   

  71.     <!--声明性事物 -->  

  72.     <tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">  

  73.         <tx:attributes>  

  74.             <tx:method name="store*" propagation="REQUIRED" />  

  75.             <tx:method name="add*" propagation="REQUIRED" />  

  76.             <tx:method name="save*" propagation="REQUIRED" />  

  77.             <tx:method name="update*" propagation="REQUIRED" />  

  78.             <tx:method name="delete*" propagation="REQUIRED" />  

  79.         </tx:attributes>  

  80.     </tx:advice>  

  81.     <aop:config>  

  82.         <aop:pointcut id="allServiceMethod"  

  83.             expression="execution(* com.qijian.service.impl.*.*(..))" />  

  84.         <aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice" />  

  85.     </aop:config>  

  86.     <!-- hibernate事物结束 -->  

  87.   

  88. </beans>  

 

     这里的事物用的是spring为hibernate提供的HibernateTransactionManager,当然你也可以用spring 的DataSourceTransactionManager管理事物,详情请看“maven搭建 springmvc+mybatis应用”一文。

    最后还有一个数据库连接信息的properties文件,主要是连接数据库的用户名密码等,代码如下:

     

Properties代码 

  1. driverClassName=com.mysql.jdbc.Driver  

  2. jdbc_url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull  

  3. jdbc_username=root  

  4. jdbc_password=king  

 

    到此基本配置就差不多了,然后就可以编写代码跑程序了,祝大家一切顺利

你可能感兴趣的:(spring,maven,Hibernate,框架,struts2)