SpringMVC+JPA+SpringData配置

pom.xml

<properties>
	    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	    <jdk.version>1.7</jdk.version>
	    <junit.version>4.12</junit.version>
	    <spring.version>4.1.1.RELEASE</spring.version>
	    <jackson.version>2.4.3</jackson.version>
	    <hessian.version>4.0.38</hessian.version>
	    <log4j.version>1.2.14</log4j.version>
	    <slf4j.log4j12.version>1.7.7</slf4j.log4j12.version>
	    <aspectj.version>1.8.3</aspectj.version>
	    <httpclient.version>4.3.6</httpclient.version>
	    <spring.data.jpa.version>1.7.0.RELEASE</spring.data.jpa.version>
	    <hibernate.version>4.3.7.Final</hibernate.version>
	    <oracle.jdbc.version>11.2.0.RELEASE</oracle.jdbc.version>
	    <dubbo.version>2.5.3</dubbo.version>
	    <freemarker.version>2.3.21</freemarker.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>

		<!-- spring -->
	    <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</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-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>

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

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

		<!-- spring mvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>

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

		<!-- JackSon -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>${jackson.version}</version>
		</dependency>

		<!-- c3po -->
	    <dependency>
			<groupId>com.mchange</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.5-pre9</version>
		</dependency>

		<!-- oracle jdbc -->
		<dependency>
	  		<groupId>com.oracle</groupId>
	  		<artifactId>ojdbc6</artifactId>
	  		<version>${oracle.jdbc.version}</version>
		</dependency>
		<!-- Hibernate+JPA -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>${hibernate.version}</version>
		</dependency>

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

		<!-- hessian -->
	    <dependency>
			<groupId>com.caucho</groupId>
			<artifactId>hessian</artifactId>
			<version>${hessian.version}</version>
		</dependency>

		<dependency>
			<groupId>com.sun.mail</groupId>
			<artifactId>javax.mail</artifactId>
			<version>1.5.2</version>
		</dependency>

		<!-- log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${slf4j.log4j12.version}</version>
		</dependency>

		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>${httpclient.version}</version>
		</dependency>

		<dependency>
			<groupId>com.tiamaes.gjds</groupId>
			<artifactId>gjds-util</artifactId>
			<version>1.0.0-SNAPSHOT</version>
		</dependency>

		<dependency>
			<groupId>com.tiamaes.gjds.dxp</groupId>
			<artifactId>gjds-dxp-api</artifactId>
			<version>1.0.0-SNAPSHOT</version>
		</dependency>

		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.21</version>
		</dependency>
	</dependencies>

	<build>
		<finalName>gjds-dxp</finalName>
		<plugins>
	        <plugin>
	            <groupId>org.apache.maven.plugins</groupId>
	            <artifactId>maven-compiler-plugin</artifactId>
	            <version>2.0.2</version>
	            <configuration>
	                <source>${jdk.version}</source>
	                <target>${jdk.version}</target>
	            </configuration>
	        </plugin>
    	</plugins>
	</build>

web.xml

<!-- 载入log4j配置文件 -->
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:log4j.properties</param-value>
	</context-param>

	<!--Spring默认刷新Log4j配置文件的间隔,单位为millisecond-->
	<context-param>
		<param-name>log4jRefreshInterval</param-name>
		<param-value>60000</param-value>
	</context-param>

	<!--Spring log4j Config loader-->
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>

	<!-- Spring 刷新Introspector防止内存泄露 -->
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:applicationContext*.xml
		</param-value>
	</context-param>

	<!-- springMVC配置 -->
	<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!--Spring的ApplicationContext 载入 -->
	<listener>
	  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- spring字符过滤器 -->
	<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>

	<context-param>
	  <param-name>webAppRootKey</param-name>
	  <param-value>gjds-dxp.root</param-value>
	</context-param>

	<session-config>
		<session-timeout>30</session-timeout>
	</session-config>

springMVC-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

	<mvc:annotation-driven conversion-service="conversionService" >
		<mvc:message-converters>
			<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>application/json; charset=UTF-8</value>
                   		<value>application/x-www-form-urlencoded; charset=UTF-8</value>
					</list>
				</property>
				<property name="prettyPrint" value="true"/>
				<property name="objectMapper">
                    <bean class="com.fasterxml.jackson.databind.ObjectMapper"
                    	p:serializationInclusion="NON_NULL">

                        <property name="dateFormat">
                            <bean class="java.text.SimpleDateFormat">
                                <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
                            </bean>
                        </property>
                    </bean>
                </property>
			</bean>
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>text/plain;charset=UTF-8</value>
						<value>text/html;charset=UTF-8</value>
					</list>
				</property>
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>

	<!-- <mvc:view-controller path="/" view-name="redirect:/login"/> -->

	<context:annotation-config />

	<context:component-scan base-package="com.tiamaes.**.controller" >
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
	</context:component-scan>

	<!-- 控制日期格式的 -->
	<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<!--<property name="converters">
			<list>
				<bean class="com.zrhis.base.converter.DateConverter" ></bean>
			</list>
		</property>
	    -->
	</bean>

	<!-- p:suffix=".jsp" -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/" p:suffix=".jsp">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
	</bean>

    <mvc:default-servlet-handler/>

	<aop:aspectj-autoproxy proxy-target-class="true" />

    <import resource="classpath:hessian.xml"/>
