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