We often use the two of object in Spring JDBCTemplate: MapSqlParameterSource and BeanPropertySqlParameterSource, but maybe we don't know what different between MapSqlParameterSource and BeanPropertySqlParameterSource is.
MapSqlParameterSource:
It's used as an argument in jdbcTemplate(sql), like java Map. if the Named Parameter isn't the same as the class attribute, we often use the MapSqlParameterSource.
BeanPropertySqlParameterSource:
It is also used as an argument in jdbcTemplate, but if the Named Parameter is the same as the class attribute, we should use it.
I will give a example, you will know the different between them.
1: First, we need to create an Employee class, as follows:
2: For example, we need to update the employee, as follows:
3: We should implement the updateEmployee above method, as follows:
Note:
It's easy to see, the three Named Parameters(:name,:salary,:id) is the same as the attribute of Employee class.
it is very complicated if we use the MapSqlParameterSource Object as the argument of the update method. So, we can change the implementationof the updateEmployee method.
As follows:
So, the updateEmployee method becomes easier.
But, if these Named Parameters is different from the Employee class attribute, as follows:
In this case, we have to use the BeanPropertySqlParameterSource object, or not we will get some errors.
As follows:
Thanks!
No comments:
Post a Comment
Note: only a member of this blog may post a comment.