Java 实现
提供的每个组件都使用 o.s.i.jpa.core.JpaExecutor
类,该类又使用 o.s.i.jpa.core.JpaOperations
接口的一个实现。JpaOperations
操作类似于典型的 数据访问对象 (DAO),并提供诸如 find、persist、executeUpdate 等方法。对于大多数用例,默认实现 (o.s.i.jpa.core.DefaultJpaOperations
) 应该是足够的。但是,如果您需要自定义行为,可以指定自己的实现。
要初始化 JpaExecutor
,您必须使用以下构造函数之一,这些构造函数接受以下内容之一:
-
实体管理器工厂
-
实体管理器
-
Jpa 操作
以下示例展示了如何使用 entityManagerFactory
初始化 JpaExecutor
并在 outbound 网关中使用它:
@Bean
public JpaExecutor jpaExecutor() {
JpaExecutor executor = new JpaExecutor(this.entityManagerFactory);
executor.setJpaParameters(Collections.singletonList(new JpaParameter("firstName", null, "#this")));
executor.setUsePayloadAsParameterSource(true);
executor.setExpectSingleResult(true);
return executor;
}
@ServiceActivator(inputChannel = "getEntityChannel")
@Bean
public MessageHandler retrievingJpaGateway() {
JpaOutboundGateway gateway = new JpaOutboundGateway(jpaExecutor());
gateway.setGatewayType(OutboundGatewayType.RETRIEVING);
gateway.setOutputChannelName("resultsChannel");
return gateway;
}