Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why we use MapSqlParameterSource

I am new to spring world, in above code i understand query but i don't get why "new MapSqlParameterSource("username" ,username)" is used?

  public boolean exists(String username) {      
       return jdbc.queryForObject("select count(*) from users where username=:username",
           new MapSqlParameterSource("username" ,username),Integer.class)>0;                                        }

what is the purpose of using it?

Thanks in advance

like image 201
user3826914 Avatar asked Sep 15 '25 06:09

user3826914


1 Answers

It is because some developers do not like use ? in a sql sentence. This ? is named or referred how (mostly the first):

  • Placeholders
  • Bind parameter

Therefore Spring offers the same approach, it such as how Hibernate/JPA does with :parameter, it through the MapSqlParameterSource class.

I suggest you do a research about RowMapper<T> too.

like image 168
Manuel Jordan Avatar answered Sep 17 '25 21:09

Manuel Jordan