简体   繁体   English

是否可以在运行时创建Java模型对象并动态映射Spring Batch Reader中的字段?

[英]Is it possible to create a Java Model Object at runtime and map the fields from the Spring Batch Reader dynamically?

We are trying to make our spring batch jobs more generic. 我们正在努力使春季批处理工作更加通用。 In this process, we want to make the model object that gets passed between reader, processor and writer generic/dynamic too. 在此过程中,我们还希望使在读取器,处理器和写入器之间传递的模型对象具有通用性/动态性。 If we store the table-name, column-name, type-name in the database, and fetch this info at run time, will we be able to create a model class on the fly, map the data read in the reader and pass it along to the writer? 如果我们将表名,列名,类型名存储在数据库中,并在运行时获取此信息,我们将能够动态创建模型类,映射在读取器中读取的数据并将其传递跟作家在一起吗?

Is it possible to create MyClass at runtime and do the mappings based on the configuration? 是否可以在运行时创建MyClass并根据配置进行映射? RowMapper doesnt accept generic ? RowMapper不接受泛型吗? So, how do we specify it in the code without actually having an already compiled class at run time? 那么,我们如何在代码中指定它而不在运行时实际拥有一个已编译的类呢?

public class MyRowMapper implements RowMapper { 公共类MyRowMapper实现RowMapper {

@Override
public MyClass mapRow(ResultSet rs, int rowNum) throws SQLException {

    MyClass myClass= new MyClass();
            myClass.setName(rs.getString("Name"));
            myClass.setNumber(rs.getLong("Number"));
    return myClass;
}

}

will we be able to create a model class on the fly, map the data read in the reader and pass it along to the writer? 我们能否即时创建一个模型类,映射在读取器中读取的数据并将其传递给写入器?

Yes, using a bytecode library you can create classes at runtime, then assign values using reflection, and pass the object around, but what would be the point? 是的,使用字节码库,您可以在运行时创建类,然后使用反射分配值,然后将对象传递给周围对象,但这有什么意义呢?

If no code knows the object ahead of time, even the code using the data needs to use reflection to access the data. 如果没有代码提前知道对象,那么即使使用数据的代码也需要使用反射来访问数据。

You might as well just use a Map<String, Object> . 您也可以只使用Map<String, Object> Or some wrapping class around that, if you need to carry metadata too (eg value type when value is null). 或者围绕它的一些包装类,如果您也需要携带元数据(例如,当value为null时的值类型)。

Spring can already do this for you. Spring已经可以为您做到这一点。 Eg with JdbcTemplate , instead of calling 例如,使用JdbcTemplate ,而不是调用

<T> List<T> query(String sql, Object[] args, RowMapper<T> rowMapper)

you can just call 你可以打电话

List<Map<String,Object>> queryForList(String sql, Object... args)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Batch中地图的项目阅读器? - Item Reader for a Map in Spring Batch? 如何在春季批处理中将编组的对象值(读取器)映射到休眠实体对象(写入器) - How to map the marshalled object values(reader) to hibernate entity object(writer) in spring batch 是否可以在运行时将自己动态创建的对象注册为spring bean? - Is it possible to register an object dynamically created by myself in runtime as a spring bean? 模型上的Java Spring Batch Embedded字段始终为null - Java Spring Batch Embedded fields on model always null 如何在春季批量创建自定义项目阅读器 - How to create custom item reader in spring batch 如何在 Spring 批处理中为 JSON 文件创建阅读器 - How to create reader for JSON file in Spring Batch Java FileChannel与BufferedReader-Spring Batch-Reader - Java FileChannel Vs BufferedReader - Spring Batch - Reader 从 Spring MVC 作为 JSON 发送时,动态忽略来自 Java 对象的字段 - Ignore fields from Java object dynamically while sending as JSON from Spring MVC 我们可以在春季批处理时在运行时设置读取器,写入器,处理器类型吗 - Can we set reader, writer, processor types at runtime in spring batch 在Java 8中优雅地创建具有对象字段的映射作为来自对象流的键/值 - Elegantly create map with object fields as key/value from object stream in Java 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM