-1

Background

We have a list of tables in a database, we want to read them through select statements, then turn the result into csv files

Problem

I could use mybatis to load data from tableA then map it to a resultMap A, then using openCSV to save it as a csv file.

but I am seeking a generic way, which I only need to specify

<sql> select * from tableA </sql>
<csvFile> A.csv </csvFile>
<mapping> Table.1stName = CSV.firstName </Mapping>
<mapping> Table.2ndName = CSV.secondName </Mapping>
........

in my config.xml, and my program should be able to run the sql select then map the resultset to a generic resutMap in mybatis? or turn the results into xml or csv or any string format?

then I can composing the csv file using the mapping in my config

the key question is how to let the mybatis return a more generic format of the resultset, rather than map it to a specific object.

user5324782
  • 177
  • 1
  • 1
  • 14
  • Does your database not have an export feature for that? What's the use case? – ftr Oct 02 '15 at 17:19
  • 2
    Honestly, the problem is easy enough, since you only need basic JDBC for that. I would probably add Spring into the mix to make it a little bit easier, but effectivly, you could only write the query (without *, but explicit list of columns) and read it, writing the fields (by index) into a csv. As ftr already mentioned, most databases should allow an export anyway, but the code for this sould be simple with JDBC alone, I wouldn't use MyBatis for that. – Florian Schaetz Oct 02 '15 at 17:59
  • You could use spring batch which does have inbuilt mybatis db reader and writer to write as csv file. You can bringup the application in an hour or 2. – Karthik Prasad Oct 09 '15 at 07:14

1 Answers1

1

@FlorianSchaetz indeed for low level parsing Spring JDBC comes in handy. Mybatis is a ORM, I didn't need to map the object. no need to using Mybatis. Finally the implementation is simple, Paring the XML using JAXB, then excute the sql using Spring JDBC, which returns a resultset, then according to the mapping names. extract data from the dataset, them amp to the right csv file field.

user5324782
  • 177
  • 1
  • 1
  • 14