Comprehensive comparison of hibernate, mybatis, beetlsql

original
2016/02/19 11:42
Reading amount 2.9W

This is my comprehensive score. There are 12 items in total. Each item has a maximum score of 5 points and a minimum score of 0 points. Note that the evaluation only includes the standard functions provided by these software, and does not include the functions provided by a third party, such as code generation Etc.

 

Development efficiency

hibernate The database metadata can be obtained, so simple sql operations, such as insert, update, delete, and page turn, can be automatically completed. However, because of the complex concept, developers often can write SQL and Java, but cannot write hibernate (relational configuration or hql), which reduces the development efficiency.

 

 

 

 session.save(user); String hql= "from bank.Person person where person.id.country = 'AU' " Query query = sessin.createQuery(hql);



mybatis You have to write sql manually, which is inefficient. Only third-party tools that can generate sql automatically can barely improve the development efficiency

beetlsq L It also has the hibernate function, but does not require configuration or annotation. Therefore, it is believed that the development efficiency is higher than that of hibernate, because the use of beetlsql itself is not easy to make mistakes. Unlike hibernate, the concept of beetlsql is complex and easy to make mistakes, which leads to the reduction of actual development speed When writing SQL files, MD+beetl is also much faster than Myabtis's xml  

 

 sqlManager.save(user); List<User> list = sqlManager.select("user.queryMaxAgeUser",User.class,paras); //Or UserDao is an interface defined by developers, which implements DAO through proxy UserDao dao = sqlManager.getMapper(UserDao.class); List<User> list  dao.queryMaxAgeUser(paras)

 

 

Across multiple databases

hibernate It supports almost all databases, and completes cross database operations through automatically generated sql (insert, delete, page turn, etc.) and hql. However, hibernate is unable to handle the differences between databases, so only 4 points
mybatis Almost no cross database function has been considered. The configuration file can be judged by the if tag, but this technology is weak.
beetlsql , currently supports Oracle, Mysql,Postgres,SQLServer,SQLLite,H2。 It also provides cross database functions such as automatic generation of sql to complete insert, delete, and page turning. Beetl script can complete some cross database operations. In addition, beetlsql manages sql files independently. sql is placed in the root directory, and specific database sql is placed in a subdirectory named after the database. If the database SQL does have one thing, you can overwrite the default SQL with the specific database SQL.

 

 

 

 

Maintainability

hibernate : The native complexity of hibernate makes maintenance difficult. For some simple refactorings, the hibernate code does not need to be changed. For complex SQL (hql) changes, hibernate is laborious
mybatis : It can adapt to complex SQL changes, but the SQL in the mapper configuration file must be modified for table reconfiguration.
beetlsql : It has the advantages of both hibernate and myabtis in terms of maintainability. Support refactoring and SQL maintenance out of code. The md format of beetlsql makes maintaining sql like reading documents. Like Myabtis, it supports automatic mapping of SQL statements in SQL files to dao methods.

 

 

 

 QQ screenshot 20160217182303.png

 

 

System startup time

hibernate : Start the metadata that needs to load the database, load the pojo, load the configuration file, and then verify. It takes a long time to start
mybatis : You need to load a basic configuration file and various mapper files. The system will be slow when it is first accessed
beetlsql : The metadata of the database is simply loaded without any verification, and the startup almost takes no time

 

 

 

 

Error prompt

hibernate Because of the complexity of ORMapping, the prompt is also very complex. In addition, whether it is xml configuration or annotation configuration, it is difficult to understand the meaning of the error prompt if there is an error prompt. HQL is the same, and the error prompt is not ideal
mybatis : Myabtis error prompt is the most difficult to understand, because its xml based parsing and ognl expression are not his own, so we will see all kinds of inexplicable prompts.

As follows, it should have been a resultType, but it was written as a resultMap

 

 

 

 <select id="getGrantCount" resultMap="Integer"  parameterType="Map">


Although the error prompt is understandable, it is not intuitive and doesn't tell you which line is wrong. You can only check the manual error in all parts of the configuration file

 

 Caused by: java.lang.IllegalArgumentException: Result Maps collection does not contain value for cn.xxx. IGiftStatisticCacheService.Integer



beetlsql : Based on the beetl template language, which configuration file is wrong, what is the reason for the error, the prompt is very clear, and it is in Chinese

 

 

