Db2csv about the learning of spring batch

Source: Internet
Author: User
Tags string format

The previous article has described how to import the contents of a CSV file into a database, and we'll look at how to export the data in the database to a CSV file

On the basis of the previous article, we will not re-create the MAVEN project without new additions.

First in the original project above the Java package to create a new configuration, to configure the job's related content, for the above article, we named Configuration_db_2_csv, the code is as follows:

1 @Configuration2 @EnableBatchProcessing3  Public classConfiguration_db_2_csv {4 @Bean5      PublicItemreader<user>Reader (DataSource DataSource) {6jdbccursoritemreader<user> reader =NewJdbccursoritemreader<user>();7 Reader.setdatasource (dataSource);8Reader.setsql ("Select User_id,user_name,address,birth,gender from User");9Reader.setrowmapper (NewBeanpropertyrowmapper<user> (User.class));Ten         returnReader; One     } A  - @Bean -      PublicItemprocessor<user, string>Processor () { the         return Newuseritemprocessor_db_2_csv (); -     } -  - @Bean +      Publicitemwriter<string> writer ()throwsIOException { -flatfileitemwriter<string> writer =NewFlatfileitemwriter<string>(); +Writer.setresource (NewPathresource ("Test.csv")); ADelimitedlineaggregator<string> Delimitedlineaggregator =NewDelimitedlineaggregator<string>(); atDelimitedlineaggregator.setdelimiter (","); - Writer.setlineaggregator (delimitedlineaggregator); -         returnwriter; -     } -  - @Bean in      PublicJob exportuserjob (jobbuilderfactory jobs, Step s1) { -         returnJobs.get ("Exportuser") to. Incrementer (Newrunidincrementer ()) + . Flow (S1) - . End () the . Build (); *     } $ Panax Notoginseng @Bean -      PublicStep Step1 (stepbuilderfactory stepbuilderfactory, itemreader<user>Reader, theitemwriter<string> writer, Itemprocessor<user, string>processor) { +         returnStepbuilderfactory.get ("Step1") A. <user, string> Chunk (10) the . Reader (reader) + . Processor (processor) - . Writer (writer) $ . Build (); $     } -  - @Bean the      Publicjdbctemplate JdbcTemplate (DataSource DataSource) { -         return NewJdbcTemplate (dataSource);Wuyi     } the  -}

Next, we need to add the handler personitemprocessor_db_2_csv, because we are currently storing the DB database processing in a CSV file, the final form is stored in a string format, so the return value type we are dealing with is set to type string, This will be a lot easier for us to deal with later. Below we will directly post our processing procedures, here, according to the needs of the project, the different filed processing. We're just a demo.

1  Public classUseritemprocessor_db_2_csvImplementsItemprocessor<user, string> {2 3 @Override4      PublicString Process (FinalUser user)throwsException {5         //Db2csv6         FinalString userId = "2014010" +User.getuserid ();7         FinalString gender = User.getgender (). Equals ("M")? " Male ":" Female ";8SimpleDateFormat SDF =NewSimpleDateFormat ("yyyy mm month DD Day");9         FinalString birth =Sdf.format (User.getbirth ());Ten         FinalString userName = User.getusername (). substring (0,1). toUpperCase () +user.getusername (). SUBSTRING (1). toLowerCase (); One         FinalString address= user.getaddress (). substring (0,1). toUpperCase () +user.getaddress (). SUBSTRING (1). toLowerCase (); A         returnUserid+ "," +username+ "," +getaddress+ "," +birth+ "," +gender; -     } -}

The last step, in order to prevent the writing of the file is useless, here I need to remove the schema_all.sql from the previous resources package, or to replace the build table statement, to prevent the data in the database is deleted. It is also necessary to comment out the @configuration and @enablebatchprocessing annotations in the previous configuration file, otherwise the system will be confused with what configuration to start the job.

Db2csv about the learning of spring batch

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.