玩转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"
	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-1.3.xsd
			http://www.springframework.org/schema/data/repository
            http://www.springframework.org/schema/data/repository/spring-repository-1.6.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.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.dao"
        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

时间: 2024-12-13 22:54:23

玩转spring mvc(四)---在spring MVC中整合JPA的相关文章

攻城狮在路上(贰) Spring(四)--- Spring BeanFactory简介

BeanFactory时Spring框架最核心的接口,它提供了高级IoC的配置机制,使管理不同类型的Java对象成为了可能.我们一般称BeanFactory为IoC容器.BeanFactory是Spring的基础设施,面向Spring本身. 一.BeanFactory的体系结构: 二.类图说明: ListableBeanFactory:定义了访问容器中Bean基本信息的方法,查看Bean的个数.获取某一类型Bean的配置名,看容器中是否包含某一Bean等方法. HierarchicalBeanF

Spring(四)Spring与数据库编程

Spring最重要的功能毫无疑问就是操作数据.数据库的百年城是互联网编程的基础,Spring为开发者提供了JDBC模板模式,那就是它自身的JdbcTemplate.Spring还提供了TransactionTemplate支持事务的模板.Spring并没有支持MyBatis,好在MyBatis社区开发了接入Spring的开发包,该包也提供了SqlSessionTemplate给开发者使用,该包还可以屏蔽SqlSessionTemplate这样的功能性代码,可以在编程中擦除SqlSessionTe

Spring点滴四:Spring Bean生命周期

Spring Bean 生命周期示意图: 了解Spring的生命周期非常重要,我们可以利用Spring机制来定制Bean的实例化过程. --------------------------------------------------------------------------------------------------------------------------------------------------- spring-service.xml: <?xml version=

深入浅出Spring(四) Spring实例分析

上次的博文中 深入浅出Spring(二) IoC详解 和 深入浅出Spring(三) AOP详解中,我们分别介绍了一下Spring框架的两个核心一个是IoC,一个是AOP.接下来我们来做一个Spring的实例. 为了更好的讲解Spring的相关内容,这次的博文会针对一个[添加用户]的实例,进行逐步的解剖和优化,再此过程中,细节内容大家不需要考虑,只需要加深对Spring的理解即可. 1.实例一 首先,我们来看一个没有使用任何Spring框架内容,比较单纯的添加用户的实例.先看一下相关的类图和实现

《Spring技术内幕》笔记-第四章 Spring MVC与web环境

?上下文在web容器中的启动 1,IoC容器的启动过程 IoC的启动过程就是建立上下文的过程,该上下文是与ServletContext相伴.在Spring中存在一个核心控制分发器,DispatcherServlet,这是Spring的核心.在web容器启动Spring应用程序时,首先建立根上下文,然后ContextLoader建立WebApplicationContext. Web容器中启动Spring过程如下: 在web.xml中,已经配置了ContextLoadListener,该类实现了S

从Spring到SpringBoot构建WEB MVC核心配置详解

目录 理解Spring WEB MVC架构的演变 认识Spring WEB MVC 传统时代的Spring WEB MVC 新时代Spring WEB MVC SpringBoot简化WEB MVC开发 自动装配 条件装配 外部化配置 本章源码下载 理解Spring WEB MVC架构的演变 基础Servlet架构 核心架构:前端控制器 Spring WEB MVC架构 认识Spring WEB MVC 传统时代的Spring WEB MVC 怎么讲呢?就是很传统的使用Spring Framew

8 -- 深入使用Spring -- 7...2 MVC框架与Spring整合的思考

8.7.2 MVC 框架与Spring整合的思考 对于一个基于B/S架构的JAVA EE 应用而言,用户请求总是向MVC框架的控制器请求,而当控制器拦截到用户请求后,必须调用业务逻辑组件来处理用户请求.此时有一个问题:控制器应该如何获得业务逻辑组件? 最容易想到的策略是,直接通过new 关键字创建业务逻辑组件,然后调用业务逻辑组件的方法,根据业务逻辑方法的返回值确定结果. 在实际的应用中,很少见到采用上面的访问策略,因为这是一种非常差的策略.不这样做至少有如下三个原因: ⊙ 控制器直接创建业务逻

Spring MVC 简述:从MVC框架普遍关注的问题说起

任何一个完备的MVC框架都需要解决Web开发过程中的一些共性的问题,比如请求的收集与分发.数据前后台流转与转换,当前最流行的SpringMVC和Struts2也不例外.本文首先概述MVC模式的分层思想与MVC框架普遍关注的问题,并以此为契机结合SpringMVC的入门级案例简要地从原理.架构角度介绍了它对这些问题的处理,包括请求处理流程.消息转换机制和数据绑定机制等核心问题.最后,本文对目前最为流行的两个MVC框架SpringMVC 和 一. MVC 模式与框架 1.MVC 模式 Java We

Spring MVC静态资源处理——&lt;mvc:resources /&gt; ||&lt;mvc:default-servlet-handler /&gt;

优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往使用 *.do . *.xhtml等方式.这就决定了请求URL必须是一个带后缀的URL,而无法采用真正的REST风格的URL. 如果将DispatcherServlet请求映射配置为"/",则Spring MVC将捕获Web容器所有的请求,包括静态资源的请求,Spring MVC会将它们当