// 1:读数据 @Bean public ItemReader<Person> reader(DataSource dataSource) { JdbcCursorItemReader<Person> reader = new JdbcCursorItemReader<>(); reader.setDataSource(dataSource); reader.setSql("select * from person limit 1"); reader.setRowMapper( new RowMapper<Person>() { @Override public Person mapRow(ResultSet rs, int rowNum) throws SQLException { Person p = new Person(); p.setPersonId(rs.getLong("personId")); p.setPersonName(rs.getString("personName")); p.setPersonAge(rs.getInt("personAge")); p.setPersonSex(rs.getString("personSex")); return p; } }); return reader; }
@Transactional @Test public void testRead() throws Exception { itemReader.open(new ExecutionContext()); Foo foo = itemReader.read(); assertEquals(2, foo.getId()); foo = itemReader.read(); assertEquals(3, foo.getId()); assertNull(itemReader.read()); }
@Before public void onSetUpInTransaction() throws Exception { itemReader = new JdbcCursorItemReader<Foo>(); itemReader.setDataSource(dataSource); itemReader.setSql("select ID, NAME, VALUE from T_FOOS where ID > ? and ID < ?"); itemReader.setIgnoreWarnings(true); itemReader.setVerifyCursorPosition(true); itemReader.setRowMapper(new FooRowMapper()); itemReader.setFetchSize(10); itemReader.setMaxRows(100); itemReader.setQueryTimeout(1000); itemReader.setSaveState(true); ListPreparedStatementSetter pss = new ListPreparedStatementSetter(); List<Long> parameters = new ArrayList<Long>(); parameters.add(1L); parameters.add(4L); pss.setParameters(parameters); itemReader.setPreparedStatementSetter(pss); }