通过上面一步骤。我们简化了hibernate中扰人的 xxxxx.hbm.xml文件。那么是否可以再进行简化呢?
那么,我们在这一步骤的目的,就是把整个 hibernate.cfg.xml都给简化了。
依然是利用注解注入的方式。通过jpa 我们可以把 hibernate.cfg.xml中那些 mapping classes再次简化与无形。
- 在applicationContext.xml中进行如下配置
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
- xmlns:tx="http://www.springframework.org/schema/tx">
- <bean id="entityManagerFactory"
- class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
- <property name="persistenceUnitName" value="testerPU" />
- </bean>
- <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
- <property name="entityManagerFactory" ref="entityManagerFactory" />
- </bean>
- <tx:annotation-driven transaction-manager="transactionManager" />
- <bean id="transactionInterceptor"
- class="org.springframework.transaction.interceptor.TransactionInterceptor">
- <!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
- <property name="transactionManager">
- <ref local="transactionManager" />
- </property>
- <property name="transactionAttributes">
- <!-- 下面定义事务(指service里面的方法)传播属性 -->
- <props>
- <prop key="insert*">PROPAGATION_REQUIRED</prop>
- <prop key="update*">PROPAGATION_REQUIRED</prop>
- <prop key="save*">PROPAGATION_REQUIRED</prop>
- <prop key="add*">PROPAGATION_REQUIRED</prop>
- <prop key="update*">PROPAGATION_REQUIRED</prop>
- <prop key="remove*">PROPAGATION_REQUIRED</prop>
- <prop key="delete*">PROPAGATION_REQUIRED</prop>
- <prop key="get*">PROPAGATION_REQUIRED,readOnly
- </prop>
- <prop key="find*">PROPAGATION_REQUIRED,readOnly
- </prop>
- <prop key="load*">PROPAGATION_REQUIRED,readOnly
- </prop>
- <prop key="change*">PROPAGATION_REQUIRED</prop>
- <prop key="count*">PROPAGATION_REQUIRED</prop>
- <prop key="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- <!-- 定义自动代理BeanNameAutoProxyCreator -->
- <bean id="beanNameAutoProxyCreator"
- class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <!-- 指定对满足哪些bean name的bean自动生成业务代理 -->
- <property name="beanNames">
- <list>
- <value>*Service</value>
- </list>
- </property>
- <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器 -->
- <property name="interceptorNames">
- <list>
- <!-- 此处可增加其他新的Interceptor -->
- <value>transactionInterceptor</value>
- </list>
- </property>
- </bean>
- <bean id="AlcorTCountriesDAO" class="com.firemax.test.hibernate.AlcorTCountriesDAO">
- <property name="entityManagerFactory" ref="entityManagerFactory" />
- </bean>
- <bean id="AlcorTProvincesDAO" class="com.firemax.test.hibernate.AlcorTProvincesDAO">
- <property name="entityManagerFactory" ref="entityManagerFactory" />
- </bean>
- <bean id="AlcotTDistrictDAO" class="com.firemax.test.hibernate.AlcotTDistrictDAO">
- <property name="entityManagerFactory" ref="entityManagerFactory" />
- </bean>
- <bean id="AlcorTCitysDAO" class="com.firemax.test.hibernate.AlcorTCitysDAO">
- <property name="entityManagerFactory" ref="entityManagerFactory" />
- </bean>
- <bean id="CountryService" class="com.firemax.test.service.CountryService">
- <property name="alcorTCountriesDAO" ref="AlcorTCountriesDAO" />
- <property name="alcorTProvincesDAO" ref="AlcorTProvincesDAO" />
- <property name="alcorTCitysDAO" ref="AlcorTCitysDAO" />
- <property name="alcotTDistrictDAO" ref="AlcotTDistrictDAO" />
- </bean>
- </beans>
注意红色部分。这部分和(一)里面提到的那个applicationContext.xml中使用的bean是不同的。这里面已经用到了spring 的jpa部分。其中 <property name="persistenceUnitName" value="testerPU" /> 是指向了一个 jpa的PU(Persistent Units)
那么这个 是在哪里定义的呢?这里的定义是在你的 classes(src)目录下META-INF下的persistence.xml文件中来定义persistence.xml文件内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <persistence-unit name="testerPU"
- transaction-type="RESOURCE_LOCAL">
- <provider>org.hibernate.ejb.HibernatePersistence</provider>
- <class>com.firemax.test.hibernate.AlcorTCountries</class>
- <class>com.firemax.test.hibernate.AlcorTProvinces</class>
- <class>com.firemax.test.hibernate.AlcotTDistrict</class>
- <class>com.firemax.test.hibernate.AlcorTCitys</class>
- <properties>
- <property name="hibernate.connection.driver_class"
- value="com.mysql.jdbc.Driver" />
- <property name="hibernate.connection.url"
- value="jdbc:mysql://localhost:3306/alcorweb?useUnicode=true&characterEncoding=utf-8" />
- <property name="hibernate.connection.username"
- value="alcorweb" />
- <property name="hibernate.connection.password"
- value="alcorweb" />
- <property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
- <property name="hibernate.c3p0.max_size" value="100"/>
- <property name="hibernate.c3p0.min_size" value="20"/>
- <property name="hibernate.c3p0.timeout" value="120"/>
- <property name="hibernate.c3p0.max_statements" value="0"/>
- <property name="hibernate.c3p0.idle_test_period" value="120"/>
- <property name="hibernate.c3p0.acquire_increment" value="5 "/>
- <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
- <property name="hibernate.cache.use_query_cache" value="false"/>
- <property name="hibernate.show_sql" value="false"/>
- <property name="hibernate.useUnicode" value="true"/>
- <property name="hibernate.characterEncoding" value="utf8"/>
- </properties>
- </persistence-unit>
- </persistence>
仔细观察他的代码,我们发现其实和hibernate.cfg.xml和相似。都有指向pojo的定义 和 database连接的定义。先别着急。我们这次的优化,就先完成这个目标。主要是引入的spring的jpa框架。后面将介绍更进一步的优化。来展示jpa 和 注释注入的能力。