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;
}