在spring batch中如何使用rowmapper
概述
在spring batch框架中提供了三个核心的概念,分别是reader和processor和writer,分别用于读取,处理和写数据。关于这部分更详细的内容可以参考博客:批处理框架spring batch介绍及使用。这里不再展开。本篇文章主要想记录的问题是使用spring batch时,对于读取到的数据我们应该怎样去处理的问题。
怎样去处理读取到的数据,这个问题本质上是由需求决定的。如果读取到的数据不需要做任何处理,只需要插入到目标数据库即可的话,这意味着procesor这个模块不需要任何逻辑。而如果我们读取到的数据是需要经过处理才能写入的,那这必然导致procesor需要写逻辑代码,且我们必须将读到的数据序列化成一个能操作的对象,当然这也是根据需求决定的。
什么是rowmapper
在spring batch的reader读取到数据之后,也都是将数据放到ResultSet对象里面的,这与直接使用JdbcTemplate无异。rowmapper是spring jdbc里面定义的接口,其左右就是用于将从数据库读到的一行记录,映射到另一个对象里面去。该接口定义如下:
@FunctionalInterface
public interface RowMapper<T> {/*** Implementations must implement this method to map each row of data* in the ResultSet. This method should not call {@code next()} on* the ResultSet; it is only supposed to map values of the current row.* @param rs the ResultSet to map (pre-initialized for the current row)* @param rowNum the number of the current row* @return the result object for the current row (may be {@code null})* @throws SQLException if a SQLException is encountered getting* column values (that is, there's no need to catch SQLException)*/@NullableT mapRow(ResultSet rs, int rowNum) throws SQLException;}
要使用rowmapper将数据映射为一个对象,只需要实现该mapRow方法即可。
实现Rowmapper
我们可以实现Rowmapper接口,例如下面的这个例子:
public class BPRowMapper implements RowMapper<BP> {@Nullable@Overridepublic Student mapRow(ResultSet resultSet, int rowNum) throws SQLException {Student student = new Student();student.setPkId(resultSet.getString("pkId"));student.setChangedAt(resultSet.getDate("changedAt"));student.setCreatedAt(resultSet.getDate("createdAt"));student.setChangedBy(resultSet.getString("changedBy"));student.setCreatedBy(resultSet.getString("createdBy"));student.setCompany(resultSet.getString("company"));student.setMax_deletion_date(resultSet.getDate("max_deletion_date"));student.setFirstName(resultSet.getString("firstName"));student.setLastName(resultSet.getString("lastName"));student.setName(resultSet.getString("name"));student.setVersion(resultSet.getInt("version"));student.setIs_blocked(resultSet.getBoolean("is_blocked"));return student;}
}
上述代码就是将读到的数据映射到一个student的对象当中,后续操作就基于Student对象即可,这样的好处是类型安全,同时操作方便,但是类型安全带来的代价是失去了灵活性
使用ColumnMapRowMapper
使用ColumnMapRowMapper的作用就是将读回来的数据存在一个map对象里面,map的key就是字段的名字,map的value就是字段的值,这样的坏处是失去了一定的类型安全,同时操作数据比较不方便,但是它的核心好处在于灵活性。
在上述例子当中我们定义了一个Student对象用于持久化,当读回来的数据有多种类型时,那么就需要定义相应的对象才可以。需要处理的数据越多时,则需要定义的对象越多,需要操作的对象也就越多。
这个时候map的优势就体现出来了,map是一个灵活的结构,并不需要指定字段名字。它的使用方法非常简单:
private JdbcCursorItemReader generateJdbcCursorItemReader() {JdbcCursorItemReader<Map<String, Object>> itemReader = new JdbcCursorItemReader<>();itemReader.setDataSource(dataSource);itemReader.setSql("sql");itemReader.setRowMapper(new ColumnMapRowMapper());return itemReader;}
ColumnMapRowMapper是spring实现的一个类,它已经自己实现了mapRow方法,我们只需直接使用即可。它的maprow方法实现如下:
@Overridepublic Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {ResultSetMetaData rsmd = rs.getMetaData();int columnCount = rsmd.getColumnCount();Map<String, Object> mapOfColValues = createColumnMap(columnCount);for (int i = 1; i <= columnCount; i++) {String key = getColumnKey(JdbcUtils.lookupColumnName(rsmd, i));Object obj = getColumnValue(rs, i);mapOfColValues.put(key, obj);}return mapOfColValues;}
其逻辑和我们描述的一样,就是根据读到的放在ResultSet里面的数据,然后将其按照key-value的方式放在map里。map的key就是column的名字,由方法getColumnKey获得,map的value就是column的值,由方法getColumnValue获得。
但是值得注意的是这两种方式获取到的数据在写数据时的逻辑也是有所区别的,需要做相应调整。
因此,在spring batch迁移数据时,我们可以灵活的根据自己的需求选择恰当的rowmapper,若我们只需要迁移数据而不需要做任何数据操作时,则我们可以考虑使用ColumnMapRowMapper,这将会大大减少我们需要写的代码,其灵活性也更强。
在spring batch中如何使用rowmapper相关推荐
- Spring Batch中的块处理
大数据集的处理是软件世界中最重要的问题之一. Spring Batch是一个轻量级且强大的批处理框架,用于处理数据集. Spring Batch Framework提供了"面向Tasklet ...
- Spring Batch中面向TaskletStep的处理
许多企业应用程序需要批处理才能每天处理数十亿笔交易. 必须处理这些大事务集,而不会出现性能问题. Spring Batch是一个轻量级且强大的批处理框架,用于处理这些大数据集. Spring Batc ...
- 介绍Spring Batch 中Tasklet 和 Chunks
介绍Spring Batch 中Tasklet 和 Chunks Spring Batch 提供了两种不同方式实现job: tasklet 和 chunk.本文通过实例实践两种方法. 示例需求说明 给 ...
- Spring Batch在大型企业中的最佳实践
在大型企业中,由于业务复杂.数据量大.数据格式不同.数据交互格式繁杂,并非所有的操作都能通过交互界面进行处理.而有一些操作需要定期读取大批量的数据,然后进行一系列的后续处理.这样的过程就是" ...
- java中batch基础_详解Spring batch 入门学习教程(附源码)
详解Spring batch 入门学习教程(附源码) 发布时间:2020-09-08 00:28:40 来源:脚本之家 阅读:99 作者:achuo Spring batch 是一个开源的批处理框架. ...
- 首次使用批处理框架 Spring Batch ,被震撼到了,太强大...
以下文章来源方志朋的博客,回复"666"获面试宝典 spring batch简介 spring batch是spring提供的一个数据处理框架.企业域中的许多应用程序需要批量处理才 ...
- 使用 Spring Batch 构建企业级批处理应用
https://www.ibm.com/developerworks/cn/java/j-lo-springbatch1/index.html https://www.ibm.com/develope ...
- Spring batch 2.0例子(lineMapper)
http://www.ibm.com/developerworks/cn/java/j-lo-springbatch1/ 使用 Spring Batch 构建企业级批处理应用: 第 1 部分 刘 光瑞 ...
- Spring batch Job define
http://www.ibm.com/developerworks/cn/java/j-lo-springbatch1/ 总述 本系列文章旨在通过示例搭建以及特性介绍,详细讲述如何利用 Spring ...
最新文章
- [已解决] InnoDB: preallocating bytes for file ./ibdata1 failed with error
- MySQL_控制台操作_01
- mastered skills
- HTML 30分钟入门教程
- 03-spring_配置bean
- python多个变量与字符串判断_python怎么判断变量是否为字符串
- 龙族幻想服务器维护中怎么办,龙族幻想遇到无法连接服务器?两招教你轻松解决...
- 文玩扇子(折扇)的寸、方、排口、头分别指什么?
- 《Two Dozen Short Lessons in Haskell》学习(三)
- Android手机root概念
- 乘法口诀表 java_利用java 实现一个九九乘法口诀表
- texLive使用一条龙
- 串口总线舵机之舵机命令
- 牛牛倒计时抽签软件1.0发布
- 无线调试神器:无线WiFi串口透传模块使用
- 本题要求实现一个函数,用下列公式求cos(x)的近似值,精确到最后一项的绝对值小于e
- 课堂笔记(3) 假设检验 Hypothesis testing
- D和弦的音阶在尤克里里上应该怎么按?
- Splash抓取jd
- 【考研政治】2021肖八整理(毛中特部分)