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,你必须使用以下接受其中一种参数的构造函数:
-
EntityManagerFactory
-
EntityManager
-
JpaOperations
以下示例展示了如何使用 entityManagerFactory 初始化 JpaExecutor 并在出站网关中使用它:
@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;
}