第一步:拷贝JAR包(包括Spring,Hibernate,数据库驱动JAR包);
第二步:于Src文件夹下新建一文件夹命名为:META-INF,其中放一文件:persistence.xml,此文件用于配置数据库链接等信息。
- <?xml version="1.0"?>
- <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
- <!-- file:///D:/hibernate/hibernate-entitymanager-3.3.2.CR1/resources/org/hibernate/ejb/persistence_1_0.xsd -->
- <persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL">
- <provider>org.hibernate.ejb.HibernatePersistence</provider>
- <properties>
- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
- <property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
- <property name="hibernate.connection.username" value="root"/>
- <property name="hibernate.connection.password" value="mysql"/>
- <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/>
- <property name="hibernate.max_fetch_depth" value="3"/>
- <property name="hibernate.hbm2ddl.auto" value="update"/>
- <property name="hibernate.jdbc.fetch_size" value="18"/>
- <property name="hibernate.jdbc.batch_size" value="10"/>
- <property name="hibernate.show_sql" value="true"/>
- <property name="hibernate.format_sql" value="false"/>
- </properties>
- </persistence-unit>
- </persistence>
第三步:将beans.xml(或applicationContext.xml)放于Src下,并进行配置。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <context:annotation-config/>
- <!--实体bean所在的基础包的包名为:cn.yang-->
- <context:component-scan base-package="cn.yang"/>
- <!-- 使用数据源和指定persistence.xml位置的方式创建entityManagerFactory,如果使用的不是hibernate JPA实现,
- 需要在tomcat作一些特殊配置.具体参考手册
- 注意:使用该方式需要把persistence.xml中的hibernate.connection.driver_class,hibernate.connection.username,hibernate.connection.password,hibernate.connection.url配置删除
- -->
- <!--指定jdbc.properties的位置
- <context:property-placeholder location="classpath:jdbc.properties"/>
- -->
- <!-- 此配置与上面的配置同效,两都选一即可。
- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:jdbc.properties</value>
- </list>
- </property>
- </bean>
- -->
- <!--配置数据源,使用到了jdbc.properties。
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="${jdbc.driverClassName}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </bean>
- -->
- <!--JPA中管理实体Bean。
- <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
- <property name="loadTimeWeaver">
- <bean
- class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
- </property>
- </bean>
- -->
- <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
- <property name="persistenceUnitName" value="itcast"/>
- </bean>
- <!--事务管理Bean-->
- <bean id="myTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
- <property name="entityManagerFactory" ref="entityManagerFactory"/>
- </bean>
- <!--说明事务的配置使用的注解方式-->
- <tx:annotation-driven transaction-manager="myTxManager"/>
- </beans>
第四步:进行领域建模,写出实体Bean。
第五步:新建一个单元测试类,其中的一个方法写成如下代码:
- @Test
- public void test(){
- //启动Spring容器
- ApplicationContext beans=new ClassPathXmlApplicationContext("beans.xml");
- }
以上代码的作用就是找到类路径下的beans.xml文件,启动Spring容器,在起动Spring容器过程中,需要创建名为entityManagerFactory的Bean,就是在创建此bean的过程中将实体bean生成对应数据库中的表。
若生成表成功,则表示Spring与JPA集成成功。
注:
(1)若想在单元测试中直接操作JPA,则需写如下代码:
- //直接操作JPA
- EntityManagerFactory factory=Persistence.createEntityManagerFactory("itcast");
- //itcast为persistence.xml中定义的实例化单元名。
- EntityManager em=factory.createEntityManager();
- em.getTransaction().begin();
- em.persist(new ProductType());
- em.getTransaction().commit();
- em.close();
- factory.close();
(2) 在业务bean中使用EntityManager的方法:
- @PersistenceContext
- protected EntityManager em;
至此,Spring与JPA集成完毕。