For the following sql configuration, the if statement has an additional d

 

 

 

 select * from User where 1 =1   @if(isNotEmpty(age)d){ and age = #age# @}

 

 

 

The error prompt is as follows

 

 >>11: 14:10: Missing symbol (PARSER_MISS_ERROR): d on line 2 Resource: user.queryUser_page Excess input'd 'expected RIGHT_PAR 1|select * from User where 1 =1 2|@if(isNotEmpty(age)d){ 3|and age = #age# 4|@} 

 

 

Model support

hibernate , only pojo is supported, and the development is not fast enough due to relationship constraints
mybatis : Both pojo and map support, and map support can quickly support some small function development
beetlsql : Both pojo and map support. In addition, the hybrid model uses fewer pojos to complete a large number of query operations without adding additional attributes to the model. Therefore, the model support is better than myabitis.

 

 

 

 List<User> list = sqlManager.select("user.queryXXX",User.class,paras); List<Map> list = sqlManager.select("user.queryXXX",Map.class,paras); //If the user inherits the Tail interface, it supports the hybrid model. The following example can access the role name through user.get ("roleName"), which is very useful for web projects List<User> list = sqlManager.excute("select u.*,r.name  roleName from user u,role r ...  ",User.class,paras);

 

 

learning curve

hibernate There is no doubt that the most difficult thing to master is hibernate, which is generally recognized. Its awkward point is that it is clearly an sql operation, and developers must first convert to objects, and then to sql (in fact, sql language has shielded the complexity of operating databases). You can know the complexity of hibernate by looking at how many books and documents it has
myabtis : It's very simple. In addition to writing mapper files, you may need to learn how to write them, but it only takes 3-5 days. The starting point of the author of Myabtis was also to write a "dao tool without a book". He did it, but he did not do it well enough.
beetlsql : API is only available in SQLManager. The sql file is in md format, which eliminates the tediousness of xml, and uses beetl as the template language. It is very simple and can be mastered in one day. For database access technology, there should be no so-called "brick house", but hibernate has, which is abnormal. Therefore, hiberante scored the lowest and beetlsql the highest.

 

 

 

 

DBA friendly

hibernate : This tool is hostile to DBAs, and naturally DBAs do not like hibernate. I won't talk about it here.
myabtis : The SQL file is managed separately and adopts the xml method, which is relatively friendly, but the xml method is too cumbersome. Like the common "<" symbol, Myabtis has to deal with it specially
beetlsql : SQL can appear in both the code and the SQL file. Because of the md+beetl format, it is better than Myabtis. DBA looks at the beetlsql SQL file as if it were a document

 

 

 

 

Friendly with other tools

hibernate : Because the container management bean is used, the effect of using third-party tools in the container is different from that of using third-party tools outside the container, such as Hibernate's unique Open Session In View problem, and Session Has Closed
myabtis : No container management, so it's easy to use
beetlsql : Like myabtis, it is friendly to other tools

 

 

 

 

performance

No specific comparison has been made, but considering that all three tools involve sql template parsing, MetaData acquisition and analysis, ResutlSet mapping operation, java reflection, etc., the performance is similar. I will give specific performance comparisons in the future. Considering the database operation, dao can be ignored, so 5 points are given for performance

 

 

 

 

Database master-slave support

hibernate : Not supported. Third party routing middleware is required
myabtis : Yes, but some simple coding is required to switch data sources
beetlsql : Support database master-slave configuration without developer coding. Beetlsql decides to go to the master and slave database by default according to whether the sql is queried or updated. It can also decide to go to the master and slave database according to transactions, such as read-only transactions. Beetlsql is not only suitable for a start-up project, but also suitable for the huge number of users with the rapid development of the project. Of course, the huge number of users is beyond the function of beetlsql.

 

 

 

 

OR Mapping

hibernate : Entities and relationships are configured to complete OR Mapping, including the implementation of one to many, many to many, inheritance and other relationships. However, as I said in an article about OR Mapping, hibernate does a very good job, but it is useless.
myabtis : Entity and relationship are also configured, but they are not used well because they are added later
beetlsql : Support entity mapping, not relationship mapping, but support relationship mapping query, one-to-one, one-to-many, many to many query and lazy loading

 

 

 

 

One minute introduction to BeetlSQL

Since beetlsql is a new tool, a simple example is used to illustrate beetlsql

//The initialization part is integrated with the framework, such as directly injecting SqlManager through spring ioc

 

 

 

 MySqlStyle style = new MySqlStyle(); MySqlConnectoinSource cs = new MySqlConnectoinSource(); SQLLoader loader = new ClasspathLoader("/org/beetl/sql/test"); SQLManager sql = new SQLManager(style,loader,cs,new DefaultNameConversion(), new Interceptor[]{new DebugInterceptor()});

 

 //Directly use the api operation of sql manager User user = ....; sql.insert(user); User query = new User(); query.setStatus(1); List<User> list = sql.template(User.class,query); //Query the mySelect sql template of the user.sql file List<User> list = sql.select("user.mySelect",User.class,query) ;



Beetlsql also encapsulates the SqlManager and provides the dao interface

 

 

 

 public interface UserDao extends BaseMapper<User> { public User findById(@Param("id") Integer id); public int getCount(); public void setUserStatus(User user); public Integer setAge(@Param("id") Integer id,@Param("age") Integer age);

 

}

Then, it can be used in business logic to make dao easier to maintain

 

 

 UserDao dao = sql.getMapper(UserDao.class); dao.insert(user); //Use the built-in methods of BaseMapper dao.setAge(12, 18); //Call the setAge sql template in the user.sql file



User.sql is an md format file

 

 setAge === *Change user age update user set age = #age# where id=#id#
Expand to read the full text
Loading
Click to join the discussion 🔥 (23) Post and join the discussion 🔥
Reward
twenty-three comment
thirty-eight Collection
nine fabulous
 Back to top
Top