Super CSV Dozer Extension framework tutorial
Super CSV Dozer Extension framework tutorial
Super CSV Dozer Extension is a Java framework that combines Super CSV and Dozer to simplify the conversion process between CSV files and Java objects.This tutorial will show you how to use the Super CSV Dozer Extension framework for configuration and programming to better understand its usage.
1. Introduction to dependencies
In order to start using Super CSV Dozer Extension, you need to add the following dependencies to the construction file of the project:
<dependency>
<groupId>net.sf.supercsv</groupId>
<artifactId>super-csv</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.github.mygreen</groupId>
<artifactId>super-csv-dozer</artifactId>
<version>2.6</version>
</dependency>
2. Create a CSV file
First, you need to create a file containing CSV data.Suppose you have a CSV file containing the following: name, Age, Address.Save the file as "Sample.csv".
3. Create a Java object
In order to map CSV data to the Java object, you need to create a Java class corresponding to columns in the CSV file.In this example, we create a Java class called "Person", which has "name", "Age" and "Address" fields::
public class Person {
private String name;
private int age;
private String address;
// Getters and setters
}
4. Create dozer configuration file
Next, you need to create a Dozer configuration file and tell the Super CSV Dozer Extension how to map the CSV to the Java object.Create a file called "Dozer-Configuration.xml" and add the following:
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozermapper.github.io/schema/bean-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping
http://dozermapper.github.io/schema/bean-mapping.xsd">
<mapping map-null="true">
<class-a>csv.Person</class-a>
<class-b>Person</class-b>
<field>
<a>name</a>
<b>name</b>
</field>
<field>
<a>age</a>
<b>age</b>
</field>
<field>
<a>address</a>
<b>address</b>
</field>
</mapping>
</mappings>
This configuration file specifies how to map the CSV file to the field of the Java object.
5. Writing code
Now, let's see how to use the Super CSV Dozer Extension in the Java code to read the CSV file and convert it to the Java object.Create a class called "Main" and add the following code:
import org.supercsv.io.CsvBeanReader;
import org.supercsv.prefs.CsvPreference;
import com.github.mygreen.supercsv.io.dozer.CsvDozerBeanReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
CsvPreference csvPreference = CsvPreference.STANDARD_PREFERENCE;
try (CsvDozerBeanReader<Person> csvReader = new CsvDozerBeanReader<>(
new FileReader("sample.csv"), csvPreference, "dozer-configuration.xml")) {
csvReader.getHeader(true);
csvReader.configureBeanMapping(Person.class, "person");
Person person;
while ((person = csvReader.read(Person.class)) != null) {
System.out.println(person.getName() + ", " + person.getAge() + ", " + person.getAddress());
}
}
}
}
This code uses CSVDozerbeanReader to read CSV files and converts it into Person objects.The configuration file "Dozer-configuration.xml" is used to specify the mapping relationship between the CSV column and the Java object field.By calling the `CSVReader.Getheader (TRUE)`, you can skip the title line of the CSV file.Finally, we traversed the Person object read through CSV and print its attribute value.
6. Running code
You can convert the content of the CSV file into the Java object and print it by running the Java class above.
Through the above steps, you have successfully learned how to use the Super CSV Dozer Extension framework to simplify the conversion process between the CSV file and the Java object.