为什么我们使用 MapSqlParameterSource [英] why we use MapSqlParameterSource

查看:32
本文介绍了为什么我们使用 MapSqlParameterSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 spring 世界的新手,在上面的代码中我理解查询,但我不明白为什么"new MapSqlParameterSource("username" ,username)" 使用了吗?

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;                                        }

使用它的目的是什么?

提前致谢

推荐答案

这是因为有些开发者不喜欢在 sql 语句中使用 ?.这个 ? 是如何命名或引用的(主要是第一个):

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

  • 占位符
  • 绑定参数

因此 Spring 提供了相同的方法,例如 Hibernate/JPA 如何使用 :parameter,它通过 MapSqlParameterSource 类.

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

我建议你也研究一下 RowMapper.

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

这篇关于为什么我们使用 MapSqlParameterSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