springbatch的使用步骤

第一步 创建JOBBUILDERFACTORY
第二部 创建STEPBUILDERFACTORY
第三部New jobbuilderfactory.get(”任务名字”).incrementer(new RunIdIncrementer()).start(itemReaderDemoStep()).build();
第四部NEW StepBuilderfactory.get(“step名字”).chunk(2).reader(fileReader()).processer(myprocesser).writer(flatFieldWriter).build();

MyReader的创建
1,实现itemsreader
2,设置reader的返回值类型,决定reader的读取类型是什么类型的
3,设置reader的参数
4,对reader做映射配置,将读取的内容与JAVA类做映射

MyWriter的创建
1,实现itemswriter
2,对读取的内容做处理
writer的关于数据库写入的写法

@Autowired
	private DataSource datasource;

	@Bean
	public JdbcBatchItemWriter itemWriterDb() {
		JdbcBatchItemWriter writer = new JdbcBatchItemWriter();
		writer.setDataSource(datasource);
		writer.setSql(
				"insert into customer(id,firstName,lastName,birthday) values" + "(:id,:firstName,:lastName,:birthday)");
		writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider());
		return writer;

	}

reader的关于数据库的读法

@Bean
	@StepScope
	public JdbcPagingItemReader dbJdbcReader() {
		JdbcPagingItemReader reader=new JdbcPagingItemReader();
		reader.setDataSource(datasource);
		reader.setFetchSize(2);
		reader.setRowMapper(new RowMapper() {

			@Override
			public User mapRow(ResultSet rs, int rowNum) throws SQLException {
				User user=new User();
				user.setId(rs.getInt(1));
				user.setUsername(rs.getString(2));
				user.setPassword(rs.getString(5));
				return user;
			}
		});
		MySqlPagingQueryProvider provider=new MySqlPagingQueryProvider();
		provider.setSelectClause("id,username,password");
		provider.setFromClause("from user");
		Map sort=new HashMap<>();
		sort.put("id",Order.ASCENDING);
		provider.setSortKeys(sort);
		reader.setQueryProvider(provider);
		return reader;

你可能感兴趣的:(沈阳第一个项目)