How to combine 2 MapSqlParameterSource in Spring boot Java?
Not Working:
private MapSqlParameterSource mapFn1(Object A) {
MapSqlParameterSource mapSql = new MapSqlParameterSource();
mapSql.addValue("name", A.getName);
mapSql.addValue("age", A.getAge);
retrun mapSql;
}
private MapSqlParameterSource mapFn2(Object B) {
MapSqlParameterSource mapSql = new MapSqlParameterSource();
mapSql.addValue("education", B.getEducation);
mapSql.addValue("address", B.getAddress);
retrun mapSql;
}
private MapSqlParameterSource getMapFn1Fn2(Object A, Object B) {
MapSqlParameterSource mapSql = new MapSqlParameterSource();
mapSql = mapFn1(A);
mapSql = mapFn2(B);
return mapSql;
}
Solution 1:
private MapSqlParameterSource mapFn1(Object A, MapSqlParameterSource mapSql) {
mapSql.addValue("name", A.getName);
mapSql.addValue("age", A.getAge);
retrun mapSql;
}
private MapSqlParameterSource mapFn2(Object B, MapSqlParameterSource mapSql) {
mapSql.addValue("education", B.getEducation);
mapSql.addValue("address", B.getAddress);
retrun mapSql;
}
private MapSqlParameterSource getMapFn1Fn2(Object A, Object B) {
MapSqlParameterSource mapSql = new MapSqlParameterSource();
mapFn1(A, mapSql);
mapFn2(B, mapSql);
return mapSql;
}
Solution 2:
private MapSqlParameterSource mapFn1(Object A, Object B) {
if (A != null) {
mapSql.addValue("name", A.getName);
mapSql.addValue("age", A.getAge);
}
if (B != null) {
mapSql.addValue("education", B.getEducation);
mapSql.addValue("address", B.getAddress);
retrun mapSql;
}
}
private MapSqlParameterSource getMapFn1Fn2(Object A, Object B) {
MapSqlParameterSource mapSql = new MapSqlParameterSource();
mapFn1(A, B);
return mapSql;
}
private MapSqlParameterSource getMapFn1Fn2(Object A) {
MapSqlParameterSource mapSql = new MapSqlParameterSource();
mapFn1(A, null);
return mapSql;
}
Both solution 1 and 2 are working as expected but not sure whether its a recommended way to implement. Please suggest any better solution or recommend Solution 1 or 2 is better.