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:通用的出站适配器。请参阅通用入站和出站通道适配器