Java在各种框架下编译Scala 【三】

设置Spring bean访问和调用的数据库

要定义这些Spring bean,创建main/resources的beans.xml以及一下的内容:

 
  1. <?xml version="1.0" encoding="UTF-8" ?>         
  2. <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"          
  3.           
  4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         
  5.        xmlns:context="http://www.springframework.org/schema/context"          
  6.           
  7.         xmlns:tx="http://www.springframework.org/schema/tx"         
  8.        xsi:schemaLocation="http://www.springframework.org/schema/beans          
  9.           
  10.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd          
  11.          http://www.springframework.org/schema/context         
  12.           
  13.         http://www.springframework.org/schema/context/spring-context-3.0.xsd          
  14.          http://www.springframework.org/schema/tx         
  15.           
  16.         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">          
  17.      <context:component-scan base-package="com.foo.myapp"/>         
  18.           
  19.     <bean id="emf"class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">          
  20.         <property name="persistenceXmlLocation"value="classpath:/META-INF/my-persistence.xml"/>          
  21.      </bean>         
  22.     <bean id="transactionManager"class="org.springframework.orm.jpa.JpaTransactionManager">          
  23.           
  24.         <property name="entityManagerFactory" ref="emf"/>          
  25.      </bean>         
  26.           
  27.     <tx:annotation-driven />          
  28. </beans> 

读取数据库配置中persistence.xml文件。在这里,您将获得一个主目录中名为MyApp的H2数据库。因此,创建main/resources/META-INF文件的内容如下。

 
  1. <persistencexmlnspersistencexmlns="http://java.sun.com/xml/ns/persistence"          
  2.             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           
  3.           
  4.             xsi:schemaLocation="http://java.sun.com/xml/ns/persistencehttp://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"           
  5.              version="2.0">          
  6.           
  7.    <persistence-unit name="myapp" transaction-type="RESOURCE_LOCAL">           
  8.         <provider>org.hibernate.ejb.HibernatePersistence</provider>          
  9.           
  10.         <properties>          
  11.            <propertynamepropertyname="hibernate.connection.driver_class"value="org.h2.Driver"/>           
  12.           
  13.            <property name="hibernate.connection.url"value="jdbc:h2:tcp://localhost/~/myapp"/>           
  14.             <propertynamepropertyname="hibernate.connection.username" value="sa"/>          
  15.           
  16.             <propertynamepropertyname="hibernate.connection.password" value=""/>          
  17.            <property name="hibernate.dialect"value="org.hibernate.dialect.H2Dialect"/>           
  18.           
  19.            <property name="hibernate.hbm2ddl.auto"value="create"/>           
  20.         </properties>          
  21.           
  22.    </persistence-unit>           
  23. /persistence>       

你可能感兴趣的:(java,spring,框架,scala,Hibernate)