title: How to Read Records From Any Database Table and Export As TextFile Using Spring Batch
body: <p>I am building a spring batch job that will be invoked through a webservice. The webservice will take a list of select and delete statement pairs. The records returned by the select statement will be saved as a CSV on the filesystem and then those same records will be deleted by executing the supplied delete statement. </p>
  
  <p>I have seen a number of ColumnRowMapper examples but that requires me to create a POJO for each table entity. I am looking for a solution that will handle any column from any table. Any suggestions on approach?</p>
  
  <p>****UPDATE****
  Since writing this post, I've landed on the following solution.</p>
  
  <pre><code>@Bean
  @StepScope
  public JdbcCursorItemReader&lt;Map&lt;String, ?&gt;&gt; getRowsOfDataForExportFromTable(){
      JdbcCursorItemReader&lt;Map&lt;String, ? extends Object&gt;&gt; databaseReader = new JdbcCursorItemReader&lt;&gt;();
  
      databaseReader.setDataSource(jdbcTemplate.getDataSource());
      databaseReader.setSql("select * from SOME_TABLE where last_updated_date &lt; DATE_SUB(NOW(), INTERVAL 10 DAY);");
      databaseReader.setRowMapper(new RowMapper&lt;Map&lt;String, ? extends Object&gt;&gt;() {
          @Override
          public Map&lt;String, ? extends Object&gt; mapRow(ResultSet resultSet, int i) throws SQLException {
              Map&lt;String,String&gt; resultMap = new LinkedHashMap&lt;&gt;();
              int numOfColumns = resultSet.getMetaData().getColumnCount();
              for (int j = 1; j &lt; numOfColumns+1; j++){
                  String columnName = resultSet.getMetaData().getColumnName(j);
                  String value = resultSet.getString(j);
                  resultMap.put(columnName,value);
              }
  
              return resultMap;
          }
      });
      return databaseReader;
  }
  </code></pre>
  
  <p>The above ItemReader will build a LinkedHashMap row mapper where the column name is the key and the column value is the value. </p>