Mybastic Overview
Continuing our series of informative articles on information technology for our readers, today NAL SE Duy Truong presents an overview article on MyBatis: What is Mybastic?, Mybastic Features, Mybatis Benefits and Performance.
Continuing our series of informative articles on information technology for our readers, today NAL SE Duy Truong presents an overview article on MyBatis: What is Mybastic?, Mybastic Features, Mybatis Benefits and Performance.
①What is Mybastic❓
MyBatis is an open source framework created to simplify the connection to JDBC databases. It also provides an API to facilitate database operations.
②Mybastic features
Simplicity - MyBatis is widely used and is considered one of the simplest persistence frameworks available nowadays.
Rapid development - MyBatis can speed up application development due to its simplicity and ease of use.
Portability - MyBatis can be installed on almost any language or platform, including Java, Ruby, and C#.
Independent communication - MyBatis provides a database-independent interface and API.
Open source - MyBatis is free and open source software.
③Advantages of MyBatis
- Less JDBC code to write. For example, if a project needs to convert a database from Oracle to Postgres, the source uses JDBC, and you open a pile of source and add a String with a "+", you see it and think you don't need any more.
- Once you have a solid understanding of SQL and Java, MyBatis is very easy to use. Apart from a huge amount of documentation, very little new knowledge is required.
- While Spring projects are usually "bundled" for use with Hibernate, both Eclipse and IntelliJ support installing "this cheeky bird" as a plugin. There is no jar or downloadable file, which is very convenient. Furthermore, changing between versions is not that difficult.
- Query (high performance): With its caching and polling connection mechanisms, MyBatis offers better query performance than JDBC. However, when executing a SELECT for the first time, JDBC will not find any conflicts and will miss only because the SELECT has to call the database every time, and of course it cannot fetch the result from the cache.
- ORM support: MyBatis supports many features similar to ORM tools. Examples include lazy loading, join fetching, runtime code generation, and inheritance.
MyBatis Performance
Support for connection polling
If you don't know about polling, please refer to the article about http polling and connection pooling. Normally, every time a request is made to open a connection to the database, another connection is created; when using Mybatis, the polling mechanism (which does not close the connection after it has been created and used, but remains in the connection pool for the next reuse, and is only actually closed when the timeout expires) is used. Using polling can greatly reduce the cost of initializing connections and improve the performance of applications.
Mybatis uses polling to save the cost of initiating a database connection.
Polling configuration in MyBatis is relatively simple. For the dataSource type, just set type = "POOLED".
- Use cache for duplicate queries.
MyBatis has a mechanism to cache the results of SQL queries at the SqlSesssion level. This means that when you call back a select query that is mapped to the cache, Mybatis will no longer execute the query in the database and will fetch the result directly from the cache.
Let's look at the following example.
In that case, Mybatis will only execute the firstSelectWithCache. But when it comes to the second SQL statement, which is the same as SQL1, but in the same sqlsession, it will fetch the result directly from the cache. This is a small thing, but for applications that need to query data multiple times and in duplicate, using Mybatis can greatly improve performance.
The results of all queries are stored in a cache. The results of all queries are stored in the cache. insert/ update/ delete. will terminate the transaction (or at the end of a query configured with autoCommit = true).
To elaborate further, Mybatis supports two layers of caching. The first is a session cache and the second is a global cache.
- No proxies are used.
MyBatis makes heavy use of proxies and performs much better than other ORM frameworks that make heavy use of proxies.
④The difference between MyBatis and Hibernate
- There will be many people who will question it. MyBatis and Hibernate are different frameworks and when do we use each?
First, here are the differences between MyBatis and Hibernate.
MyBatis::
+ MyBatis is simpler and more compact.
+ MyBatis: +MyBatis is more flexible and reduces development time.
+ MyBatis uses SQL, which is a dependent database.
+ MyBatisはJDBC APIのResultSetをPOJOオブジェクトにマッピングするので、テーブル構造を気にする必要がない。
+ Easier to use stored procedures in MyBatis.
Hibernate::
+ Hibernate generates SQL, so you don't have to waste time writing SQL.
+ Hibernate is highly scalable and provides advanced caching.
+ Hibernate uses HQL, which is relatively independent of the database, making it easier to convert the database to Hibernate.
+ Hibernate maps Java POJO objects to tables.
+ It is more difficult to use procedures stored in Hibernate.
- When to use MyBatis
+ You want to create and maintain your own SQL.
+ The environment is driven by the relational data model.
+ You need to work with an existing complex schema.
いつWhen to use Hibernate使用するか
If your environment is oriented by an object model and you need to generate SQL automatically, use Hibernate.
⑤Setup and usage of MyBatis
Manual installation
Download the latest version of MyBatis here:
http://www.java2s.com/Code/Jar/m/Downloadmybatis302jar.htm
Download the latest version of mysqlconnector here:
Extract the downloaded file into a .jar file and place it in an appropriate directory.
Set the CLASSPATH for the extracted file.
Setup using Maven
How to use MyBatis
+ Using XML files
Using Anotation
Original article: https://media.nal.vn/mybatis-overview