We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
iBATIS 和 Hibernate 是完全不同的东西。
我倾向于这样看待它:如果您的视图更加以对象为中心,Hibernate 会更好地工作。然而,如果您认为更以数据库为中心,那么 iBATIS 是一个更强大的选择。
如果您完全控制模式并且没有极高的吞吐量要求,那么 Hibernate 可以很好地工作。对象模型使得代码相当方便,但复杂性成本很高。
如果您正在处理“遗留”数据库模式,需要编写相当复杂的 SQL 查询,那么 iBATIS 可能会工作得更好。
HQL(Hibernate 查询语言)是您必须学习的另一种语言,即使如此,您可能会发现您仍然需要编写 SQL。更重要的是,您可能会花半天的时间来找出 XML、属性、注释等的正确组合,以使 Hibernate 生成高性能的 SQL 查询。
对于这个问题,没有普遍的“A 比 B 更好”的答案。
iBATIS and Hibernate are quite different beasts.
The way I tend to look at it is this: Hibernate works better if your view is more object-centric. If however you view is more database-centric then iBATIS is a much stronger choice.
If you're in complete control of your schema and you don't have an extremely high throughput requirement then Hibernate can work quite well. The object model makes for fairly convenient code but at a huge complexity cost.
If you're dealing with a "legacy" database schema where you need to write fairly complicated SQL queries then chances are iBATIS will work better.
HQL (Hibernate Query Language) is another language you'll have to learn and even then you'll probably find cases where you still need to write SQL. What's more, chances are you will at some spend half a day figuring out the right combination of XML, properties, annotations, etc to get Hibernate to generate a performant SQL query.
There is no universal "A is better than B" answer for this question.
考虑一下您想要实现的目标。通常,命令查询响应隔离
模型适用于复杂领域。
原因是您通常尝试执行以下两件事之一:
Hibernate 非常适合情况 1,允许你只需制作一个 POJO 并保存/更新它。它也可以快速完成此操作,除非您的域非常大。
myBatis 非常适合您只想要答案的获取查询(情况 2)。 Hibernate 会尝试加载整个对象图,您需要开始使用 LazyLoading 技巧调整查询,以使其在大域上正常工作。相反,如果您只想要一些分析 POJO 页面,则相同查询的 myBatis 实现将是微不足道的。
因此,myBatis 在 SELECTS 上比 Hibernate 更快。
这两种情况是您想要更改域数据的命令和您只想获取一些数据的响应之间的区别。
因此,请考虑这两种情况以及您的应用程序的用途。如果您有一个简单的域并且只是获取信息,请使用 myBatis。如果您有复杂的域并保留实体,请使用 Hibernate。如果两者都做,请考虑采用混合方法。这就是我们在拥有数千个实体的项目中使用的方法来控制它。 ;)
Consider what you're trying to achieve. Typically, the Command Query Response Segregation
model works well for complex domains.
The reason is that you're trying to do one of two things typically:
Hibernate works well for case 1 allowing you to just make a POJO and persist/update it. It also does this quickly, unless your domain is quite large.
myBatis is great for fetch queries (case 2) where you just want an answer. Hibernate would attempt to load the entire object graph and you'd need to start tuning queries with LazyLoading tricks to keep it working on a large domain. Conversely if you just want some analytic POJO page, the myBatis implementation of the same query would be trivial.
Because of this, myBatis is faster than Hibernate at SELECTS.
These two cases are the difference between Commands where you want to change the domain data and Responses where you just want to fetch some data.
So, consider these two cases and what your application does. If you have a simple domain and just fetch information, use myBatis. If you have a complex domain and persist entities, use Hibernate. If you do both, consider a hybrid approach. That's what we use on our project that has thousands of entities to keep it under control. ;)
ORM 与持久性框架
Hibernate 是对象关系映射框架 (ORM),它将 Java 类映射到数据库表。 MyBatis 是持久性框架 - 不是 ORM。它将 SQL 语句映射到 Java 方法。
数据库模式
Hibernate可以根据您的Java模型创建或验证数据库模式,而MyBatis没有这样的功能。当您使用内存数据库时,它也方便测试环境。相关讨论:
缓存
Hibernate 有一级缓存,无法禁用。这意味着如果你通过ORM查询item,然后直接用SQL删除它,它会保留在缓存中。您可以显式清除缓存以从数据库获取最新的结果。相关讨论:
乐观锁管理
乐观锁管理也有区别:
相关讨论:
延迟加载
Hibernate 将尝试加载整个对象图,除了标记为延迟加载的对象之外。 myBatis 将根据 SQL 查询加载数据。延迟加载可以提高性能,但如果与以下一起使用,可能会导致连接泄漏
<属性名称=“hibernate.enable_lazy_load_no_trans”值=“true”/>
特性。相关讨论:
Hibernate 会话管理
通过 Hibernate 执行保存、更新或删除等实体操作 会话。需要很好地理解如何实施正确的 Hibernate Session 管理策略,以避免
分离实体传递到持久化
以及与 Hibernate 相关的其他现象。有时,尝试理解底层 Hibernate 行为可能比添加更多工作并为 myBatis 编写原始 SQL 语句花费更多时间。
级联
Hibernate 为对象图提供级联、孤立删除和其他功能,而 myBatis 中没有这些功能 - 要实现它们,您需要显式编写 SQL 查询。
查询
在 myBatis 中,您将编写几乎简单的 SQL 查询。 Hibernate 有多种选项来形成查询:SQL、HQL、Criteria API。有时,当条件中有许多可选字段时,可能适合使用 Criteria API。它将提供更结构化的方法来形成查询,并可能避免相关错误。
ORM vs persistence framework
Hibernate is object-relation mapping framework (ORM) which maps Java classes to database tables. MyBatis is persistence framework - not ORM. It maps SQL statements to Java methods.
Database schema
Hibernate can create or validate database schema according to your Java model while MyBatis does not have such feature. Also it is convenient for testing environment when you're using in-memory DB. Related discussions:
Cache
Hibernate has first level cache which is impossible to disable. It means that if you query item through ORM and then delete it directly with SQL, it stays in the cache. You can explicitly clear the cache to get the most updated results from database. Related discussions:
Optimistic lock management
Also there are differences for optimistic lock management:
Related discussions:
Lazy loading
Hibernate will try to load entire object graph except objects which are marked for lazy loading. myBatis will load data according a SQL query. Lazy loading may improve performance but it may cause connection leaks if it used with
<property name="hibernate.enable_lazy_load_no_trans" value="true" />
properties. Related discussions:
Hibernate Session management
Entities operations like saving, updating or deleting are performed via Hibernate Session. It requires good understanding how to implement proper Hibernate Session management strategy to avoid
detached entity passed to persist
and other phenomenons related to Hibernate.Sometimes it may take more time trying to understand underlying Hibernate behavior than add a little bit more work and write raw SQL statements for myBatis.
Cascading
Hibernate provides cascading, orphan removal and other features for object graphs while they not present in myBatis - to implement them you'll need to write SQL queries explicitly.
Queries
In myBatis you'll write almost plain SQL queries. Hibernate has multiple options to form query: SQL, HQL, Criteria API. Sometimes it may be suitable to use Criteria API when you have many optional fields in criteria. It would provide more structured approach to form query and maybe avoid related mistakes.
Cletus 很好地总结了这一比较。当您控制数据模型并且更加以对象为中心时,Hibernate 工作得很好,而当您需要与现有数据库集成并且更加以数据为中心时,iBATIS 工作得很好。
我还认为 Hibernate 有更多的学习曲线。使用 iBATIS,可以很容易地知道发生了什么,而使用 Hibernate 会发生更多“魔法”。换句话说,新手可能会发现 iBatis 更易于使用和理解。
但我并不是说你应该更喜欢 iBatis,iBatis 和 Hibernate 只是如上所述不同。
顺便说一句,如果您选择 Hibernate,也许可以考虑使用 Hibernate 注释。
Cletus did a great job at summarizing this comparison. Hibernate works well when you control the data model and is more object-centric while iBATIS works well when you need to integrate with an existing database and is more data-centric.
Also I think that Hibernate has a bit more of learning curve. With iBATIS, it's pretty easy to know what is going on while more "magic" happens with Hibernate. In other words, newbies might find iBatis easier to use and to understand.
But I'm not saying that you should prefer iBatis, iBatis and Hibernate are just different as said above.
And by the way, if you go for Hibernate, maybe consider using standardized JPA and EJB 3.0 (JSR-220) object/relational mapping annotations provided by Hibernate Annotations.
Hibernate 是一个 ORM,这意味着(在最基本的层面上)它将 java 对象的实例映射到数据库表中的实际行。一般来说,对于通过 Hibernate 检索的 pojo:对这些 pojo 的任何操作和修改都会出现在数据库中。 Hibernate会在适当的时候生成并执行相关的SQL。
Mybatis(在最基本的层面上)只是一个用于拼凑和执行存储在 xml 文件中的 SQL 的工具。它不会将 Java 对象的实例映射到数据库表中的行,而是将 Java 方法映射到 SQL 语句,因此它不是 ORM。当然,它也可以返回 pojo,但它们不依赖于任何类型的持久性上下文。
这两种工具的功能都比上面描述的要多得多,但一种是 ORM,另一种则不是。
我认为,让您选择使用哪一个的标准主要取决于您必须使用的数据库模型。
例如,想象一个庞大的庞大模式,代表某种保险模型。开发者需要找回
数据,并以符合当前业务的方式与该数据进行交互。
开发人员不断涌现,并且永远不会被期望拥有编写所有内容所需的业务知识
手动sql(Mybatis需要)。 Hibernate 就适合这样的场景。
业务分析师定义数据模型、实体、关系和交互,这是他们的专业知识。
然后,Java 开发人员使用 Hibernate 来“遍历模型”。业务开发人员可以变得非常高效
快速,无需编写复杂的容易出错的 SQL 来在非常复杂的模式上运行。
根据我的经验,Hibernate 和 Mybatis 经常在同一个项目中使用。
Hibernate 用于
以及 Mybatis 用于
Hibernate is an ORM, meaning (at its most basic level) it maps instances of java objects to actual rows in a database table. Generally, for pojo's retrieved via Hibernate: any manipulations and modifications to these pojo's will appear in the database. Hibernate will generate and execute the relevant SQL at an appropriate time.
Mybatis (at its most basic level) is simply a tool for piecing together and executing SQL that is stored in xml files. It does not map instances of Java objects to rows in a database table, rather it maps Java methods to SQL statements, and therefore it is not an ORM. It can also return pojo's of course, but they are not tied to any kind of a persistence context.
Both tools do a lot more than described above, but one is an ORM and one is not.
The criteria to enable you to choose which one to use, I believe, depends critically on the database model you have to work with.
For example imagine a large sprawling schema, representing some insurance model. Developers are required to retrieve
data, and interact with that data in a way that meets the business at hand.
Developer's come on go, and would never be expected to have the requisite business knowledge to write all
the sql by hand (which Mybatis would require). Hibernate would suit a scenario like that.
Business analysts define the datamodel, the entities, the relationships and the interactions, as is their expertise.
Java developer's then use Hibernate to "walk the model". The business developer's can become very productive
quickly without the need to write complicated error prone sql to run on a very complicated schema.
In my expierence, both Hibernate and Mybatis are used regularly on the same project.
Where Hibernate is being used for
and where Mybatis is being used for
如果您已经在使用 Spring,我会从 Spring JDBC 开始,而不是直接投入 Hibernate 或 iBatis。如果您根据接口编写持久层,那么在掌握 Hibernate 或 iBatis 后切换实现应该没有问题。
没有理由必须做出“全有或全无”的决定。使用最适合您情况的方法。
if you're already using Spring, I would start with Spring JDBC rather than plunging right into Hibernate or iBatis. If you write your persistence tier in terms of interfaces, you should have no problem switching implementations after you've gotten Hibernate or iBatis under your belt.
There's no reason why it has to be an "all or none" decision. Use what's best for your situation.