Skip to content

20. 案例研究 – 第2版

现在,我们呈现应用程序的最终版本:

通过克隆 [pam-01] 项目,创建了新的 NetBeans 项目 [pam-02]:

  • 在 [1] 中,将新项目
  • 在 [2] 中,将新项目命名为 [pam-02] 并指定其目录
  • 在 [3] 中,新项目 [pam-02]

为了将真正的 [业务] 层与我们构建的 Web 层“连接”起来,我们需要做三件事:

  1. 删除我们创建的模拟 [业务] 层
  2. 配置 Spring 以实例化位于 [pam-spring-metier-dao-jpa-hibernate.jar] 归档文件中的真实 [业务] 层
  3. 将所有必要的包(Spring、Hibernate、JPA、MySQL JDBC 驱动程序)添加到项目中。

Spring 配置文件 [WEB-INF/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"
       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.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
 
  <!-- application layers -->
 
<!-- web -->
  <bean id="config" class="web.Config" init-method="init">
    <property name="metier" ref="metier"/>
  </bean>
  <!-- business -->
  <bean id="metier" class="metier.Metier">
    <property name="employeDao" ref="employeDao"/>
    <property name="cotisationDao" ref="cotisationDao"/>  
  </bean>
  <!--  dao -->
  <bean id="employeDao" class="dao.EmployeDao" />
  <bean id="indemniteDao" class="dao.IndemniteDao" />
  <bean id="cotisationDao" class="dao.CotisationDao" />
 
  <!-- configuration JPA -->
  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
      </bean>
    </property>
    <property name="loadTimeWeaver">
      <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
  </bean>
 
  <!-- data source DBCP -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/dbpam_hibernate" />
    <property name="username" value="root" />
    <property name="password" value="" />
  </bean>
 
  <!-- transaction manager -->
  <tx:annotation-driven transaction-manager="txManager" />
  <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
  </bean>
 
  <!-- translation of exceptions -->
  <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
 
  <!-- persistence -->
  <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
 
</beans>

第 36–41 行配置了数据库访问。

从 [lib] 文件夹 [2] 中添加了新的库(Spring、Hibernate、JPA、MySQL JDBC 驱动程序)。整个文件夹都被包含进来。需要添加的 .jar 文件有几十个 [1]:

构建此库相当困难,因为这些框架有时会使用相同的归档文件。因此必须删除重复项。本文档的示例归档中已将这些文件收集在 [lib] 文件夹 [2] 中,以便读者无需自行重建此库。

就这样。新的应用程序 [pam-02] 现在可以与数据库管理系统(DBMS)配合使用了。以下是薪资计算的屏幕截图:

Image

这次计算出的工资是实际工资,而非第 1 版中的虚拟工资。欢迎读者亲自测试这个新应用程序。