rowmapper
JDBC入站通道适配器的基本功能是执行SQL查询,提取数据并将以Message
形式封装的结果集传递到本地通道。 您可以在JDBC入站通道适配器的示例中阅读有关此内容的更多信息。
有效负载的类型由行映射策略决定。 默认结果是产生类型为List的有效负载,其中每个元素都是列值的Map。 在上一篇有关Jdbc入站适配器的文章中,我们使用了默认的行映射策略,这就是为什么消息包含映射值列表作为有效负载的原因。 列值将作为Map返回,列名是键值。
在本文中,我们将看到如何自定义映射策略,以便有效负载是POJO的列表。 我们还将学习如何处理JDBC消息以及使用transactional
元素。
在开始示例之前,首先将模块依赖项添加到pom.xml
。
依存关系
添加以下依赖项:
-
spring-core
-
spring-context
-
spring-integration-core
-
spring-integration-jdbc
–返回此值以访问jdbc适配器 -
mysql-connector-java
–我们将使用MySQL作为数据库,因此您需要添加MySql驱动程序
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.javarticles.spring.integration.jms</groupId><artifactId>springintegrationjms</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework.integration</groupId><artifactId>spring-integration-core</artifactId><version>4.1.2.RELEASE</version></dependency><dependency><groupId>org.springframework.integration</groupId><artifactId>spring-integration-jdbc</artifactId><version>4.1.2.RELEASE</version><scope>compile</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.26</version></dependency> </dependencies><properties><spring.version>4.1.4.RELEASE</spring.version></properties></project>
自定义行映射器
文章:
package com.javarticles.spring.integration.jdbc;public class Article {private int id;private String name;private String tags;private String category;private String author;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getTags() {return tags;}public void setTags(String tags) {this.tags = tags;}public String getCategory() {return category;}public void setCategory(String category) {this.category = category;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public String toString() {StringBuilder sb = new StringBuilder("id: ");sb.append(id).append(", name: ").append(name).append(", tags: ").append(tags).append(", category: ").append(category).append(", author").append(author);return sb.toString();}
}
我们将通过实现org.springframework.jdbc.core.RowMapper接口并通过row-mapper属性引用此类来自定义有效负载对象。
ArticleRowMapper:
package com.javarticles.spring.integration.jdbc;import java.sql.ResultSet;
import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;public class ArticleRowMapper implements RowMapper<Article> {public Article mapRow(ResultSet rs, int rowNum) throws SQLException {String name = rs.getString("name");String category = rs.getString("category");String author = rs.getString("author");String tags = rs.getString("tags");int id = rs.getInt("id");Article article = new Article();article.setId(id);article.setCategory(category);article.setAuthor(author);article.setName(name);article.setTags(tags);return article;}
}
这是模式和一些测试数据:
db-schema.sql:
drop table if exists `articles`;
CREATE TABLE `articles` (`ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,`NAME` VARCHAR(100) NOT NULL,`CATEGORY` VARCHAR(50) NOT NULL,`TAGS` VARCHAR(100) NOT NULL,`AUTHOR` VARCHAR(50) NOT NULL,`SENT` INT,PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
db-test-data.sql:
insert into articles(id, name, category, tags, author, sent) values (1, "SpringIntegration Example", "spring", "spring,integration", "Joe", 0);
insert into articles(id, name, category, tags, author, sent) values (2, "NamedParameterJdbcTemplate Example", "spring", "spring,jdbcTemplate", "Sam", 0);
insert into articles(id, name, category, tags, author, sent) values (3, "MVC Example", "spring", "spring", "Joe", 0);
我们还将在这里介绍交易概念。 提取文章后,我们希望将“已发送”列更新为1,以便已阅读的文章不会在下一次轮询中显示。
我们将简单地将tansactional元素添加到poller元素。 这将导致更新和选择查询在同一事务中运行。 由于我们依赖事务,因此我们需要配置事务管理器。
我们引入的另一件事是服务适配器,以防您想处理JDBC消息。
JdbcMessageHandler:
package com.javarticles.spring.integration.jdbc;import java.util.List;public class JdbcMessageHandler {public void handleMessage(List<Article> articleList) {System.out.println("In JdbcMessageHandler:" + articleList);}
}
让我们看看我们的配置。
jdbcInboundApplicationContext.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:int="http://www.springframework.org/schema/integration"xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:int-jdbc="http://www.springframework.org/schema/integration/jdbc"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/integrationhttp://www.springframework.org/schema/integration/spring-integration.xsdhttp://www.springframework.org/schema/integration/jdbchttp://www.springframework.org/schema/integration/jdbc/spring-integration-jdbc.xsdhttp://www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"><int-jdbc:inbound-channel-adapter id="dataChannel"query="select * from articles where author='Joe' and sent = 0" update="update articles set sent = 1 where id in (:id)"data-source="dataSource" row-mapper="articleRowMapper"><int:poller fixed-rate="100"><int:transactional /></int:poller></int-jdbc:inbound-channel-adapter><int:service-activator input-channel="dataChannel" ref="jdbcMessageHandler" /><bean id="jdbcMessageHandler"class="com.javarticles.spring.integration.jdbc.JdbcMessageHandler" /><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><int:poller default="true" fixed-rate="100" /><int:channel id="dataChannel"><int:queue /></int:channel><jdbc:initialize-database data-source="dataSource"enabled="true"><jdbc:script location="classpath:db-schema.sql" /><jdbc:script location="classpath:db-test-data.sql" /></jdbc:initialize-database><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost/test" /><property name="username" value="root" /><property name="password" value="mnrpass" /></bean><bean id="articleRowMapper"class="com.javarticles.spring.integration.jdbc.ArticleRowMapper" />
</beans>
我们的主类看起来很简单,我们只需要加载上下文即可启动轮询。
SpringIntegrationJdbcInboundRowMapper示例:
package com.javarticles.spring.integration.jdbc;import java.io.IOException;
import java.sql.SQLException;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringIntegrationJdbcInboundRowMapperExample {public static void main(String[] args) throws InterruptedException, IOException, SQLException {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("jdbcInboundApplicationContext.xml");Thread.sleep(1000);context.close();}}
输出:
In JdbcMessageHandler:[id: 1, name: SpringIntegration Example, tags: spring,integration, category: spring, authorJoe, sent: 0, id: 3, name: MVC Example, tags: spring, category: spring, authorJoe, sent: 0]
下载源代码
这是有关带有RowMapper的Jdbc入站适配器的示例。 您可以在此处下载源代码: springintegrationJdbcRowMapper.zip
翻译自: https://www.javacodegeeks.com/2015/05/spring-integration-jdbc-rowmapper-example.html
rowmapper