玩转spring mvc(四)---在spring MVC中整合JPA

关于在Spring MVC中整合JPA是在我的上一篇关于spring mvc基本配置基础上进行的,所以大家先参考一下我的上一篇文章:http://blog.csdn.net/u012116457/article/details/43528111

接下来是需要新添加的一些文件:

jdbc.properties:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/tmos?useUnicode\=true&characterEncoding\=utf-8&autoReconnect\=true
jdbc.user=root
jdbc.password=root
jdbc.dialect=org.hibernate.dialect.MySQL5Dialect
jdbc.show_sql=false
jdbc.format_sql=true
#jdbc.dialect=org.hibernate.dialect.MySQLInnoDBDialect
jdbc.initialPoolSize=2
jdbc.maxPoolSize=200
jdbc.batch_size=20
jdbc.hbm2ddl.auto=update
jdbc.query.substitutions=true 1, false 0, yes ''Y'', no ''N''

jpaConfig.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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:mvc="http://www.springframework.org/schema/mvc"	
	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/aop http://www.springframework.org/schema/aop/spring-aop-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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd			
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">

    <context:annotation-config/>
	<context:component-scan base-package="module" />  <!-- 此处根据项目而定 -->
	
	<!-- 配置占位符 -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="classpath:/jdbc.properties" />
	</bean>

	<!-- 数据源 -->
	<!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" abstract="false" 
        lazy-init="default" autowire="default"> -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}"/>
		<property name="url" value="${jdbc.url}"/>
		<property name="username" value="${jdbc.user}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	<!-- 配置一个Factory -->
     <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceProvider" ref="persistenceProvider" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="jpaDialect" ref="jpaDialect" />
 
        <property name="jpaProperties">
            <props>
               <prop key="hibernate.dialect">${jdbc.dialect}</prop>
			   <prop key="hibernate.hbm2ddl.auto">${jdbc.hbm2ddl.auto}</prop>
			   <!-- 避免重复打印sql -->
			   <prop key="hibernate.show_sql">${jdbc.show_sql}</prop>
			   <prop key="hibernate.format_sql">${jdbc.format_sql}</prop>
            </props>
        </property>
        
        <property name="packagesToScan">
			<list>
				<value>module.app.entity</value>
				<value>module.system.entity</value>
		    </list>
		</property>	       
    </bean>
	
	<bean id="persistenceProvider"
        class="org.hibernate.ejb.HibernatePersistence" />
         
    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="MYSQL" />
    </bean>
     
    <bean id="jpaDialect"
        class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
 
    <jpa:repositories base-package="module"
        entity-manager-factory-ref="entityManagerFactory" 
        transaction-manager-ref="txManager" />
        
    <!-- <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
        p:cache-manager-ref="ehcache">
        </bean>    
        
    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
        p:config-location="classpath:spring-ehcache.xml" />  -->      
   

    <bean id="txManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory"
            ref="entityManagerFactory" />
    </bean>
	
	<tx:annotation-driven transaction-manager="txManager" />


</beans>


这样就完成了spring MVC中整合JPA  ,在这里大家可以下载本项目,里边包含一些测试代码以及用到的jar包:http://download.csdn.net/detail/u012116457/8426211

这个代码包里的jpaConfig.xml进行了修改,大家可以用这篇文章里贴的jpaConfig.xml的代码替换原来的。





版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/dingxiaoyue/p/4931772.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值