maven搭建 SSH

Devliyangjunfei 2014-10-21

             上次讲述了maven搭建springmvc+mybatis框架,若未浏览却对此感兴趣的话,请移步这里:maven搭建 springmvc+mybatis应用。

             本次主要讲解使用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文件如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.qijian</groupId>
	<artifactId>SSH</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>SSH Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<!-- 构件的版本 -->
	<properties>
		<junit.version>3.8.1</junit.version>
		<logback.version>1.1.2</logback.version>
		<struts.version>2.3.8</struts.version>
		<spring.version>3.0.5.RELEASE</spring.version>
		<hibernate.annotations.version>3.5.6-Final</hibernate.annotations.version>
		<aspectj.version>1.8.1</aspectj.version>
		<c3p0.version>0.9.1.2</c3p0.version>
		<mysql.version>5.1.31</mysql.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>${logback.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-spring-plugin</artifactId>
			<version>${struts.version}</version>
		</dependency>
			<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-json-plugin</artifactId>
			<version>${struts.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-convention-plugin</artifactId>
			<version>${struts.version}</version>
		</dependency>
		
		

		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>${aspectj.version}</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>${aspectj.version}</version>
		</dependency>

	
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<!-- 用c3p0连接池去管理数据库连接 -->
		<dependency>
			<groupId>c3p0</groupId>
			<artifactId>c3p0</artifactId>
			<version>${c3p0.version}</version>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-annotations</artifactId>
			<version>${hibernate.annotations.version}</version>
		</dependency>
		
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.version}</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>SSH</finalName>
	</build>
</project>

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

   

<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		<init-param>
			<param-name>struts.devMode</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>actionPackages</param-name>
			<param-value>com.qijian.action</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

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

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

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0" metadata-complete="true">
	<display-name>SSH</display-name>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:spring*.xml
		</param-value>
	</context-param>
	<filter>
		<filter-name>characterEncodingFilter</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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>characterEncodingFilter</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>
		<init-param>
			<param-name>struts.devMode</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>actionPackages</param-name>
			<param-value>com.dou.action</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

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

         下面是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:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
      <!--扫描项目目录启用注解-->
       <context:component-scan base-package="com.qijian"/>
</beans>

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

  

<?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:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	<!-- 引入数据库属性文件 -->
	<context:property-placeholder location="classpath*:config.properties" />
	<!--c3p0数据库连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${driverClassName}" />
		<property name="jdbcUrl" value="${jdbc_url}" />
		<property name="user" value="${jdbc_username}" />
		<property name="password" value="${jdbc_password}" />
		<property name="initialPoolSize" value="1" />
		<property name="minPoolSize" value="0" />
		<property name="maxPoolSize" value="50" />
		<property name="acquireIncrement" value="5" />
		<property name="maxIdleTime" value="10" />
		<property name="maxStatements" value="0" />
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hiberante.format_sql">true</prop>
				<!-- 延迟加载 -->
				<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
				<!--是否允许Hibernate用JDBC的可滚动的结果集,对分页时的设置非常有帮助 -->
				<prop key="hibernate.jdbc.use_scrollable_resultset">false</prop>
			</props>
		</property>
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="packagesToScan">
			<list>
				<value>com.dou.dao.po</value>
			</list>
		</property>
		<!-- 设置数据源是否使用事务包装 -->
		<property name="useTransactionAwareDataSource">
			<value>true</value>
		</property>
	</bean>

	<!-- hibernate事物开始 -->
	<bean id="hibernateTransactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>
	<!-- 注解式事物 -->
	<!-- <tx:annotation-driven transaction-manager="hibernateTransactionManager"/> -->

	<!--声明性事物 -->
	<tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">
		<tx:attributes>
			<tx:method name="store*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut id="allServiceMethod"
			expression="execution(* com.qijian.service.impl.*.*(..))" />
		<aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice" />
	</aop:config>
	<!-- hibernate事物结束 -->

</beans>

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

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

     

driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc_username=root
jdbc_password=king

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

     

相关推荐