java企业开发五:springmvc-jpa(hibernate实现)

先在spring集成hibernate,实现JPA.再处理MVC.

1.添加依赖jar.
A.spring部分:添加spring-webmvc会将core,beans,context,web等加进来.orm会将tx,jdbc加进来.
B.hibernate部分:添加hibernate-entitymanager会将hibernate必须的添加进来.c3p0,mysql驱动也要加进来..
C.web部分:添加jackjson主要生成json,jstl在jsp页面使用,servlet基本api

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<spring.version>3.2.7.RELEASE</spring.version>
		<hibernate.version>4.3.1.Final</hibernate.version>
		<tomcat.version>7.0.47</tomcat.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>${hibernate.version}</version>
		</dependency>
		<dependency>
			<groupId>c3p0</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.1.2</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.26</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.3.1</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp.jstl</groupId>
			<artifactId>javax.servlet.jsp.jstl-api</artifactId>
			<version>1.2.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-servlet-api</artifactId>
			<version>${tomcat.version}</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>

2.applicationContext.xml 比较简单,不解析,不理解spring集成jpa,可参考java企业开发四:ssh+JPA(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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	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.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
	<context:component-scan base-package="com.test.service.impl" />
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh2?useUnicode=true&characterEncoding=UTF-8" />
		<property name="user" value="root" />
		<property name="password" value="123456" />
		<property name="minPoolSize" value="1" />
		<property name="maxPoolSize" value="300" />
		<property name="maxIdleTime" value="60" />
		<property name="acquireIncrement" value="5" />
		<property name="idleConnectionTestPeriod" value="60" />
	</bean>
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="packagesToScan" value="com.test.domain" />
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="showSql" value="false" />
				<property name="generateDdl" value="true" />
			</bean>
		</property>
	</bean>
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" />
	
	<context:component-scan base-package="com.test.web" />
	<mvc:resources location="resources/" mapping="resources/**"/>
	<mvc:resources location="/" mapping="**/*.html"/>
	<!-- 继承HandlerInterceptorAdapter重写preHandle实现权限拦截 
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**"/>
			<bean class="com.test.AuthInterceptor"/>
		</mvc:interceptor>
	</mvc:interceptors>
	 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/html;charset=UTF-8</value>
						</list>
					</property>
				</bean>
			</list>
		</property>
	</bean>
	<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>
3.写domain,service,web层代码.

4.web.xml和jsp.  一般,特定的spring mvc组件是由DispatcherServlet的ApplicationContext接口初始化的.而剩下的其它组件是通过ContextLoaderListener加载的.更重要的是要知道,ApplicationContext的bean(例如通过DispatcherServlet创建的bean)能引用它的ApplicationContext的bean(例如通过ContextLoaderListener创建的bean).而ApplicationContext不能引用ApplicationContext的bean. 下面是将所有的bean都放入到DispatcherServlet的ApplicationContext,这并不是最好的做法!最好还是将spring mvc特定的组件和一般的组件分开...就像前所说的,一般的组件由ContextLoaderListener加载初始化.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"
	metadata-complete="true">
	<display-name>Tomcat Manager Application</display-name>
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationContext.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcher</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>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
</web-app>

本文源代码: http://download.csdn.net/detail/xiejx618/6938479

以上是基于xml的配置,如果使用spring mvc 3.2+和servelt 3+容器(比如tomcat8),那么web.xml和applicationContext.xml都不是必须的,可使用基于注解的配置:

基于注解配置的集成例子源代码:http://download.csdn.net/detail/xiejx618/7050485



你可能感兴趣的:(java企业开发五:springmvc-jpa(hibernate实现))