Spring Batch_DEMO_使用JdbcCursorItemReader

Spring Batch_DEMO_使用JdbcCursorItemReader


该demo主要完成的功能就是把数据从表people读出来处理加上people的desc属性然后保存到表ok_people。。

对应的People.java和PeopleDESC.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package  com.lyx.batch;
 
public  class  People {
     private  int  id;
     private  String lastName;
     private  String firstName;
 
     public  People() {
     }
 
     public  People(String firstName, String lastName) {
         this .firstName = firstName;
         this .lastName = lastName;
     }
 
     public  int  getId() {
         return  this .id;
     }
 
     public  void  setId( int  id) {
         this .id = id;
     }
 
     public  void  setFirstName(String firstName) {
         this .firstName = firstName;
     }
 
     public  String getFirstName() {
         return  this .firstName;
     }
 
     public  String getLastName() {
         return  this .lastName;
     }
 
     public  void  setLastName(String lastName) {
         this .lastName = lastName;
     }
 
     @Override
     public  String toString() {
         return  "firstName: "  this .firstName +  ", lastName: "  this .lastName;
     }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package  com.lyx.batch;
 
public  class  PeopleDESC {
     private  int  id;
     private  String lastName;
     private  String firstName;
     private  String desc;
 
     public  PeopleDESC(String lastName, String firstName, String desc) {
         super ();
         this .lastName = lastName;
         this .firstName = firstName;
         this .desc = desc;
     }
 
     public  int  getId() {
         return  this .id;
     }
 
     public  void  setId( int  id) {
         this .id = id;
     }
 
     public  void  setFirstName(String firstName) {
         this .firstName = firstName;
     }
 
     public  String getFirstName() {
         return  this .firstName;
     }
 
     public  String getLastName() {
         return  this .lastName;
     }
 
     public  void  setLastName(String lastName) {
         this .lastName = lastName;
     }
 
     @Override
     public  String toString() {
         return  "firstName: "  this .firstName +  ", lastName: "  this .lastName;
     }
 
     public  String getDesc() {
         return  this .desc;
     }
 
     public  void  setDesc(String desc) {
         this .desc = desc;
     }
 
}


对应的数据库表

?
1
2
3
4
5
6
7
8
9
10
11
12
13
CREATE  TABLE  ok_people  (
     person_id  BIGINT  NOT  NULL  AUTO_INCREMENT,
     first_name  VARCHAR (20),
     last_name  VARCHAR (20),
     batch_desc  varchar (255),
     PRIMARY  KEY  (person_id)
);
CREATE  TABLE  people  (
     person_id  BIGINT  NOT  NULL  AUTO_INCREMENT,
     first_name  VARCHAR (20),
     last_name  VARCHAR (20),
     PRIMARY  KEY  (person_id)
);

主要过程还是配置job的reader,processor,writer,下面是配置文件中的reader,processor,wirter,如下配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
< beans  xmlns = "http://www.springframework.org/schema/beans"
     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"  xmlns:batch = "http://www.springframework.org/schema/batch"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
         http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">
     
     < bean  id = "exceptionHandler"  class = "com.lyx.batch.ExceptionListener"  />
 
     < batch:step  id = "abstractStep"  abstract = "true" >
         < batch:listeners >
             < batch:listener  ref = "exceptionHandler"  />
         batch:listeners >
     batch:step >
     < bean  id = "abstractCursorReader"  abstract = "true"
         class = "org.springframework.batch.item.database.JdbcCursorItemReader" >
         < property  name = "dataSource"  ref = "dataSource"  />
     bean >
 
