javascript
rowmapper_Spring Integration Jdbc RowMapper示例
rowmapper
JDBC入站通道適配器的基本功能是執行SQL查詢,提取數據并將以Message形式封裝的結果集傳遞到本地通道。 您可以在JDBC入站通道適配器的示例中閱讀有關此內容的更多信息。
有效負載的類型由行映射策略決定。 默認結果是產生類型為List的有效負載,其中每個元素都是列值的Map。 在上一篇有關Jdbc入站適配器的文章中,我們使用了默認的行映射策略,這就是為什么消息包含映射值列表作為有效負載的原因。 列值將作為Map返回,列名是鍵值。
在本文中,我們將看到如何自定義映射策略,以便有效負載是POJO的列表。 我們還將學習如何處理JDBC消息以及使用transactional元素。
在開始示例之前,首先將模塊依賴項添加到pom.xml 。
依存關系
添加以下依賴項:
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
總結
以上是生活随笔為你收集整理的rowmapper_Spring Integration Jdbc RowMapper示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 并配上五笔输入法教程电脑上如何安装五笔输
- 下一篇: 给自己打造一个舒适的电脑桌面给自己打造一