Jdbc 入站介面卡 - xml 配置

官方參考檔案中 ,它說:

入站通道介面卡的主要功能是執行 SQL SELECT 查詢並將結果集作為訊息進行轉換。訊息有效負載是整個結果集,表示為 List,列表中項的型別取決於所使用的行對映策略。預設策略是一個通用對映器,它只返回查詢結果中每一行的 Map。

StackOverflow 文件

  • 原始碼

    public class Application {
       static class Book {
          String title;
          double price;
    
          Book(String title, double price) {
              this.title = title;
              this.price = price;
          }
    
          double getPrice() {
              return price;
          }
    
          String getTitle() {
              return title;
          }
    
          @Override
          public String toString() {
              return String.format("{title: %s, price: %s}", title, price);
          }
        }
    
        static class Consumer {
          public void consume(List<Book> books) {
              books.stream().forEach(System.out::println);
          }
        }
    
        static class BookRowMapper implements RowMapper<Book> {
    
          @Override
          public Book mapRow(ResultSet rs, int rowNum) throws SQLException {
              String title = rs.getString("TITLE");
              double price = rs.getDouble("PRICE");
              return new Book(title, price);
          }
        }
    
        public static void main(String[] args) {
          new ClassPathXmlApplicationContext(
                  "classpath:spring/integration/stackoverflow/jdbc/jdbc.xml");
        }
    }
    
  • xml 配置檔案

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:jdbc="http://www.springframework.org/schema/jdbc"
           xmlns:int="http://www.springframework.org/schema/integration"
           xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
           http://www.springframework.org/schema/integration/jdbc http://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsd
           http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
        <jdbc:embedded-database id="dataSource" type="H2">
            <jdbc:script location="classpath:spring/integration/stackoverflow/jdbc/schema.sql"/>
        </jdbc:embedded-database>
        <bean id="bookRowMapper"
              class="spring.integration.stackoverflow.jdbc.Application$BookRowMapper"/>
    
        <int:channel id="channel"/>
    
        <int-jdbc:inbound-channel-adapter id="jdbcInbound"
                                          channel="channel"
                                          data-source="dataSource"
                                          query="SELECT * FROM BOOKS"
                                          row-mapper="bookRowMapper">
            <int:poller fixed-rate="1000"/>
        </int-jdbc:inbound-channel-adapter>
    
        <int:outbound-channel-adapter id="outbound" channel="channel" method="consume">
            <bean class="spring.integration.stackoverflow.jdbc.Application$Consumer"/>
        </int:outbound-channel-adapter>
    </beans>
    
  • schema.sql 檔案

    CREATE TABLE BOOKS (
      TITLE VARCHAR(20) NOT NULL,
      PRICE DOUBLE      NOT NULL
    );
    
    INSERT INTO BOOKS(TITLE, PRICE) VALUES('book1', 10);
    INSERT INTO BOOKS(TITLE, PRICE) VALUES('book2', 20);
    
  • 摘要:

    • jdbcInbound:Jdbc 入站通道介面卡。它執行 SQL SELECT * FROM BOOKS 並通過 bean bookRowMapper 將結果集轉換為 List<Book>。最後,它將此書​​列表傳送到渠道 channel
    • channel:轉移訊息
    • outbound:通用的出站介面卡。請參閱通用入站和出站通道介面卡