     < batch:job  id = "addPeopleDescJob" >
         < batch:step  id = "addDescStep"  parent = "abstractStep" >
             < batch:tasklet >
                 < batch:chunk  reader = "peopleAddDescReader"  processor = "addDescProcessor"
                     writer = "addDescPeopleWriter"  commit-interval = "2" />
             batch:tasklet >
         batch:step >
     batch:job >
     < bean  id = "peopleAddDescReader"  parent = "abstractCursorReader"
         scope = "step" >
         < property  name = "sql" >
             < value >
             first_name like ? or last_name like ?]]> value >
         property >
         < property  name = "rowMapper"  ref = "peopleRowMapper"  />
         < property  name = "preparedStatementSetter"  ref = "preparedStatementSetter"  />
         < property  name = "fetchSize"  value = "20"  />
     bean >
     < bean  id = "peopleRowMapper"  class = "com.lyx.batch.PeopleRowMapper"  />
     < bean  id = "preparedStatementSetter"  class = "com.lyx.batch.PeoplePreparedStatementSetter"  />
     < bean  id = "addDescProcessor"  class = "com.lyx.batch.AddPeopleDescProcessor"  />
     < bean  id = "addDescPeopleWriter"  class = "com.lyx.batch.AddDescPeopleWriter"  >
         < property  name = "dataSource"  ref = "dataSource" />
     bean >
     
    
     < bean  id = "dataSource"  class = "org.apache.tomcat.jdbc.pool.DataSource"
         destroy-method = "close" >
         < property  name = "poolProperties" >
             < bean  class = "org.apache.tomcat.jdbc.pool.PoolProperties" >
                 < property  name = "driverClassName"  value = "com.mysql.jdbc.Driver"  />
                 < property  name = "url"  value = "jdbc:mysql://localhost:3306/test"  />
                 < property  name = "username"  value = "root"  />
                 < property  name = "password"  value = "034039"  />
             bean >
         property >
     bean >
     
    
     < batch:job-repository  id = "jobRepository"
         data-source = "dataSource"  transaction-manager = "transactionManager"
         isolation-level-for-create = "REPEATABLE_READ"  table-prefix = "BATCH_"
         max-varchar-length = "1000"  />
    
     < bean  id = "transactionManager"
         class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
         < property  name = "dataSource"  ref = "dataSource"  />
     bean >
 
    
     < bean  id = "jobLauncher"
         class = "org.springframework.batch.core.launch.support.SimpleJobLauncher" >
         < property  name = "jobRepository"  ref = "jobRepository"  />
     bean >
beans >

对应的类:


PeopleRowMapper.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package  com.lyx.batch;
 
import  java.sql.ResultSet;
import  java.sql.SQLException;
 
import  org.springframework.jdbc.core.RowMapper;
 
public  class  PeopleRowMapper  implements  RowMapper {
 
     public  People mapRow(ResultSet rs,  int  rowNum)  throws  SQLException {
         People p =  new  People();
         p.setFirstName(rs.getString( "first_name" ));
         p.setLastName(rs.getString( "last_name" ));
         return  p;
     }
 
}


PeoplePreparedStatementSetter.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package  com.lyx.batch;
 
import  java.sql.PreparedStatement;
import  java.sql.SQLException;
 
import  org.springframework.jdbc.core.PreparedStatementSetter;
 
public  class  PeoplePreparedStatementSetter  implements  PreparedStatementSetter {
 
     public  void  setValues(PreparedStatement ps)  throws  SQLException {
         // TODO Auto-generated method stub
         ps.setString( 1 "%JOHN%" );
         ps.setString( 2 "%DOE%" );
     }
}


AddPeopleDescProcessor.java

?
1
2
3
4
5
6
7
8
9
10
11
12
package  com.lyx.batch;
 
import  org.springframework.batch.item.ItemProcessor;
 
public  class  AddPeopleDescProcessor  implements
         ItemProcessor {
 
     public  PeopleDESC process(People item)  throws  Exception {
         return  new  PeopleDESC(item.getLastName(), item.getFirstName(), Thread
                 .currentThread().getName());
     }
}


AddDescPeopleWriter.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package  com.lyx.batch;
 
import  java.util.List;
 
import  javax.sql.DataSource;
 
import  org.springframework.batch.item.ItemWriter;
import  org.springframework.jdbc.core.JdbcTemplate;
 
public  class  AddDescPeopleWriter  implements  ItemWriter {
 
     private  JdbcTemplate jdbcTemplate;
 
     public  void  setDataSource(DataSource dataSource) {
         this .jdbcTemplate =  new  JdbcTemplate(dataSource);
     }
 
     public  void  write(List extends  PeopleDESC> items)  throws  Exception {
         for  (PeopleDESC peopleDESC : items) {
             this .jdbcTemplate
                     .update( "insert into ok_people (first_name, last_name, batch_desc) values (?, ?, ?)" ,
                             peopleDESC.getFirstName(),
                             peopleDESC.getLastName(), peopleDESC.getDesc());
         }
     }
 
}


===================END===================

你可能感兴趣的:(java)