--- title: 与自定义模型交互 description: 了解如果通过代码的方式操作数据 --- Halo 提供了两个类用于与自定义模型数据交互 `ExtensionClient` 和 `ReactiveExtensionClient`。 它们的本质就是操作数据库,区别在于 `ExtensionClient` 是阻塞式 API,而 `ReactiveExtensionClient` 是响应式 API,接口返回值只有两种 Mono 或 Flux,它们由 [reactor](https://projectreactor.io/) 提供。 ```java public interface ReactiveExtensionClient { /** * Lists Extensions by Extension type, filter and sorter. * * @param type is the class type of Extension. * @param predicate filters the reEnqueue. * @param comparator sorts the reEnqueue. * @param is Extension type. * @return all filtered and sorted Extensions. */ Flux list(Class type, Predicate predicate, Comparator comparator); /** * Lists Extensions by Extension type, filter, sorter and page info. * * @param type is the class type of Extension. * @param predicate filters the reEnqueue. * @param comparator sorts the reEnqueue. * @param page is page number which starts from 0. * @param size is page size. * @param is Extension type. * @return a list of Extensions. */ Mono> list(Class type, Predicate predicate, Comparator comparator, int page, int size); /** * Fetches Extension by its type and name. * * @param type is Extension type. * @param name is Extension name. * @param is Extension type. * @return an optional Extension. */ Mono fetch(Class type, String name); Mono fetch(GroupVersionKind gvk, String name); Mono get(Class type, String name); /** * Creates an Extension. * * @param extension is fresh Extension to be created. Please make sure the Extension name does * not exist. * @param is Extension type. */ Mono create(E extension); /** * Updates an Extension. * * @param extension is an Extension to be updated. Please make sure the resource version is * latest. * @param is Extension type. */ Mono update(E extension); /** * Deletes an Extension. * * @param extension is an Extension to be deleted. Please make sure the resource version is * latest. * @param is Extension type. */ Mono delete(E extension); } ``` ### 示例 如果你想在插件中根据 name 参数查询获取到 Person 自定义模型的数据,则可以这样写: ```java private final ReactiveExtensionClient client; Mono getPerson(String name) { return client.fetch(Person.class, name); } ``` 或者使用阻塞式 API: ```java private final ExtensionClient client; Optional getPerson(String name) { return client.fetch(Person.class, name); } ``` 我们建议你更多的使用响应式的 `ReactiveExtensionClient` 去替代 `ExtensionClient`。