熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Spring+JPA+MySQL的配置文件

2013-11-23 20:23:00  來源: Java開源技術 

  一直在整合spring+hibernate今天整合一下JPA +Spring

  persistencexml 默認放在 METAINF 目錄下

  Xml代碼
    <persistence xmlns= xmlns:xsi=instance
    xsi:schemaLocation= persistence__xsd version=>
    <persistenceunit name=java transactiontype=RESOURCE_LOCAL>
    <provider>orghibernateejbHibernatePersistence</provider>
    <class>MyClass</class>
    <properties>
    <property name=hibernatecacheprovider_class value=orghibernatecacheEhCacheProvider />
    <property name=show_sql value=true />
    <property name=hibernatecacheuse_second_level_cache value=true />
    <property name=hibernatecacheuse_query_cache value=false />
    </properties>
    </persistenceunit>
    </persistence>
    view plaincopy to clipboardprint?
    <persistence xmlns= xmlns:xsi=instance
    xsi:schemaLocation= persistence__xsd version=>
    <persistenceunit name=java transactiontype=RESOURCE_LOCAL>
    <provider>orghibernateejbHibernatePersistence</provider>
    <class>MyClass</class>
    <properties>
    <property name=hibernatecacheprovider_class value=orghibernatecacheEhCacheProvider />
    <property name=show_sql value=true />
    <property name=hibernatecacheuse_second_level_cache value=true />
    <property name=hibernatecacheuse_query_cache value=false />
    </properties>
    </persistenceunit>
    </persistence>

  <persistence xmlns= xmlns:xsi=instance
    xsi:schemaLocation= persistence__xsd version=>
    <persistenceunit name=java transactiontype=RESOURCE_LOCAL>
    <provider>orghibernateejbHibernatePersistence</provider>
    <class>MyClass</class>
    <properties>
    <property name=hibernatecacheprovider_class value=orghibernatecacheEhCacheProvider />
    <property name=show_sql value=true />
    <property name=hibernatecacheuse_second_level_cache value=true />
    <property name=hibernatecacheuse_query_cache value=false />
    </properties>
    </persistenceunit>
    </persistence>
     applicationContextxml

  Xml代碼
    <?xml version= encoding=UTF?>
    <beans xmlns= xmlns:xsi=instance
    xmlns:aop= xmlns:tx=
    xsi:schemaLocation= beansxsd
                aopxsd
                txxsd>

  <bean class=orgspringframeworkormjpasupportPersistenceAnnotationBeanPostProcessor />

  <bean id=Myervice class=MyService />
    <bean id=dataSource class=monsdbcpBasicDataSource>
    <property name=driverClassName>
    <value>commysqljdbcDriver</value>
    </property>
    <property name=url>
    <value>jdbc:mysql://localhost:</value>
    </property>
    <property name=username>
    <value>########</value>
    </property>
    <property name=password>
    <value>########</value>
    </property>
    </bean>

  <! JPA EntityManagerFactoryBean for EntityManager>
    <bean id=entityManagerFactory class=orgspringframeworkormjpaLocalContainerEntityManagerFactoryBean>
    <property name=persistenceXmlLocation value=persistencexml />
    <property name=persistenceUnitName value=java />
    <property name=dataSource ref=dataSource />
    </bean>

  <! Transaction manager for JPA >
    <bean id=transactionManager class=orgspringframeworkormjpaJpaTransactionManager>
    <property name=entityManagerFactory>
    <ref bean=entityManagerFactory />
    </property>
    </bean>

  <tx:annotationdriven transactionmanager=transactionManager />

  </beans>


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28409.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.