spring-data jpa配置

1、META-INF下persistence.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <persistence xmlns="http://java.sun.com/xml/ns/persistence" 
  3.     version="2.0"> 
  4.     <!--   
  5.     <persistence-unit name="common" transaction-type="RESOURCE_LOCAL"> 
  6.         <provider>org.hibernate.ejb.HibernatePersistence</provider> 
  7.         <properties> 
  8.             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> 
  9.             <property name="hibernate.show_sql" value="true" /> 
  10.             <property name="hibernate.format_sql" value="true" /> 
  11.             <property name="hibernate.use_sql_comments" value="false" /> 
  12.             <property name="hibernate.hbm2ddl.auto" value="update" /> 
  13.         </properties> 
  14.     </persistence-unit> 
  15.     --> 
  16.     <persistence-unit name="common" /> 
  17. </persistence>  

2、spring配置文件

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 
  4.     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 
  5.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
  7.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
  9.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
  10.         http://www.springframework.org/schema/data/jpa 
  11.         http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> 
  12.  
  13.     <context:component-scan base-package="com.langaili.dao" /> 
  14.     <context:component-scan base-package="com.langaili.service" /> 
  15.  
  16.     <!-- 定義上下文返回的消息的國際化。 
  17.     <bean id="messageSource" 
  18.         class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
  19.         <property name="basename" 
  20.             value="classpath:org/springframework/security/messages_zh_CN" /> 
  21.     </bean> 
  22.  --> 
  23.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
  24.         destroy-method="close"> 
  25.         <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
  26.         <property name="url" 
  27.             value="jdbc:mysql://localhost/fanka?useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true" /> 
  28.         <property name="username" value="root" /> 
  29.         <property name="password" value="password" /> 
  30.     </bean> 
  31.     <!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  32.         <property name="dataSource" ref="dataSource" /> <property name="packagesToScan">  
  33.         <value>com.langaili.domain</value> </property> <property name="hibernateProperties">  
  34.         <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQL5InnoDBDialect  
  35.         </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop>  
  36.         <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> --> 
  37.     <!--   
  38.     <bean id="entityManagerFactory" 
  39.         class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
  40.         <property name="persistenceUnitName" value="common"></property> 
  41.         <property name="packagesToScan" value="com.langaili.domain" /> 
  42.         <property name="dataSource" ref="dataSource" /> 
  43.     </bean> 
  44.     --> 
  45.     <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
  46.         <property name="dataSource" ref="dataSource" /> 
  47.         <property name="persistenceUnitName" value="common" /> 
  48.         <property name="jpaVendorAdapter"> 
  49.             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
  50.                 <property name="showSql" value="true" /> 
  51.                 <property name="generateDdl" value="true" /> 
  52.                 <property name="database" value="MYSQL" /> 
  53.             </bean> 
  54.         </property> 
  55.         <property name="jpaProperties">   
  56.             <props>   
  57.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>   
  58.                 <prop key="hibernate.format_sql">true</prop> 
  59.                 <prop key="hibernate.hbm2ddl.auto">update</prop> 
  60.             </props>   
  61.         </property>    
  62.     </bean> 
  63.     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
  64.         <property name="entityManagerFactory" ref="entityManagerFactory" /> 
  65.     </bean> 
  66.  
  67.     <jpa:repositories base-package="com.langaili.dao" 
  68.         entity-manager-factory-ref="entityManagerFactory" 
  69.         transaction-manager-ref="transactionManager"> 
  70.         <!-- <jpa:repository id="userRepository" custom-impl-ref="userRepositoryCustom"/> --> 
  71.     </jpa:repositories> 
  72.  
  73.     <tx:annotation-driven transaction-manager="transactionManager"/> 
  74.  
  75. </beans> 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章