</beans>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/task
		http://www.springframework.org/schema/task/spring-task-4.0.xsd
		http://www.springframework.org/schema/data/jpa
		http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

	<context:property-placeholder location="classpath:jdbc.properties"/>

	<context:annotation-config />
	<context:component-scan base-package="com.tiamaes.**">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
		<property name="driverClass" value="${dataSource.className}" />
		<property name="jdbcUrl" value="${dataSource.url}" />
		<property name="user" value="${dataSource.username}" />
		<property name="password" value="${dataSource.password}" />

		<!-- 连接关闭时默认将所有未提交的操作回滚。Default: false -->
		<property name="autoCommitOnClose" value="true" />
		<!-- 当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出 SQLException,如设为0则无限期等待。单位毫秒。Default: 0-->
		<property name="checkoutTimeout" value="${cpool.checkoutTimeout}" />
		<!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize" value="${cpool.minPoolSize}" />
		<!-- 连接池中保留的最小连接数 -->
		<property name="minPoolSize" value="${cpool.minPoolSize}" />
		<!-- 连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize" value="${cpool.maxPoolSize}" />
		<!-- 最大空闲时间,36000秒(10小时)内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime" value="${cpool.maxIdleTime}" />
		<property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}" />
		<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		<property name="acquireIncrement" value="${cpool.acquireIncrement}" />
		<!-- 定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
        <property name="acquireRetryAttempts" value="${cpool.acquireRetryAttempts}" />
        <!-- 两次连接中间隔时间,单位毫秒。Default: 1000 -->
        <property name="acquireRetryDelay" value="${cpool.acquireRetryDelay}" />
	</bean>

	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="persistenceUnitName" value="Oracle" />
		<!-- <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" /> -->
		<property name="persistenceProvider" ref="persistenceProvider" />
		<property name="jpaDialect" ref="jpaDialect" />
		<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
		<property name="packagesToScan" value="com.tiamaes.gjds.dxp.bean"/>
		<property name="jpaProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.temp.use_jdbc_metadata_defaults">${hibernate.temp.use_jdbc_metadata_defaults}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
				<prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
				<prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
				<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
			</props>
		</property>
	</bean>

	<!-- <bean class="org.hibernate.jpa.HibernatePersistenceProvider" /> -->
	<bean id="persistenceProvider" class="org.hibernate.jpa.HibernatePersistenceProvider" />
	<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
	<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
		<property name="database" value="ORACLE"></property>
	</bean>

	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>

	<!-- 事务装配 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED"/>
			<tx:method name="save*" propagation="REQUIRED"/>
			<tx:method name="insert*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="query*" propagation="NOT_SUPPORTED"/>
			<tx:method name="get*" propagation="NOT_SUPPORTED"/>
			<tx:method name="find*" propagation="NOT_SUPPORTED"/>
			<tx:method name="*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>

	<aop:config>
		<aop:pointcut expression="execution(* com.tiamaes.**.service.*.*(..))" id="txPointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
	</aop:config>

	<tx:annotation-driven transaction-manager="transactionManager" />

	<aop:aspectj-autoproxy />

	<jpa:repositories base-package="com.tiamaes.gjds.dxp.repository"
		entity-manager-factory-ref="entityManagerFactory"
		transaction-manager-ref="transactionManager"/>

	<task:annotation-driven />

	<bean id="javascriptProcesser" class="com.tiamaes.gjds.script.JavascriptProcesser" />

	<bean id="tiamaesMailSender" class="com.tiamaes.gjds.dxp.sender.mail.TiamaesMailSender" />

</beans>

jdbc.properties

dataSource.className=oracle.jdbc.OracleDriver
dataSource.url=jdbc\:oracle\:thin\:@//192.168.13.111\:1521/gongjiaodushi
dataSource.username=GJDS
dataSource.password=GJDS

# Time to wait for an open connection before timing out
# (in milliseconds)
cpool.checkoutTimeout=5000

# Connection pool size
cpool.minPoolSize=5
cpool.maxPoolSize=20

# How long to keep unused connections around(in seconds)
# Note: MySQL times out idle connections after 8 hours(28,800 seconds)
# so ensure this value is below MySQL idle timeout
cpool.maxIdleTime=36000

