注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(二):进一步消除 hibernate.cfg.xml

通过上面一步骤。我们简化了hibernate中扰人的 xxxxx.hbm.xml文件。那么是否可以再进行简化呢?
那么,我们在这一步骤的目的,就是把整个 hibernate.cfg.xml都给简化了。
 
依然是利用注解注入的方式。通过jpa 我们可以把 hibernate.cfg.xml中那些 mapping classes再次简化与无形。
 
  1. 在applicationContext.xml中进行如下配置
    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"
    4.     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"
    5.     xmlns:tx="http://www.springframework.org/schema/tx">
    6.     <bean id="entityManagerFactory"
    7.         class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    8.         <property name="persistenceUnitName" value="testerPU" />
    9.     </bean>
    10.     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    11.         <property name="entityManagerFactory" ref="entityManagerFactory" />
    12.     </bean>
    13.     <tx:annotation-driven transaction-manager="transactionManager" />
    14.     <bean id="transactionInterceptor"
    15.         class="org.springframework.transaction.interceptor.TransactionInterceptor">
    16.         <!-- 事务拦截器bean需要依赖注入一个事务管理器 -->
    17.         <property name="transactionManager">
    18.             <ref local="transactionManager" />
    19.         </property>
    20.         <property name="transactionAttributes">
    21.             <!-- 下面定义事务(指service里面的方法)传播属性 -->
    22.             <props>
    23.                 <prop key="insert*">PROPAGATION_REQUIRED</prop>
    24.                 <prop key="update*">PROPAGATION_REQUIRED</prop>
    25.                 <prop key="save*">PROPAGATION_REQUIRED</prop>
    26.                 <prop key="add*">PROPAGATION_REQUIRED</prop>
    27.                 <prop key="update*">PROPAGATION_REQUIRED</prop>
    28.                 <prop key="remove*">PROPAGATION_REQUIRED</prop>
    29.                 <prop key="delete*">PROPAGATION_REQUIRED</prop>
    30.                 <prop key="get*">PROPAGATION_REQUIRED,readOnly
    31.                 </prop>
    32.                 <prop key="find*">PROPAGATION_REQUIRED,readOnly
    33.                 </prop>
    34.                 <prop key="load*">PROPAGATION_REQUIRED,readOnly
    35.                 </prop>
    36.                 <prop key="change*">PROPAGATION_REQUIRED</prop>
    37.                 <prop key="count*">PROPAGATION_REQUIRED</prop>
    38.                 <prop key="*">PROPAGATION_REQUIRED</prop>
    39.             </props>
    40.         </property>
    41.     </bean>
    42.     <!-- 定义自动代理BeanNameAutoProxyCreator -->
    43.     <bean id="beanNameAutoProxyCreator"
    44.         class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    45.         <!-- 指定对满足哪些bean name的bean自动生成业务代理 -->
    46.         <property name="beanNames">
    47.             <list>
    48.                 <value>*Service</value>
    49.             </list>
    50.         </property>
    51.         <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器  -->
    52.         <property name="interceptorNames">
    53.             <list>
    54.                 <!-- 此处可增加其他新的Interceptor -->
    55.                 <value>transactionInterceptor</value>
    56.             </list>
    57.         </property>
    58.     </bean>
    59.     <bean id="AlcorTCountriesDAO" class="com.firemax.test.hibernate.AlcorTCountriesDAO">
    60.         <property name="entityManagerFactory" ref="entityManagerFactory" />
    61.     </bean>
    62.     <bean id="AlcorTProvincesDAO" class="com.firemax.test.hibernate.AlcorTProvincesDAO">
    63.         <property name="entityManagerFactory" ref="entityManagerFactory" />
    64.     </bean>
    65.     <bean id="AlcotTDistrictDAO" class="com.firemax.test.hibernate.AlcotTDistrictDAO">
    66.         <property name="entityManagerFactory" ref="entityManagerFactory" />
    67.     </bean>
    68.     <bean id="AlcorTCitysDAO" class="com.firemax.test.hibernate.AlcorTCitysDAO">
    69.         <property name="entityManagerFactory" ref="entityManagerFactory" />
    70.     </bean>
    71.     <bean id="CountryService" class="com.firemax.test.service.CountryService">
    72.         <property name="alcorTCountriesDAO" ref="AlcorTCountriesDAO" />
    73.         <property name="alcorTProvincesDAO" ref="AlcorTProvincesDAO" />
    74.         <property name="alcorTCitysDAO" ref="AlcorTCitysDAO" />
    75.         <property name="alcotTDistrictDAO" ref="AlcotTDistrictDAO" />
    76.     </bean>
    77. </beans>


    注意红色部分。这部分和(一)里面提到的那个applicationContext.xml中使用的bean是不同的。这里面已经用到了spring 的jpa部分。
    其中 <property name="persistenceUnitName" value="testerPU" />  是指向了一个 jpa的PU(Persistent Units)

    那么这个 是在哪里定义的呢?
    这里的定义是在你的 classes(src)目录下META-INF下的persistence.xml文件中来定义
    persistence.xml文件内容如下:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    5.     http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    6.     version="1.0">
    7.     <persistence-unit name="testerPU"
    8.         transaction-type="RESOURCE_LOCAL">
    9.         <provider>org.hibernate.ejb.HibernatePersistence</provider>
    10.         <class>com.firemax.test.hibernate.AlcorTCountries</class>
    11.         <class>com.firemax.test.hibernate.AlcorTProvinces</class>
    12.         <class>com.firemax.test.hibernate.AlcotTDistrict</class>
    13.         <class>com.firemax.test.hibernate.AlcorTCitys</class>
    14.         <properties>
    15.             <property name="hibernate.connection.driver_class"
    16.                 value="com.mysql.jdbc.Driver" />
    17.             <property name="hibernate.connection.url"
    18.                 value="jdbc:mysql://localhost:3306/alcorweb?useUnicode=true&characterEncoding=utf-8" />
    19.             <property name="hibernate.connection.username"
    20.                 value="alcorweb" />
    21.             <property name="hibernate.connection.password"
    22.                 value="alcorweb" />
    23.             <property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
    24.             <property name="hibernate.c3p0.max_size" value="100"/>
    25.             <property name="hibernate.c3p0.min_size"  value="20"/>
    26.             <property name="hibernate.c3p0.timeout"   value="120"/>
    27.             <property name="hibernate.c3p0.max_statements" value="0"/>
    28.             <property name="hibernate.c3p0.idle_test_period"  value="120"/>
    29.             <property name="hibernate.c3p0.acquire_increment" value="5  "/>
    30.             <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
    31.             <property name="hibernate.cache.use_query_cache" value="false"/>
    32.             <property name="hibernate.show_sql" value="false"/>
    33.             <property name="hibernate.useUnicode" value="true"/>
    34.             <property name="hibernate.characterEncoding" value="utf8"/>
    35.         </properties>
    36.     </persistence-unit>
    37. </persistence>

    仔细观察他的代码,我们发现其实和hibernate.cfg.xml和相似。都有指向pojo的定义 和 database连接的定义。先别着急。我们这次的优化,就先完成这个目标。主要是引入的spring的jpa框架。
    后面将介绍更进一步的优化。来展示jpa 和 注释注入的能力。

你可能感兴趣的:(Hibernate)