Filling Java beans with opencsv – Code Description

I just started using Java and have a lot of missing knowledge, but I need to write a simple class that will use opencsv to convert CSV files into JavaBeans I found some answers like queuing here, but no one can help me So far, there are such codes:

ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy(); 
strat.setType(YourOrderBean.class); 
String[] columns = new String[] {"name","orderNumber","id"}; // the fields to bind do in your JavaBean 
strat.setColumnMapping(columns); 
CsvToBean csv = new CsvToBean(); 
List list = csv.parse(strat,yourReader);

It is located on the opencsv FAQ website, and there is another question The problem is that I can't find what the specification object columnpositionmappingstrategy should look like and what it should look like in strat SetType statement (yourorder bean. Class) The setcolumnmapping method is the same for me, but I believe that when I know the rest, I can think of one myself

Will anyone explain the code more friendly? The opencsv document is very short for me because I lack some basic java knowledge (it's very different from PHP)

Thank you in advance!

Solution

This is a property that defines how to map string [] as a CSV line to a JavaBean

Let's suppose you have a class like this:

public class JavaBeanExample {

    private Integer id;
    private String name;
    private Integer orderNumber;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(Integer orderNumber) {
        this.orderNumber = orderNumber;
    }
}

The key points about this course are:

>Use the public noargs constructor; This is the default value in Java, so you don't need to specify a constructor. > All properties are private. > All attributes have getters, i.e. methods with return values named getxxxx. > All properties have setters, that is, the setxxxx method that sets their values

Now? The first method, setType, accepts the class of the bean It uses it to create instances through reflection In this case, we will call:

strat.setType(JavaBeanExample.class);

Next, let's assume that we have a CSV in the following format

So we need to map the first column to our name attribute, the second column to OrderNumber attribute, and the third column to ID. we use the attribute name in the bean to tell opencsv which setter to use Then, opencsv uses propertydescriptor to set properties through corresponding named setters

In this case, we will call

String[] columns = new String[] {"name","id"};
strat.setColumnMapping(columns);

Now that all this is done, we can start opencsv by calling

List list = csv.parse(strat,yourReader);

This will return a list of javabeanexample, one for each line in the file

But this is a bit unpleasant because we have to convert each item in the list This is because this example is a little outdated This is an example of using generics, which is written in Java 7

final ColumnPositionMappingStrategy<JavaBeanExample> strategy = new ColumnPositionMappingStrategy<>();
strategy.setType(JavaBeanExample.class);
strategy.setColumnMapping(new String[]{"name","id"});
final CsvToBean<JavaBeanExample> csvToBean = new CsvToBean<>();
final List<JavaBeanExample> beanExamples;
try (final Reader reader = new FileReader("myFile.csv")) {
    beanExamples = csvToBean.parse(strategy,reader);
} catch (IOException ex) {
    throw new RuntimeException(ex);
}

The difference here is that we use angle brackets to tell columnpositionmappingstrategy its generic type We also tell csvtobean its generic type This means that when we call the parsing list < < javabeanexample >, it is returned; That is, know the list of its common types Now we don't have to convert individual elements in the list

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>