# How long to hang on to excess unused connections after traffic spike
# (in seconds)
cpool.maxIdleTimeExcessConnections=1800

# Acquiring new connections is slow, so eagerly retrieve extra connections
# when current pool size is reached
cpool.acquireIncrement=5

cpool.acquireRetryAttempts=0
cpool.acquireRetryDelay=1000

hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.temp.use_jdbc_metadata_defaults=false
hibernate.show_sql=false
hibernate.format_sql=false
hibernate.max_fetch_depth=3
hibernate.jdbc.fetch_size=18
hibernate.jdbc.batch_size=10
时间: 2024-11-15 01:31:58

SpringMVC+JPA+SpringData配置的相关文章

Spring、SpringMVC、SpringData + JPA 整合详解

原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7759874.html ---------------------------------------------------------------------------------------------------------------------------------------------------------- 笔记中提供了大量的代码示例,需要说明的是,大部分代码示例都是本

springMVC+jpa配置之简单案例

搭建springMVC+jpa的亲身经历,看着网上的博客,自己摸索着搭建框架结果错误一大堆.现在把流程走一遍,以便以后方便查看. 其中我遇到这样的一个问题:直接启动tomcat运行保存实体能通过,但是通过单元测试就报一下错误: Caused by: javax.validation.ValidationException: Unable to instantiate Configuration. 解决方法: 在persistence.xml中添加这个<property name="java

spring+springMVC+JPA配置详解

SpringMVC是越来越火,自己也弄一个Spring+SpringMVC+JPA的简单框架. 1.搭建环境. 1)下载Spring3.1.2的发布包:Hibernate4.1.7的发布包(没有使用hibernate的API,只是使用了它对JPA的实现):下载 BoneCP 连接池框架及其依赖的jar,下载缓存框架ehcache,全部所用到的jar包如下: ? antlr-2.7.7.jar bonecp-0.7.1.RELEASE.jar bonecp-provider-0.7.1-rc2.j

SpringMVC框架入门配置 IDEA下搭建Maven项目

,初衷:本人初学SpringMVC的时候遇到各种稀奇古怪的问题,网上各种技术论坛上的帖子又参差不齐,难以一步到位达到配置好的效果,这里我将我配置的总结写到这里供大家初学SpringMVC的同僚们共同学习使用! 关于SpringMVC的介绍我就不在此赘述了,想必每一个配置Spring的人都已经或多或少了解过一些SpringMVC的基础知识,可能有的人用的myeclipse或者eclipse进行项目的搭建,我会告诉你这里步骤是很相似的,在myeclipse里面需要建立web项目,然后加入Spring

SpringMVC+springSecurity+flexPaper 配置--类似百度文库在线预览

背景:现在项目需要做一个类似百度文库的在线预览功能,在网上找了下很多人推荐使用FlexPaper,所以今天尝试学习了FlexPaper顺便集成到现有的框架中 由于网上目前的说的都不是很详细,所以现在记录在此,希望对需要的人有所帮助 准备:1. FlexPaper_2.2.4.zip 下载地址:http://flexpaper.devaldi.com/download/             2.swftools 下载地址:http://www.swftools.org/download.htm

【JPA】两种不同的实现jpa的配置方法

两种不同的实现jpa的配置方法 第一种: com.mchange.v2.c3p0.ComboPooledDataSource datasource.connection.driver_class=com.mysql.jdbc.Driver <!-- Where to find repositories --> <jpa:repositories base-package="org.springframework.data.jpa.example.repository.simpl

idea spring+springmvc+mybatis环境配置整合详解

idea spring+springmvc+mybatis环境配置整合详解 1.配置整合前所需准备的环境: 1.1:jdk1.8 1.2:idea2017.1.5 1.3:Maven 3.5.2 2.查看idea中是否安装Maven插件: 2.1:File --> Settings --> Plugins 2.2:如下图所示的步骤进行操作(注:安装完插件,idea会重新启动) 3.idea创建Maven项目的步骤 4.搭建目录结构 下图就是我搭建Maven项目之后,添加对应的目录和文件 5.p

springmvc jpa

昨天帮同学搭建了一个springmvc+jpa+beetl模板引擎的项目环境,供参考. https://files.cnblogs.com/files/startnow/lntu-demo.zip 数据库mysql , 名test01 表结构: 项目结构: 原文地址:https://www.cnblogs.com/startnow/p/8975590.html

SpringMVC加载配置Properties文件的几种方式

最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大, 网上介绍Controller参数绑定.URL映射的文章都很多了,写这篇博客主要总结一下SpringMVC加载配置Properties文件的几种方式 通过读取Config文件的配置例如: Map<String, String> group = ConfigurationManager.GetConfiguration("config1"); this.setBcp