Spring batch sample (read and write dB data tables using a cursor) (8)

Source: Internet
Author: User

The previous article about spring batch describes springbatch's read and write operations on flat, XML, and other files. This article will discuss with you spring batch's read and write operations on DB. Spring batch provides two methods to read dB data. One is to read data in one row based on a cursor, and the other is to read the database by page.

Pass the precedingArticleYou should have a certain understanding of the springbatch framework and basic configuration. In order not to be wordy, this article only provides the core configuration for reading the DB mode, and some auxiliary information configuration. If you do not understand it, you can refer to the content mentioned above.

First, read operations:

 1   <  BEAN: Bean  ID  = "Jdbcitemreader"  
2 Class = "Org. springframework. batch. item. database. jdbccursoritemreader"
3 Scope = "Step" >
4 < BEAN: Property Name = "Datasource" Ref = "Datasource" />
5 < BEAN: Property Name = "SQL"
6 Value = "Select ID, userid, username, password from t_user where ID & lt ;? " />
7 < BEAN: Property Name = "Rowmapper" >
8 < BEAN: Bean Class = "Org. springframework. JDBC. Core. beanpropertyrowmapper" >
9 < BEAN: Property Name = "Mappedclass"
10 Value = "JP. co. necst. eqix. batch. jdbctwodest. pojo. User" />
11 </ BEAN: Bean >
12 </ BEAN: Property >
13 < BEAN: Property Name = "Preparedstatementsetter" Ref = "Paramstatementsetter" />
14 </ BEAN: Bean >
15
16 < BEAN: Bean ID = "Paramstatementsetter"
17 Class = "Org. springframework. batch. Core. Resource. listpreparedstatementsetter"
18 Scope = "Step" >
19 < BEAN: Property Name = "Parameters" >
20 < BEAN: List >
21 < BEAN: Value > # {Jobparameters ['id']} </ BEAN: Value >
22 </ BEAN: List >
23 </ BEAN: Property >
24 </ BEAN: Bean >

Spring batch performs cursor-based data read operations on the database, which is implemented by its core class jdbccursoritemreader. Generally, reading data from a DB table involves the following steps. The first is dB connection. The basic information (such as the DB server address, user name, and password) of these connected databases is provided by the datasource attribute. springbatch does not provide a special class, the datasource of the Spring framework is used. After connecting to the database, you need to pay attention to the specific query operations of the database, that is, the relevant information of SQL, which is implemented by its SQL attributes, it is mainly used to concatenate a string of the SQL text. With SQL, you may need to pass parameters. Some information about SQL parameters is met by the preparedstatementsetter attribute. The specific implementation can be set by the core class listpreparedstatementsetter provided by springbatch, as shown inCode. When the database is connected and the SQL file is executed, the storage of the query results is concerned. This is implemented by the rowmapper attribute of jdbccursoritemreader, such as lines 7-12 of the Code. In this way, the data in the DB data table can be mapped into our pojo object, and the read operation is completed.

The second is write operations:

 1   <  BEAN: Bean  ID = "Jdbcitemwriter"  
2 Class = "Org. springframework. batch. item. database. jdbcbatchitemwriter" >
3 < BEAN: Property Name = "Datasource" Ref = "Datasource" />
4 < BEAN: Property Name = "SQL"
5 Value = "Insert into t_destuser (ID, userid, username, password, updatetime, updateuser)
6 Values
7 (: ID,: userid,: username,: Password,: updatedate,: updateuser )" />
8 < BEAN: Property Name = "Itemsqlparametersourceprovider" >
9 < BEAN: Bean
10 Class = "Org. springframework. batch. item. database. beanpropertyitemsqlparametersourceprovider" />
11 </ BEAN: Property >
12 </ BEAN: Bean >

The idea of writing a database is the same as that of reading a database. One is reading from a DB, and the other is writing to a DB. Is implemented by the jdbcbatchitemwriter class of the springbatch framework. There are also the following steps: first, connect to the database, which is also provided by the datasource attribute; second, the executed SQL text, where the SQL attribute is satisfied, and finally how to pass the parameter. When a write operation passes a parameter, it is different from a read operation. Write operations provide two ways to pass parameters: one is to directly pass an object in, as shown in the above Code in line 8-11, the itemsqlparametersourceprovider attribute is set to beanpropertyitemsqlparametersourceprovider. The parameters in SQL are also in the [object attribute name] mode, as shown in the seven lines of the code above, and are case sensitive. The second method for passing parameters is to set the itemsqlparametersourceprovider attribute of the jdbcbatchitemwriter class (the setting method is similar to the setting method of the paramstatementsetter attribute of the read operation). Of course, SQL is also different from the first method, insert into t_destuser (ID, userid, username, password, updatetime, updateuser) values (?,?,?,?,?,?). The two methods have their own advantages and can be selected based on the actual situation.

So far, the simple operations on springbatch to read and write databases have been completed. We will discuss with you some advanced features and practical issues during Development next time.

 

 

 

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.