Java NamedParameterJdbcTemplate 与 JdbcTemplate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16359316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NamedParameterJdbcTemplate vs JdbcTemplate
提问by Ashok
I'm a beginner to Spring3.x , I'm learning Spring DAO support. I want to know the difference between NamedParameterJdbcTemplate and JdbcTemplate. Which one is the best by means of performance. And when to go for NamedParameterJdbcTemplate and when to go for JdbcTemplate. Your answer will help a lot to the beginners like me.
我是 Spring3.x 的初学者,我正在学习 Spring DAO 支持。我想知道 NamedParameterJdbcTemplate 和 JdbcTemplate 之间的区别。就性能而言,哪一个是最好的。以及何时使用 NamedParameterJdbcTemplate 以及何时使用 JdbcTemplate。您的回答对像我这样的初学者有很大帮助。
回答by Xenson
There's no measurable difference performance. The NamedParameterJdbcTemplate is a convenience that allows you to use named parameters. If you're really curious take a look at the source code which is readily available for download. I find that reading the source code gives me more confidence in the answers I get on the forums.
没有可测量的性能差异。NamedParameterJdbcTemplate 是一种方便,允许您使用命名参数。如果您真的很好奇,请查看可随时下载的源代码。我发现阅读源代码让我对在论坛上得到的答案更有信心。
回答by Nathan Hughes
When you use JdbcTemplate you give it SQL that has a ?
placeholder for each parameter you want substituted into the SQL. When you assign parameters in the code you have to pass in arguments in an array and they get used in the order in which they appear in the array, like this:
当你使用 JdbcTemplate 时,你给它的 SQL 有一个?
占位符,用于你想要替换到 SQL 中的每个参数。当您在代码中分配参数时,您必须在数组中传递参数,并按照它们在数组中出现的顺序使用它们,如下所示:
Object[] args = new Object[] {"x", "y"};
String sql = "select * from foo where a = ? and b = ?";
jdbcTemplate.query(sql, args, resultSetExtractor);
so the SQL that gets run is select * from foo where a = 'x' and b = 'y'
.
所以运行的 SQL 是select * from foo where a = 'x' and b = 'y'
.
NamedParameterJdbcTemplate allows you to assign names to the parameter placeholders and pass in a map so the template can match the map names to the placeholders. So your code would look like:
NamedParameterJdbcTemplate 允许您为参数占位符分配名称并传入地图,以便模板可以将地图名称与占位符匹配。所以你的代码看起来像:
String sql = "select * from foo where a = :mya and b = :myb";
Map<String, Object> argMap = new HashMap<String, Object>();
argMap.put("mya", "x");
argMap.put("myb", "y");
namedParameterJdbcTemplate.query(sql, argMap, resultSetExtractor);
generating the same SQL as the first example.
生成与第一个示例相同的 SQL。
Running the query is the time-intensive part, the performance of the argument insertion is a non-issue.
运行查询是耗时的部分,参数插入的性能不是问题。
The idea is that matching the arguments by name is less error-prone than having to specify them in a particular order. In real-life applications I've worked on, typically we store the SQL in a separate file from the DAO code, and it may be easy to accidentally get the parameters in the wrong order.
这个想法是通过名称匹配参数比必须以特定顺序指定它们更不容易出错。在我处理过的实际应用程序中,通常我们将 SQL 存储在与 DAO 代码不同的文件中,并且很容易意外地以错误的顺序获取参数。