关于在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的代码替换原来的。

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

最新文章

  1. GraphicsMagick+Im4Java在windows和linux下的配置
  2. 三种Singleton的实现方式
  3. SQL Server 索引(index) 和 视图(view) 的简单介绍和操作
  4. 【原】MAC显示隐藏文件夹命令
  5. 一个通用的DAO模型实现增删改查
  6. 轻松入门React和Webpack
  7. oc-15-枚举结构体
  8. 如何启用第三方Chrome插件
  9. MyBatisNet 学习
  10. mbos之动态图表设计
  11. Cell
  12. iframe 加form提交数据
  13. 《SpringMVC从入门到放肆》十二、SpringMVC自定义类型转换器
  14. 音乐出身的妹纸,零基础学习JAVA靠谱么
  15. css一些基础效果
  16. JavaScript基础知识(DOM)
  17. SpringBoot使用端口运行
  18. android读取大图片并缓存
  19. iis7中session丢失的解决方法小结
  20. sougoupinyin for linux 安装步骤(精简版)

热门文章

  1. FineReport性能调优的一些办法
  2. iPhone实现自定义多选列表
  3. csdn我的blog成长轨迹(好吧我是闲的蛋疼)
  4. vue2.0-基于elementui换肤[自定义主题]
  5. jQuery学习小结
  6. JAVA面试题集
  7. AbstractQueuedSynchronizer 原理分析 - Condition 实现原理
  8. Day19 Django
  9. AngularJS数据绑定中数据监控的机制说明
  10. sudo pip install MySQLdb