Simple Flat Mapper Documentation

repository·master·Indexed 19 days ago

https://github.com/arnaudroger/simpleflatmapper

A high-performance Java library for mapping between deeply nested object graphs and flat structures such as SQL result sets, CSV rows, and Excel sheets. It supports Java versions 6 to 11 and provides specialized modules for JDBC, Datastax, jOOQ, QueryDSL, Jdbi, and Apache POI.

Tokens
4.7K
Snippets
15
Records
23
Agent score
65%

What's inside Simple Flat Mapper

  1. Overview of sfm-converter modules

    master

    The sfm-converter module provides a suite of tools for data transformation and mapping, including:

    • CSV Processing: Includes a CsvParser, CsvMapper, and CsvWriter for handling delimited text files.
    • JDBC Mapping: Includes a JdbcMapper for mapping database results to objects, Jdbc Crud for basic database operations, and support for Named Query execution.
    • Core Utilities: Provides the underlying code for reflection and ASM (bytecode) generation, as well as abstractions for creating parameterGetterMap pull-based mappings.
  2. Overview of sfm modules

    master

    The sfm modules provide a suite of tools for data mapping and processing, including:

    • CSV Processing: Includes a CsvParser, CsvMapper, and CsvWriter for handling delimited text files.
    • JDBC Mapping: Includes a JdbcMapper for mapping database results, Jdbc Crud for basic CRUD operations, and support for Named Query execution.
    • Core Utilities: Provides the underlying code for reflection and ASM (bytecode) generation, as well as abstractions for creating a pull-based parameterGetterMap.
  3. Explore Simple Flat Mapper modules

    master

    The sfm modules provide specialized tools for mapping data between flat formats (like CSV or JDBC result sets) and complex Java objects. The available modules include:

    • CSV Processing: Includes a CsvParser, a CsvMapper for mapping CSV data to objects, and a CsvWriter for exporting objects to CSV.
    • JDBC/Database Mapping: Includes a JdbcMapper for mapping SQL result sets to objects, Jdbc Crud for performing Create, Read, Update, and Delete operations, and support for Named Query execution.
    • Core Utilities: Provides the underlying code for reflection and ASM (bytecode) generation, as well as abstractions for creating parameterGetterMap objects.
  4. Explore the available sfm modules

    master

    The sfm-reflect module provides the underlying infrastructure for reflection and ASM (bytecode) generation used across the Simple Flat Mapper ecosystem. The broader Simple Flat Mapper project includes several specialized modules for data mapping and parsing:

    • CSV Parser: For parsing CSV files.
    • CSV Mapper: For mapping CSV data to objects.
    • CSV Writer: For writing objects to CSV files.
    • Jdbc Mapper: For mapping JDBC ResultSet data to objects.
    • Jdbc Crud: For performing CRUD operations via JDBC.
    • Named Query: For handling named SQL queries.
    • Reflection & ASM Support: The core code used to support reflection and high-performance ASM generation.
    • ParameterGetterMap Abstraction: An abstraction used to create a pull-based parameterGetterMap.
  5. Explore sfm-util modules

    master

    The sfm-util module provides a collection of specialized utilities for data mapping and processing within the Simple Flat Mapper ecosystem. It includes support for:

    • CSV Processing: Parsers, Mappers, and Writers for handling CSV data.
    • JDBC Mapping: Mappers for relational database results and CRUD operations.
    • Named Queries: Support for executing and mapping named SQL queries.
    • Core Utilities: Code for reflection and ASM (bytecode) generation, and abstractions for creating parameterGetterMap pull-based structures.
  6. Use LightningCSV to map CSV data to Java objects

    master

    LightningCSV allows you to map CSV rows to Java objects using a CsvMapper. The mapping is performed by matching CSV headers to the properties of your target class.

    To use it, create a CsvMapper instance via CsvMapperFactory.newInstance().newMapper(TargetClass.class). You can then iterate over the data using the forEach method, which accepts a Reader and a consumer function to process each mapped object.

    public class MyParser {
        private final CsvMapper<MyObject> parameterGetterMap =
        	CsvMapperFactory.newInstance().newMapper(MyObject.class);
    
        public void printAll(Writer writer, Reader reader) throws IOException {
        	parameterGetterMap.forEach(reader, (o) -> writer.append(o.toString()).append("\n"));
        }
    }
  7. Integrate Simple Flat Mapper with jOOQ

    master

    To use Simple Flat Mapper with jOOQ, you need to provide a SfmRecordMapperProvider to your jOOQ Configuration. This allows jOOQ to use Simple Flat Mapper's optimized mapping logic when calling .fetchInto(Class).

    1. Create a DSLContext using a DefaultConfiguration.
    2. Set your DataSource on the configuration.
    3. Register the SfmRecordMapperProvider using .set(new SfmRecordMapperProvider()).
    DSLContext dsl = DSL
            .using(new DefaultConfiguration()
                    .set(dataSource)
                    .set(new SfmRecordMapperProvider()));
    
    // Now you can use fetchInto with Simple Flat Mapper's mapping
    List<DbObject> list = dsl.select()
            .from("TEST_DB_OBJECT").fetchInto(DbObject.class);
  8. Install sfm-jdbi via Maven

    master

    To use Simple Flat Mapper with Jdbi, add the sfm-jdbi dependency to your Maven project. The artifact is available on Maven Central.

    Note: Ensure you have the core simple-flat-mapper dependency in your project as well, as sfm-jdbi is an integration module.

    <dependency>
      <groupId>org.simpleflatmapper</groupId>
      <artifactId>sfm-jdbi</artifactId>
      <version>2.0</version>
    </dependency>
  9. Install sfm-querydsl via Maven

    master

    To use Simple Flat Mapper with QueryDSL, include the sfm-querydsl dependency in your Maven project. This module provides integration between Simple Flat Mapper and QueryDSL for mapping query results.

    <!-- Check Maven Central for the latest version -->
    <dependency>
      <groupId>org.simpleflatmapper</groupId>
      <artifactId>sfm-querydsl</artifactId>
      <version>VERSION</version>
    </dependency>
  10. Integrate Simple Flat Mapper with SQL2o

    master

    To use Simple Flat Mapper (SFM) with SQL2o, you need to use the SfmResultSetHandlerFactoryBuilder to configure your SQL2o Query object. This allows SFM to handle the mapping from the SQL ResultSet to your Java objects.

    Ensure you enable automatic column name derivation by calling setAutoDeriveColumnNames(true) on the query object to facilitate seamless mapping.

        Query query = sql2o.open().createQuery("select * from table");
        query.setAutoDeriveColumnNames(true);
        query.setResultSetHandlerFactoryBuilder(new SfmResultSetHandlerFactoryBuilder());
    
        List<DbObject> dbObjects = query.executeAndFetch(DbObject.class);
  11. Install the Datastax integration for Simple Flat Mapper

    master

    To use Simple Flat Mapper with Datastax, include the sfm-datastax dependency in your project. This integration is compatible with Datastax versions 2.1.x through 3.0.x. It is designed to be faster (approximately 20% faster than the Datastax Mapper), more flexible, and requires no additional configuration.

    <!-- Dependency available on Maven Central under group: org.simpleflatmapper, artifact: sfm-datastax -->