I have data from table A
like this:
ID Name Subject 1 AAA music;history;math 2 BBB music 3 CCC math;history
and I have used ItemReader
to get the list from this table:
public StoredProcedureItemReader<Student> getList() {
StoredProcedureItemReader<Student> reader = new StoredProcedureItemReader<Student>();
SqlParameter[] parameters = {
new SqlInOutParameter("v_cursor", Types.REF_CURSOR)
};
reader.setDataSource(dataSource);
reader.setProcedureName("package.GET_STUDENTLIST");
reader.setRowMapper(new BeanPropertyRowMapper<>(Student.class));
reader.setParameters(parameters);
reader.setRefCursorPosition(1);
return reader;
}
Because of some reasons, I can't change the Stored Procedure and it will return all items in column Subject (that means the list will have 3 objects). How could I modify the StoredProcedureItemReader
to return a separative item of the Subject column?
My expect is that the list should have 6 objects:
ID Name Subject 1 AAA music 1 AAA history 1 AAA math 2 BBB music 3 CCC math 3 CCC history
You can try to implement your custom RowMapper
like below:
StoredProcedureItemReader<List<Student>> reader = new StoredProcedureItemReader<List<Student>>();
SqlParameter[] parameters = {
new SqlInOutParameter("v_cursor", Types.REF_CURSOR)
};
reader.setDataSource(dataSource);
reader.setProcedureName("package.GET_STUDENTLIST");
reader.setRowMapper(new RowMapper<List<Student>>(){
@Override
public List<Student> mapRow(ResultSet rs, int rownumber) throws SQLException {
String[] sub = rs.getString(3).split(";");
List<Student> temp = new ArrayList<Student>();
for(int i =0 ; i < sub.length ; i ++){
Student s =new Student();
s.setId(rs.getInt(1));
s.setName(rs.getString(2));
s.setSubject(sub[i]);
temp.add(s);
}
return temp;
}
});
reader.setParameters(parameters);
reader.setRefCursorPosition(1);
return reader;
Please note I have also updated ItemReader
class to expect List<Student>
instead of Student
class and yes you can do changes accordingly to avoid run time exceptions like NullPointerException
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.