玩转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 ...
随机推荐
- 学习Qt,Getting started
在界面的设计中,现在用的比较多的是Qt和WPF(C#),以前的MFC已出现衰老趋势.本人最近在学习Qt,觉得很有用,遂决定将学习历程记录下来,或许有感于后之来者,不亦乐哉. 一.Hello Qt #i ...
- Unity与web交互
Unity在发布web时,重要的是Unity与Web的交互,参数的传递 1.unity调用网页js的函数:Application.ExternalCall js函数: <script langu ...
- 【14】-java的单例设计模式详解
预加载模式 代码: public class Singleton { private volatile static Singleton singleton = new Singleton(); pr ...
- shell中关于sort的-o选项
sort -o选项意思为将排序后的结果写入文件,但你可能会说我可以重定向啊: sort >file 但如果你要排序文件names里的行再写回排序后的结果: sort names > nam ...
- The 6th tip of DB Query Analyzer
The 6th tip of DB Query Analyzer MA Gen feng (Guangdong Unitoll Services incorporated, Guangzhou ...
- 基于Redis的分布式锁两种实现方式
最近有一个竞拍的项目会用到分布式锁,网上查到的结果是有三种途径可以实现.1.数据库锁机制,2.redis的锁,3.zookeeper.考虑到使用mysql实现会在性能这一块会受影响,zookeeper ...
- JBOSS的启动和停止
本实例使用的JBOSS版本是jboss-4.2.3.GA 假设条件 1. 已设置好JAVA_HOME环境变量 2. 已下载JBoss并且安装目录为:D:\Java\jboss-4.2.3.GA 启 ...
- Day15 jss整体结构梳理
JS DOM--- 两个步骤: 1 查找标签 (1)直接查找 document.getElementById("idname") // dom对象 document.getElem ...
- Java内部抽象类的匿名类初始化
说在前面的话,以前写过一次这个变态代码,后来重构,把那个new的语法简化了,最近又要整,差点都想不起来了,留个文档把 1.下面这个案例更变态,抽象类还有一个个泛型类:首先内部抽象类的定义: /* * ...
- vfd折腾(二)
这篇是前期程序部分,主要讲驱动pt6311的程序 电路见上一篇博文 #ifndef PT6311_H #define PT6311_H #include "sys.h" #incl ...