W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

2/14/2020

what is different between MapSqlParameterSource and BeanPropertySqlParameterSource

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:

  1. public class Employee {
  2. private int id;  
  3. private String name;  
  4. private int salary;
  5. //getter setter....
  6. }

2: For example, we need to update the employee,  as follows:

  1. @Test
  2. public void save(){
  3. Employee emp = new Employee();
  4. emp.setId(4);
  5. emp.setName("Jack");
  6. emp.setSalary(500);
  7. int num = empDao.updateEmployee(emp);
  8. System.out.println(num);
  9. }

3: We should implement the updateEmployee above method, as follows:

  1. public int updateEmployee(Employee e) {
  2. String sqlStr="update employee set name=:name,salary=:salary where id=:id"; 
  3. MapSqlParameterSource map = new MapSqlParameterSource();
  4. map.addValue("name", e.getName());
  5. map.addValue("salary", e.getSalary());
  6. map.addValue("id", e.getId());
  7. return this.update(sqlStr, map);
  8. }

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:

  1. public int updateEmployee(Employee emp) {
  2. String sqlStr="update employee set name=:name,salary=:salary where id=:id"; 
  3.         BeanPropertySqlParameterSource beanParam = new BeanPropertySqlParameterSource(emp);
  4. return this.update(sqlStr, beanParam);
  5. }

So, the updateEmployee method becomes easier.

But, if these Named Parameters is different from the Employee class attribute, as follows:

  1. String sqlStr="update employee set name=:myName,salary=:mySalary where id=:empId";

In this case, we have to use the BeanPropertySqlParameterSource object, or not we will get some errors.

As follows:

  1. public int updateEmployee(Employee e) {
  2. String sqlStr="update employee set name=:myName,salary=:mySalary where id=:empId"; 
  3. MapSqlParameterSource map = new MapSqlParameterSource();
  4. map.addValue("myName", e.getName());
  5. map.addValue("mySalary", e.getSalary());
  6. map.addValue("empId", e.getId());
  7. return this.update(sqlStr, map);
  8. }

Thanks!

No comments:

Post a Comment

Note: only a member of this blog may post a comment.