Skip to content

14. Firebird 2.5.4

现在,我们将针对 Firebird 2.5.4 处理与 Oracle 相同的工作。

14.1. 设置工作环境

14.1.1. Eclipse 环境

我们将使用以下 Eclipse 环境:

  

上述 Firebird 项目位于 [<examples>/spring-database-config\firebird\eclipse] 文件夹中。

注意:按 [Alt-F5] 可重新生成所有 Maven 项目。

14.1.2. 生成数据库

与处理 Oracle、DB2 和 SQL Server 时一样,我们需要将 Firebird JDBC 驱动程序安装到本地 Maven 仓库中。

  

[install.bat] 文件包含以下代码:

"%M2_HOME%\bin\mvn.bat" install:install-file -Dfile=jaybird-2.2.7.jar -Dpackaging=jar -DgroupId=org.firebirdsql.jdbc -DartifactId=jaybird -Dversion=2.2.7

其中 [%M2-HOME%] 是 Maven 安装目录(参见第 23.2 节)。安装完成后,可在 [pom.xml] 文件中通过以下依赖项引用 Firebird JDBC 驱动程序:


        <dependency>
            <groupId>org.firebirdsql.jdbc</groupId>
            <artifactId>jaybird</artifactId>
            <version>2.2.7</version>
</dependency>

从这里开始,连接 Firebird 数据库将使用凭据 [sysdba / masterkey]。启动 Firebird 及其客户端 [IBManager](参见第 23.10 节)。Firebird 数据库的独特之处在于它们被封装在一个单一文件中。

  • 在[1]中,该文件位于[<examples>\spring-database-config\firebird\databases\DBPRODUITS.GDB];
  • 与 Oracle 一样,主键是通过序列生成的。

我们按照同样的方法加载 [dbproduitscategories] 数据库,该数据库位于 [<examples>\spring-database-config\firebird\databases\DBPRODUITSCATEGORIES.GDB]

  

现在,运行这些配置:

  • [spring-jdbc-generic-01.IntroJdbc01];
  • [spring-jdbc-generic-01.IntroJdbc02];
  • [spring-jdbc-generic-03.JUnitTestDao1];
  • [spring-jdbc-generic-03.JUnitTestDao2];
  • [spring-jdbc-generic-04.JUnitTestDao];
  • [spring-jpa-generic-JUnitTestDao-openjpa];

它们都必须通过。

14.2. 配置 JDBC 层

 

[firebird-config-jdbc] 项目配置了以下测试架构的 [JDBC] 层:

该项目类似于针对 MySQL 数据库管理系统 (DBMS) JDBC 层的 [oracle-config-jdbc] 配置项目(参见第 10.2 节)。此处仅列出变更内容:

[pom.xml] 文件引入了 Firebird JDBC 驱动程序:


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>dvp.spring.database</groupId>
    <artifactId>generic-config-jdbc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>configuration generic jdbc</name>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.3.RELEASE</version>
    </parent>
 
    <dependencies>
        <!-- dépendances variables ********************************************** -->
        <!-- driver JDBC from SGBD -->
        <dependency>
            <groupId>org.firebirdsql.jdbc</groupId>
            <artifactId>jaybird</artifactId>
            <version>2.2.7</version>
        </dependency>
        <!-- required for Firebird driver -->
        <dependency>
            <groupId>javax.resource</groupId>
            <artifactId>connector-api</artifactId>
            <version>1.5</version>
        </dependency>
        <dependency>
            <groupId>org.antlr</groupId>
            <artifactId>antlr-runtime</artifactId>
            <version>3.5.2</version>
        </dependency>
        <!-- dépendances constantes ********************************************** -->
        ...
    </dependencies>
...
</project>
  • 第 18–22 行:Firebird JDBC 驱动程序;
  • 第 24–33 行:Firebird JDBC 驱动程序所需的依赖项;

