zoukankan      html  css  js  c++  java
  • 玩转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的代码替换原来的。





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

  • 相关阅读:
    UVa 107 The Cat in the Hat
    UVa 591 Box of Bricks
    UVa 253 Cube painting
    UVa 10161 Ant on a Chessboard
    UVa 401 Palindromes
    UVa 465 Overflow
    我不知道
    消防局的设立
    某CF的D
    保安站岗
  • 原文地址:https://www.cnblogs.com/dingxiaoyue/p/4931772.html
Copyright © 2011-2022 走看看