maven+spring4+hibernate4 搭建

maven+spring4+hibernate4 搭建

mavenWEB项目搭建

参考我转的帖子http://my.oschina.net/u/204616/blog/411702

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.wangwei</groupId>
  <artifactId>MyWebApp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>MyWebApp Maven Webapp</name>  
  <url>http://maven.apache.org</url> 
  <properties>  
        <junit.version>4.10</junit.version>  
        <log4j.version>2.0</log4j.version>  
        <mysql.jdbc.version>5.1.21</mysql.jdbc.version>  
        <hibernate.version>4.1.4.Final</hibernate.version>  
        <commons.fileupload.version>1.2.1</commons.fileupload.version>  
        <javassist.version>3.12.1.GA</javassist.version>  
        <cxf.version>3.0.2</cxf.version>  
        <spring.version>4.1.3.RELEASE</spring.version>  
    </properties>  
  <dependencies>  
          
        <!-- junit包 -->  
        <dependency>  
            <groupId>junit</groupId>  
            <artifactId>junit</artifactId>  
            <version>${junit.version}</version>  
            <scope>test</scope>  
        </dependency>  
          
        <!-- cxf -->  
        <dependency>  
            <groupId>org.apache.cxf</groupId>  
            <artifactId>cxf-core</artifactId>  
            <version>${cxf.version}</version>  
        </dependency>  
          
        <!-- 读取字节码包 -->  
        <dependency>  
            <groupId>javassist</groupId>  
            <artifactId>javassist</artifactId>  
            <version>${javassist.version}</version>  
        </dependency>
	<!-- jsp --> 
	<dependency>
		<groupId>jstl</groupId>
		<artifactId>jstl</artifactId>
		<version>1.2</version>
	</dependency>

	<dependency>
		<groupId>taglibs</groupId>
		<artifactId>standard</artifactId>
		<version>1.1.2</version>
	</dependency>



        <!-- commons配置 -->  
        <dependency>  
            <groupId>commons-fileupload</groupId>  
            <artifactId>commons-fileupload</artifactId>  
            <version>${commons.fileupload.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>commons-io</groupId>  
            <artifactId>commons-io</artifactId>  
            <version>1.4</version>  
        </dependency>  
  
        <!-- log4j的依赖 -->  
        <dependency>  
            <groupId>org.apache.logging.log4j</groupId>  
            <artifactId>log4j-api</artifactId>  
            <version>${log4j.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.logging.log4j</groupId>  
            <artifactId>log4j-core</artifactId>  
            <version>${log4j.version}</version>  
        </dependency>  
  
        <!-- mysql的依赖 -->  
        <dependency>  
            <groupId>mysql</groupId>  
            <artifactId>mysql-connector-java</artifactId>  
            <version>${mysql.jdbc.version}</version>  
        </dependency>  
  
  
        <!-- hibernate部分 -->  
        <dependency>  
            <groupId>org.hibernate</groupId>  
            <artifactId>hibernate-core</artifactId>  
            <version>${hibernate.version}</version>  
        </dependency>  
  
        <!-- c3p0 -->  
        <dependency>  
            <groupId>c3p0</groupId>  
            <artifactId>c3p0</artifactId>  
            <version>0.9.1.2</version>  
        </dependency>  
          
          
        <!-- spring -->  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-core</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-web</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-webmvc</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-jdbc</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-aop</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-orm</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-aspects</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context-support</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
          
        <!-- aspectj -->  
        <dependency>  
            <groupId>org.aspectj</groupId>  
            <artifactId>aspectjrt</artifactId>  
            <version>1.8.4</version>  
        </dependency>  
        <dependency>  
            <groupId>org.aspectj</groupId>  
            <artifactId>aspectjweaver</artifactId>  
            <version>1.8.4</version>  
        </dependency>  
        <dependency>  
            <groupId>org.aspectj</groupId>  
            <artifactId>aspectjlib</artifactId>  
            <version>1.6.2</version>  
        </dependency>  
          
        <!-- quartz -->  
        <dependency>  
            <groupId>org.quartz-scheduler</groupId>  
            <artifactId>quartz</artifactId>  
            <version>2.2.0</version>  
        </dependency>  
          
    </dependencies>  
</project>

WEB.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>json_test</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 加载所有的配置文件 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath*:spring-*.xml</param-value>
  </context-param>
  
  <!-- 配置Spring监听 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 配置SpringMVC -->
  <servlet>
  	<servlet-name>springMVC</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath*:spring-mvc.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springMVC</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 配置字符集(解决乱码问题) -->
  <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>
  	<init-param>
  		<param-name>forceEncoding</param-name>
  		<param-value>true</param-value>
  	</init-param>
  </filter>
  <filter-mapping>
  	<filter-name>encodingFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- (Hibernate配置)配置Session -->
  <filter>
  	<filter-name>openSession</filter-name>
  	<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>openSession</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

spring-beans.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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean id="userDao" class="com.tgb.dao.UserDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<bean id="userManagerBase" class="com.tgb.manager.UserManagerImpl">
		<property name="userDao" ref="userDao"></property>
	</bean>
	
	<!-- 此处为代理 -->
	<bean name="userManager" parent="transactionProxy">
		<property name="target" ref="userManagerBase"></property>
	</bean>	
</beans>

spring-common.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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- 配置数据源 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8"></property>
		<property name="username" value="root"></property>
		<property name="password" value="1qazse4"></property>
	</bean>
	
	<!-- 配置SessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
			</props>
		</property>
		<property name="packagesToScan" value="com.tgb.entity" />
	</bean>
	
	<!-- 配置一个事务管理器 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	
	<!-- 配置事务,使用代理的方式 -->
	<bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">  
	    <property name="transactionManager" ref="transactionManager"></property>  
	    <property name="transactionAttributes">  
	        <props>  
	            <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>  
	            <prop key="modify*">PROPAGATION_REQUIRED,-myException</prop>  
	            <prop key="del*">PROPAGATION_REQUIRED</prop>  
	            <prop key="*">PROPAGATION_REQUIRED</prop>  
	        </props>  
	    </property>  
	</bean> 
</beans>

spring-mvc.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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.2.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
	
	<!-- 注解扫描包 -->
	<context:component-scan base-package="com.tgb" />

	<!-- 开启注解 -->
	<mvc:annotation-driven />
	
	<!-- 静态资源(js/image)的访问 -->
	<mvc:resources mapping="/js/**" location="/resource/js/"/>  
    <mvc:resources mapping="/css/**" location="/resource/css/"/>  
    <mvc:resources mapping="/images/**" location="/resource/images/"/> 
    
    
	<!-- 定义视图解析器 -->	
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

http://pan.baidu.com/s/1bnezkyb

你可能感兴趣的:(maven+spring4+hibernate4 搭建)