对于使用 Spring 和 JDBCTemplates、iBatis/myBatis 或 Hibernate 的新项目?
您好,我们正在启动一个项目,在该项目中我们必须在持久层使用 Spring JDBCTemplates、iBatis/myBatis 还是 Hibernate 之间做出决定。我或多或少熟悉他们两个的概念,但我想知道人们目前倾向于使用什么。
我的要求是:
- 使一切尽可能简单
- 易于学习和使用
- 高性能
- 最佳开发人员生产力
- 必须可与 Spring 3 一起使用
如前所述,我们希望使一切尽可能简单。我的偏好倾向于 iBatis/myBatis,因为它看起来更容易使用,而且我们不需要真正的 OR Mapper。但我真的很期待向使用这些框架的人学习。
Hi there we are starting a project in which we have to make the decision between using Spring JDBCTemplates, iBatis/myBatis or Hibernate for our persistence layer. I am more or less familiar with the concepts from both of them but i am wondering what people currently tend to use.
My requirements are:
- keep everything as simple as possible
- easy learn and use
- high performance
- optimal developer productivity
- must be usable with Spring 3
As noted we would like to keep everything as simple as possible. My preference tends to lean toward iBatis/myBatis because it looks easier to use and we do not need a real OR Mapper. But i am really looking forward to learn from the guys that use these frameworks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我建议您查看 Minuteproject spring/hibernate/ibatis 以及 JPA(2) 的逆向工程解决方案,因为它满足您的开发要求。它还没有生成一件事,那就是 jdbctemplate。
在选择技术之前,我将回顾您所有适合分钟项目方法的观点。
它提供了高级 dao 层以及您只需参考的调整查询。
,但最能判断的是你,所以在你的模型上尝试一下。要快速概览启动 /bin/start-console.(cmd/sh) 的可能性,请指向您的数据库并选择生成轨道。如果您的数据库是(mysql、db2、oracle、hsqldb 有默认值,例如 hibernatedialect 预设),通常需要几分钟。
要更高级地使用配置(它适用于所有数据库)。
至于使用哪种技术,我个人对所有这些技术都有生产经验,但我认为像 hibernate 这样的 orm 框架的双向方面比单向 sqlmap 更有优势。它保存配置并且图形导航直观。
一个好东西:转义特殊字符,例如 ', 包含在 orm 框架中。这是您在 sqlmap 中执行本机 sql 时经常遇到的问题,例如插入姓氏 = 'o'hara'...
我会选择 Hibernate(在您提到的选择中),但选择 JPA2(如果您已包含它)。如果您想要真正的额外生产力,请集成 querydsl 以获取其中的可编译条件 API。
I advise you to have a look at minuteproject reverse-engineering solution for spring/hibernate/ibatis and also JPA(2), since it fulfill your development requirement. There is one thing that it does not generate (yet) is jdbctemplate.
Before opting for a technology, I will review all your points that fit in the minuteproject approach.
It provides and avanced dao layer with tuned query that you just have to reference.
But the best one that can judge is you, so try it on you model. To have a quick overview of the possibility start /bin/start-console.(cmd/sh), point to your database and chose a generation track. It should normally take a few minutes if your database is (mysql, db2, oracle, hsqldb have default value such as hibernatedialect preset).
To go more advance use the configuration (it is for all db).
Regarding to which technology to use, personally I have production experience with all of them, but I would consider that the bidirectional aspect of orm frameworks such as hibernate is a strong point over unidirectional sqlmap. It saves configuration and graph navigation is intuitive.
A goody: Escaping special character such as ', is including in orm frameworks. And this is a problem you would commonly face while performing native sql in sqlmap such an insert of a lastname = 'o'hara'...
I would go for Hibernate (among the choice you mention), but go for JPA2 (if you would have included it). If you want real extra productivity integrate querydsl for compilable criteria API in it.
我曾使用过 Ibatis 和 Hibernate。 Ibatis 简单明了。如果您不小心,Hibernate 可能会变得很复杂,但它对您有很大帮助。 spring-jdbc 比原始 JDBC 更好。
Hibernate最大的优势是能够映射到不同的数据库。您甚至可以关闭模式前缀。您可以选择使用内存数据库进行测试,或者让开发人员使用与生产目标不同的本地数据库(例如,如果您的目标是 Oracle 并且许可证是一个问题),或者能够针对多个数据库。使用 Hibernate 可以轻松更换 id 生成器。对于 Hibernate,本机 SQL 是一种选择,但对于 Ibatis,则别无选择。
此外,保持 Ibatis 映射文件干燥也是很难甚至不可能的。如果您有多个具有不同 where 子句的查询,则会导致剪切粘贴。使用 Hibernate 就没有那么多的重复。
顺便说一下,Ibatis 和 Hibernate 都有声明式缓存机制。当然,Hibernate 的参与要复杂得多。
spring-jdbc 具有我为 Ibatis 列出的所有缺点。另外我不认为它有缓存机制。它的主要好处是 JDBC 对象隐藏得不太好,因此您可以在需要时更轻松地直接访问它们。
Spring 与所有三种替代方案集成,Spring 支持并不是区别因素。
还有一件事:Hibernate 与人工键配合得很好。它可以管理复合业务密钥,但工作量要大得多。 Ibatis 和 spring-jdbc 还不够复杂,这个问题对它们来说并不重要。
如果您的开发人员谨慎而彻底,并且您可以保持方法简单(例如,使用每个请求会话,不保留从一个会话到下一个会话的任何对象,以及使用人工密钥),那么请使用 Hibernate。如果您决定不需要 Hibernate 提供的数据库抽象,或者您不信任开发人员拥有锋利的工具,那么请选择 Ibatis。请记住 spring-jdbc 作为执行需要裸机 jdbc 调整的特定查询的后备。
顺便说一句,Grails 和 GORM 使 Hibernate 更容易实验,因为设置时间少得多,Grails 从内存数据库开始,您无需编写映射文件即可完成。
I've worked with Ibatis and Hibernate. Ibatis is straightforward and simple. Hibernate can get complicated if you're not careful, but it does a lot for you. spring-jdbc is better than raw JDBC.
Hibernate's biggest advantage is being able to map to different databases. You can even turn off schema prefixes. You have options like using an in-memory database for testing, or having developers use local databases different from the production target (for instance if you're targeting Oracle and licenses are a problem), or being able to target multiple databases. Swapping out id generators is easy with Hibernate. With Hibernate native SQL is an option, but with Ibatis there is no choice.
Also it is hard to impossible to keep the Ibatis mapping files DRY. If you have multiple queries with different where clauses, cut-n-paste will result. With Hibernate there is nowhere near as much duplication.
Both Ibatis and Hibernate have a declarative caching mechanism, by the way. Hibernate's is much more involved, of course.
spring-jdbc shares all the disadvantages I've listed for Ibatis. In addition I don't think it has a caching mechanism. its main benefit is that the JDBC objects are not as well-hidden, so you can get direct access to them more easily if you need it.
Spring integrates with all three alternatives, spring support is not a differentiator.
One more thing: Hibernate works great with artificial keys. It can manage composite business keys but it's much more work. Ibatis and spring-jdbc are not sophisticated enough for this issue to matter for them.
If your developers are cautious and thorough, and if you can keep your approach simple (for instance, using session-per-request, not retaining any objects from one session to the next, and using artificial keys), then go with Hibernate. If you've decided you don't need the database abstraction that Hibernate provides, or you don't trust your developers with sharp tools, then go with Ibatis. Keep spring-jdbc in mind as a fall-back to do particular queries that need bare-metal jdbc tweaking.
By the way, Grails and GORM make Hibernate much easier to experiment with because there's so much less setup time, Grails starts you out with an in-memory database and you can get by without writing mapping files.
除了 ORM 之外,Hibernate 还为您提供了很好且简单的方法来映射本机 SQL 查询,而 iBatis 仅提供 SQL 映射。因此,使用 Hibernate,您有更多选择,并且可以在需要时使用 ORM 或本机 SQL。因此我更喜欢 Hibernate。
Hibernate 确实与 Spring 集成得很好。 iBatis 可能也是如此,但我从未使用过这样的集成。
这是我的两分钱。如果有人有不同意见,请发布另一个答案。
Hibernate offers you nice and easy way to map native SQL queries in addition to ORM while iBatis offers only SQL mapping. So with Hibernate you have more options and can use ORM or native SQL when needed. For that reason I prefer Hibernate.
Hibernate definitelly integrates well with Spring. iBatis probably does as well but I never used such integration.
These are my two cents. If someone has different opinion, please post another answer.
为了这两个目标:
- 让一切尽可能简单
- 易于学习和使用
MyBatis 和 JdbcTemplate 都可以正常工作。它们都只提供高级 API 来使用 SQL 访问数据库。
您将使用 MyBatis 保存代码,因为它将能够完成大部分映射(从结果集到对象),并且如果您使用映射器(将方法映射到 sql 语句的动态接口),您将保存对象。
另一方面,您可能会使用 JdbcTemplate 获得最佳性能,因为手动编码映射比内省更快。
关于“最佳开发人员生产力”。 Hibernate 比 MyBatis/JdbcTemplate 生产力更高,但它需要更多的技能和经验,否则您的生产力可能会非常低。
所有这些都与 Spring 3 集成得非常好
For these two objectives:
- keep everything as simple as possible
- easy learn and use
Both MyBatis and JdbcTemplate will work fine. Both them just provide a high level API to access your database with SQL.
You will save code with MyBatis because it will be able to do most of the mappings (from result sets to objects) and you will save objects if you use mappers (dynamic interfaces that map methods to sql statements).
On the other side you will probably get the best performance with JdbcTemplate because hand coded mappings are faster than introspection.
Regarding "optimal developer productivity". Hibernate is more productive than MyBatis/JdbcTemplate but it requires more skills and experience otherwise your productivity can go really low.
All them are really well integrated with Spring 3
由于没有表示层,而且听起来大部分业务规则都在持久层之外,我的建议是 Spring 的 JDBCTemplate。虽然我之前使用过 ibatis,但我对 Hibernate 和 Spring 的体验远远超过了我对 ibatis 的体验,所以我提供以下建议的理由。
休眠
-虽然 hibernate 是一个优秀的持久性框架但需要有经验的人才能充分利用 hibernate。一些开发人员将 Hibernate 视为黑匣子,他们对 HQL 不小心,导致严重的性能问题。由于您正在寻找一个易于学习和理解的持久层 - 我会避免这种情况。 但是,如果您不使用 hibernate,您就会失去 hibernate 提供的开箱即用的简单缓存系统。在你的图形尖端没有缓存系统很糟糕,所以我会考虑将一个缓存系统集成到性能很重要的任何其他解决方案中。
春天
-Spring 的 JDBCTemplates 对于大多数人来说应该很容易理解。它不是一项新技术或框架,Spring 的目标只是让事情变得更容易使用。由于您的要求之一是 Spring3,我假设你们团队中的大多数人已经熟悉依赖注入、行映射器和 DTO 的概念?无论您选择哪种解决方案,我都会利用Spring 的事务测试工具。最后,使用 JDBCTemplates 调试性能问题比使用 hibernate 容易得多。
iBatis
- 由于我对 iBatis 的经验很少,如果您的团队对该工具有积极的经验,我只会建议您使用 JDBCTemplates。与其他两个解决方案相比,它是一个不太受欢迎的解决方案,但它看起来确实是一个不错的中间立场。
Since there is no presentation layer and it sounds like the majority of the business rules will be out side of the persistence layer my suggestion would be Spring's JDBCTemplate. While I have used ibatis before my experiences with Hibernate and Spring far exceed my experiences with ibatis so I provide the following reasons for my suggestion.
Hibernate
-While hibernate is an excellent framework for persistence it takes someone with experience to get the most out of hibernate. Some developers treat hibernate as a black box and they get careless with their HQL and cause massive performance issues. Since your looking for an easy to pick up and understand persistence layer - I would avoid this. But, if you don't use hibernate you loose out on the simple caching system that hibernate provides out of the box. Not having a caching system at your figure tips stinks so I would consider integrating one in any other solution where performance is important.
Spring
-Spring's JDBCTemplates should be pretty easy for most anyone to understand. It's not a new technology or framework and Spring's goal is to just make things easier to use. Since one of your requirements is the Spring3 I would assume that most of you team is already familiar with the idea of dependency injection, row mappers, and DTOs? Whatever solution you pick I would also take advantage of Spring's transactional testing tools. Lastly, debugging performance issues with JDBCTemplates are a lot easier than with hibernate.
iBatis
-Since my experience with iBatis is little I would only suggest this over JDBCTemplates if your team has had positive experience with this tool. It is a somewhat less popular solution to the other two but it does seem like a nice middle ground.