Spring JdbcTemplate比Spring Data Jpa快吗?

Art*_*gon 0 performance spring jdbctemplate spring-data-jpa

客户迫使我们使用JdbcTemplate而不是Spring Data Jpa来开发Spring项目。但是,该应用程序在响应速度和传递方面并不关键(它是客户端最终用户的内部Web应用程序)。我们想使用Spring Data Jpa。

问题:是否有一些客观的理由使用JdbcTemplate是因为应用程序的速度快?从我的角度来看,会有不同的瓶颈。

Jen*_*der 6

没有指定确切的用例就不可能回答更快的问题。

JdbcTemplate 在谈论纯查询执行时,它将很有可能会更快,因为JPA实现会做更多的事情:

  • 解析JPQL(假设您正在使用它)
  • 从中创建一个SQL查询
  • 执行它
  • 将结果转换为对象

尽管模板(几乎)只是:

  • 执行查询
  • 将结果作为致电ResultSetMapper或类似结果交给您。

当然,JPA这样做是有原因的。

  • 它提供了相当数量的数据库独立性。
  • it tracks your changes and generates the right update statements for persisting them.
  • it allows for lazy loading so you don't have to think about what to load before hand (of course you still have to do that if you care about performance).

And those things have costs beyond performance.

The abstraction JPA offers is actually really complex and leaky and it is not properly understood by most developers using it. While I think it is completely reasonable to use JPA in many contexts, I can also relate to people banning it from their projects. Talking about performance is absolutely too limited in order to make a well-educated decision on this.