文章 Michael Lei · 九月 15, 2022 5m read

示例:使用 Java + SpringBoot + Hibernate 和 IRIS 数据库创建 REST API

Spring Boot 是最常用来创建 REST API 和微服务的 Java 框架。 它可用于部署 Web 应用程序、可执行 Web 应用程序或桌面自包含应用程序,其中应用程序和其他依赖项打包在一起。 Spring Boot 允许执行许多功能,请参见:

注:要了解有关 SpringBoot 的信息,请参见官方网站 - https://spring.io/quickstart

要创建具有一个或多个微服务的 Web api 应用程序,可以使用 Spring IDE for Eclipse/VSCode,并使用向导配置上述将在应用程序中使用的技术,请参见:

您可以选择技术并创建项目。 所有技术都将通过 maven 导入。 它就像一个可视化的 zpm。

所创建的项目有一个类用于为应用程序提供支持,其中包含所有所需内容(Web 和应用程序服务器,以及所有依赖项、微服务概念)。

此项目的完整源代码在以下 open exchange 项目中: https://openexchange.intersystems.com/package/springboot-iris-crud

首先要配置 IRIS JDBC 驱动程序和 IRIS Hibernate 支持,为此,请将 jar 文件复制到 resources 文件夹,请参见:

打开 pom.xml 以配置这些 jar 文件的依赖项,请参见:

<dependency>
<groupId>com.intersystems</groupId>
<artifactId>intersystems-jdbc</artifactId>
<version>3.2.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/intersystems-jdbc-3.2.0.jar</systemPath>
</dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-iris</artifactId>
        <version>1.0.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/src/main/resources/hibernate-iris-1.0.0.jar</systemPath>
    </dependency>

 

现在可以在 application.properties 中配置与 IRIS 数据库的连接,请参见:

spring.datasource.username=_SYSTEM
spring.datasource.url=jdbc:IRIS://iris:1972/USER
spring.datasource.password=SYS
spring.jpa.properties.hibernate.default_schema=dc_Sample
#spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.intersystems.jdbc.IRISDriver
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.database-platform=org.hibernate.dialect.InterSystemsIRISDialect
spring.datasource.sql-script-encoding=utf-8
server.tomcat.relaxed-query-chars=|,{,},[,]
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=true

下一步是创建一个映射到 IRIS 表的持久化 Java 类,请参见:

package community.intersystems.springboot.app.model;

import java.io.Serializable; import java.util.Date;

import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType;

import com.fasterxml.jackson.annotation.JsonFormat;

@Entity @Table(name = "Product") public class Product implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;

private String name;

private String description;

private Double height;

private Double width;

private Double weight;

@Column(name="releasedate")
@Temporal(TemporalType.DATE)
@JsonFormat(pattern = "dd/MM/yyyy")
private Date releaseDate;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public Double getHeight() {
    return height;
}

public void setHeight(Double height) {
    this.height = height;
}

public Double getWidth() {
    return width;
}

public void setWidth(Double width) {
    this.width = width;
}

public Double getWeight() {
    return weight;
}

public void setWeight(Double weight) {
    this.weight = weight;
}

public Date getReleaseDate() {
    return releaseDate;
}

public void setReleaseDate(Date releaseDate) {
    this.releaseDate = releaseDate;
}

}

最后一步是创建一个仓库接口,以将您的持久化类作为 REST 服务公开(以 HATEOAS 模式),您无需执​​行 crud 操作,只需编写以下代码:

package community.intersystems.springboot.app.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import community.intersystems.springboot.app.model.Product;

public interface ProductRepository extends JpaRepository<Product, Long> {

}

现在,将您的应用程序作为 Spring Boot 应用程序运行:

等待内部服务器启动,然后打开 localhost:8080。 Spring boot 将打开一个 API REST HAL 浏览器。 请参见以下图像记录:

IRIS 与 Hibernate 配合运行

更多详细信息,请参见我的应用程序示例。 我将所有内容一起打包成一个 Docker 项目,其中包含 2 个服务:IRIS 和 SpringBoot。

HATEOAS 模式非常适合 api url、路径和导航,更多详细信息,请自行百度。

祝使用愉快!