玩转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的代码替换原来的。
版权声明:本文为博主原创文章,未经博主允许不得转载。
玩转spring mvc(四)---在spring MVC中整合JPA的更多相关文章
- Spring(四)Spring与数据库编程
Spring最重要的功能毫无疑问就是操作数据.数据库的百年城是互联网编程的基础,Spring为开发者提供了JDBC模板模式,那就是它自身的JdbcTemplate.Spring还提供了Transact ...
- 复习Spring第四课---Spring对国际化的支持
其实国际化这东西很少使用,之前也就是粗略的学了下,趁今天有空,拿出来稍微写写.以前学android开发的时候,类似于多语言的版本.差别就是一个是手机打开,一个是浏览器打开,本质是一样的. 在Sprin ...
- Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)
参考文章:http://www.cnblogs.com/hongwz/p/5764917.html 一.什么是AOP AOP(Aspect Oriented Programming),即面向切面编程, ...
- 攻城狮在路上(贰) Spring(四)--- Spring BeanFactory简介
BeanFactory时Spring框架最核心的接口,它提供了高级IoC的配置机制,使管理不同类型的Java对象成为了可能.我们一般称BeanFactory为IoC容器.BeanFactory是Spr ...
- 深入浅出Spring(四) Spring实例分析
上次的博文中 深入浅出Spring(二) IoC详解 和 深入浅出Spring(三) AOP详解中,我们分别介绍了一下Spring框架的两个核心一个是IoC,一个是AOP.接下来我们来做一个Sprin ...
- Spring点滴四:Spring Bean生命周期
Spring Bean 生命周期示意图: 了解Spring的生命周期非常重要,我们可以利用Spring机制来定制Bean的实例化过程. -------------------------------- ...
- [置顶] 深入浅出Spring(四) Spring实例分析
上次的博文中 深入浅出Spring(二) IoC详解 和 深入浅出Spring(三) AOP详解中,我们分别介绍了一下Spring框架的两个核心一个是IoC,一个是AOP.接下来我们来做一个Sprin ...
- Spring ( 四 )Spring的AOP动态代理、切面编程
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.AOP切面编程 1.什么是AOP AOP是面向切面编程.全称:Aspect Oriented Pro ...
- spring学习四:Spring中的后置处理器BeanPostProcessor
BeanPostProcessor接口作用: 如果我们想在Spring容器中完成bean实例化.配置以及其他初始化方法前后要添加一些自己逻辑处理.我们需要定义一个或多个BeanPostProcesso ...
随机推荐
- LeetCode之旅(19)-Power of Two
题目 Given an integer, write a function to determine if it is a power of two. Credits: Special thanks ...
- java finalize方法总结、GC执行finalize的过程
注:本文的目的并不是鼓励使用finalize方法,而是大致理清其作用.问题以及GC执行finalize的过程. 1. finalize的作用 finalize()是Object的protected方法 ...
- access窗体最大化到软件大小
Private Sub Form_Load()DoCmd.ShowToolbar "Ribbon", acToolbarNo '窗体最大化,占满软件最 End Sub
- Find、FindAll、Where的区别
Find.FindAll是一个List<T>的方法,返回一个new List<T>包括符合条件的数据 Where是一个linq方法,适用于任意继承了IEnumerable接口的 ...
- .net core使用orm操作mysql数据库
Mysql数据库由于其体积小.速度快.总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库.MySQL是一个多用户.多线程的关系型数据库管理系 ...
- 点聚weboffice隐藏自带工具栏
webObj = document.getElementById(webofficeID); webObj.ShowToolBar = false; //隐藏weboffice自带工具栏
- PHP快速获取MySQL数据库表结构
直接举例某个数据库中只有两个数据表,一个 test ,一个 xfp_keywords ,获取他们的数据库表结构. 此功能可以用于开发人员快速获取数据表结构通过获取的数据生成各种文件形式,用来快速理解数 ...
- 深入理解css3中的线性渐变
css3中的线性渐变 线性渐变公式: background-image: linear-gradient( [ <angle> | <side-or-corner> ]?, & ...
- zabbix 3.4 ubuntu 16 用腾讯企业邮箱作为告警邮箱
最近一直在研究zabbix监控系统,今天调试了腾讯企业邮箱作为告警邮箱的设置,本次方式是用内置email组件. 第一步: 选择Administration-->Media Types--> ...
- LESS的简单介绍
对于一些布局和样式比较复杂的网页,如何构建一个健康.优雅的CSS文件是一个很令人苦恼的问题.在书写静态页面的时候,我总是遇到布局结构累赘和重复样式复用性不高的问题,当然,对于这些问题归根究底还是要多联 ...