第二个改动位于 [ConfigJdbc] 类中,该类定义了数据库凭据:


    // paramètres de connexion
    public final static String DRIVER_CLASSNAME = "org.firebirdsql.jdbc.FBDriver";
    public final static String URL_DBPRODUITS = "jdbc:firebirdsql:localhost/3050:<exemples>/SPRING-DATABASE-CONFIG/FIREBIRD/DATABASES/DBPRODUITS.GDB";
    public final static String USER_DBPRODUITS = "sysdba";
    public final static String PASSWD_DBPRODUITS = "masterkey";
    public final static String URL_DBPRODUITSCATEGORIES = "jdbc:firebirdsql:localhost/3050:<exemples>/SPRING-DATABASE-CONFIG/FIREBIRD/DATABASES/DBPRODUITSCATEGORIES.GDB";
    public final static String USER_DBPRODUITSCATEGORIES = "sysdba";
public final static String PASSWD_DBPRODUITSCATEGORIES = "masterkey";
  • 第 3 行和第 6 行:必须为这两个数据库输入确切的路径;

可以进行的第三项修改是调整 [PreparedStatement] 支持的参数最大数量:


    // max number of parameters of a [PreparedStatement]
    public final static int MAX_PREPAREDSTATEMENT_PARAMETERS = 1000;

[JUnitTestPushTheLimits] 测试会为 5,000 种产品生成 SQL 语句,这将生成包含 5,000 个参数的 [PreparedStatement] 对象。MySQL 支持此参数数量,而 Firebird 则不支持。

第四项更改涉及 Firebird-config-jdbc 中的保留关键字:PASSWORD。因此,[USERS.PASSWORD] 列已重命名为 [USERS.PASSWD]:


public final static String TAB_USERS_PASSWORD = "PASSWD";
public static final String SELECT_USER_BYLOGIN = "SELECT u.ID as u_ID, u.VERSIONING as u_VERSIONING, u.NAME as u_NAME,u.LOGIN as u_LOGIN,u.PASSWD as u_PASSWORD FROM USERS u WHERE u.LOGIN= :login";

14.3. 配置 OpenJPA JPA 层

 

[firebird-config-jpa-openjpa] 项目用于配置测试架构的 [JPA] 层:

该项目与针对 Oracle DBMS 的 OpenJPA JPA 层的 [firebird-config-jpa-openjpa] 配置项目类似(参见第 10.5 节)。事实上,这两种 DBMS 都使用序列来生成主键。只需进行一项修改,即在 [ConfigJpa] 类中对 [jpaVendorAdapter] Bean 的定义进行调整:


    // the provider JPA
    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        OpenJpaVendorAdapter openJpaVendorAdapter = new OpenJpaVendorAdapter();
        openJpaVendorAdapter.setShowSql(false);
        openJpaVendorAdapter.setDatabase(Database.DEFAULT);
        openJpaVendorAdapter.setGenerateDdl(true);
        return openJpaVendorAdapter;
}
  • 第 6 行:我们告知 JPA 实现,它将处理一个未识别的数据库管理系统(DBMS)。随后,JPA 实现将采用标准 SQL 数据类型。

完成这些修改后,[spring-jpa-generic-JUnitTestDao-openjpa] 配置的执行应能成功。

14.4. 配置 Hibernate JPA 层

 

注意:按 [Alt-F5] 可重新生成所有 Maven 项目。

[firebird-config-jpa-hibernate] 项目与 [oracle-config-jpa-hibernate] 项目(第 10.4 节)类似,采用了与将 [oracle-config-jpa-openjpa] 项目移植到 [firebird-config-jpa-openjpa] 项目(第 10.4 节)时相同的修改。

完成这些修改后,运行 [spring-jpa-generic-JUnitTestDao-hibernate-eclipselink] 配置应能成功。

 

注意:按 [Alt-F5] 可重新生成所有 Maven 项目。

[firebird-config-jpa-eclipselink] 项目与 [oracle-config-jpa-eclipselink](第 10.3 节)类似,采用了与将 [oracle-config-jpa-openjpa] 项目移植到 [firebird-config-jpa-openjpa] 项目(第 10.4 节)时相同的修改。

完成这些修改后,[spring-jpa-generic-JUnitTestDao-hibernate-eclipselink] 配置的执行应能成功。