跳到主要内容

5. 根实例

以下示例创建一个根实例来存储 Customers

package io.micronaut.microstream.docs;

import io.micronaut.core.annotation.NonNull;

import java.util.HashMap;
import java.util.Map;

public class Data {
private Map<String, Customer> customers = new HashMap<>();

@NonNull
public Map<String, Customer> getCustomers() {
return this.customers;
}
}

Customer 定义为:

package io.micronaut.microstream.docs;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.serde.annotation.Serdeable;

import javax.validation.constraints.NotBlank;

@Serdeable //
public class Customer {
@NonNull
@NotBlank
private final String id;

@NonNull
@NotBlank
private String firstName;

@Nullable
private String lastName;

public Customer(@NonNull String id, @NonNull String firstName, @Nullable String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}

@NonNull
public String getId() {
return id;
}

public void setFirstName(@NonNull String firstName) {
this.firstName = firstName;
}

public void setLastName(@Nullable String lastName) {
this.lastName = lastName;
}

@NonNull
public String getFirstName() {
return firstName;
}

@Nullable
public String getLastName() {
return lastName;
}
}
  1. 该类型使用 @Serdeable 进行注解,以启用序列化/反序列化

你可以通过配置指定根类:

src/main/resources/application.yml

microstream:
storage:
main:
root-class: 'io.micronaut.microstream.docs.Data'
storage-directory: '/Users/sdelamo/Documents/MicroStreamData'
  1. main:为 MicroStream 实例指定名称限定符
  2. root-class:指定实体图根的类

英文链接