Here is another approach that I have taken. I added the ability for the user to set the number of parameters that they will provide when called. This will be the first n number of positional parameters. Any remaining options available in stored-proc must be set using database default processing. This allows you to add new parameters to the end of the list with default values ββor to be zero, without breaking code that you donβt know to provide a value.
I subclassed SimpleJdbcCall and added methods to set "maxParamCount". I also used a bit of malicious reflection to set my subclassified version of CallMetaDataContext.
public class MySimpleJdbcCall extends SimpleJdbcCall { private final MyCallMetaDataContext callMetaDataContext = new MyCallMetaDataContext(); public MySimpleJdbcCall(DataSource dataSource) { this(new JdbcTemplate(dataSource)); } public MySimpleJdbcCall(JdbcTemplate jdbcTemplate) { super(jdbcTemplate); try {
In my CallMetaDataContext subclass, I store maxInParamCount and use it to trim the list of parameters that are known to exist in-proc memory.
public class MyCallMetaDataContext extends CallMetaDataContext { private int maxParamCount = Integer.MAX_VALUE; public int getMaxParamCount() { return maxParamCount; } public void setMaxParamCount(int maxInParamCount) { this.maxParamCount = maxInParamCount; } @Override protected List<SqlParameter> reconcileParameters(List<SqlParameter> parameters) { List<SqlParameter> limittedParams = new ArrayList<>(); int paramCount = 0; for(SqlParameter param : super.reconcileParameters(parameters)) { if (!param.isResultsParameter()) { paramCount++; if (paramCount > this.maxParamCount) continue; } limittedParams.add(param); } return limittedParams; } }
Usage is basically the same, with the exception of choosing the maximum number of parameters.
SimpleJdbcCall call = new MySimpleJdbcCall(jdbcTemplate) .withMaxParamCount(3) .withProcedureName("MayProc");
LITTLE RENT: It's funny that Spring is well known for its IOC container. But within his utility classes, I have to resort to reflection to provide an alternative implementation of the dependent class.
Gary johnson
source share