Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the use of Mybatis?

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail what is the use of Mybatis for you. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Brief introduction

MyBatis, formerly known as iBatis, was originally an open source project of apache, which was migrated from apache software foundation to google code in 2010 and renamed MyBatis. MyBatis is an excellent persistence layer framework that supports normal SQL queries, stored procedures, and advanced mapping. MyBatis eliminates almost all manual setting of JDBC code and parameters and retrieval of result sets. MyBatis uses simple XML or annotations for configuration and raw mapping, mapping interfaces and Java's POJOs (Plan Old Java Objects, plain Java objects) to records in the database.

The functional structure of Mybatis is divided into three layers (Baidu encyclopedia is borrowed from the picture):

1) API interface layer: provides external use of the interface API, developers through these local API to manipulate the database. As soon as the interface layer receives the call request, it will call the data processing layer to complete the specific data processing.

2) data processing layer: responsible for specific SQL lookup, SQL parsing, SQL execution and result mapping processing. Its main purpose is to complete a database operation according to the called request.

3) basic support layer: responsible for the most basic functional support, including connection management, transaction management, configuration loading and cache processing, these are common things, extracted as the most basic components. It provides the most basic support for the upper data processing layer.

Getting started

Overall flow chart of Mybatis

SqlSessionFactoryBuilder

The entrance to each MyBatis application is SqlSessionFactoryBuilder, which creates a Configuration object through an XML configuration file (or you can create it yourself, of course), and then creates a SqlSessionFactory object through the build method. There is no need to create a SqlSessionFactoryBuilder every time you visit Mybatis, it is common practice to create a global object. The example program is as follows:

[java] view plain copy

Private static SqlSessionFactoryBuilder sqlSessionFactoryBuilder

Private static SqlSessionFactory sqlSessionFactory

Private static void init () throws IOException {

String resource = "mybatis-config.xml"

Reader reader = Resources.getResourceAsReader (resource)

SqlSessionFactoryBuilder = new SqlSessionFactoryBuilder ()

SqlSessionFactory = sqlSessionFactoryBuilder.build (reader)

}

SqlSessionFactory

The SqlSessionFactory object is created by SqlSessionFactoryBuilder. Its main function is to create SqlSession objects, just like SqlSessionFactoryBuilder objects, it is not necessary to create SqlSessionFactory every time you visit Mybatis, the usual practice is to create a global object. One of the necessary properties of the SqlSessionFactory object is the Configuration object, which is a configuration object that holds the global configuration of Mybatis, usually created by SqlSessionFactoryBuilder from the XML configuration file. Here is a simple example:

[html] view plain copy

Note the declaration of the XML header, which needs to be used to verify the correctness of the XML document. The typeAliases element is a list of all typeAlias (aliases), and aliases are used to replace the full class name, so that aliases can be used where the full class name is needed. The environment element body contains the environment configuration for transaction management and connection pooling. The mappers element is a list of all the mapper (mappers) whose XML files contain SQL code and mapping definition information. Of course, there are many configurations in the XML configuration file, and the above example indicates the most critical part. For other configurations, please refer to the official documentation of Mybatis.

SqlSession

The main function of SqlSession object is to access the database and map the results. It is similar to the session concept of database. Because it is not thread-safe, the scope of SqlSession object needs to be limited within the method. The default implementation class for SqlSession is DefaultSqlSession, which has two properties that must be configured: Configuration and Executor. As described earlier by Configuration, there is no more to say here. All the operations of SqlSession to the database are done through Executor, and the specific functions of Executor are described in the next section.

What we've seen so far is the mybatis process. Where does our application plug into this process and get the results we want? This is where SqlSession is. SqlSession has an important method, getMapper, which, as its name implies, is used to get Mapper objects. What is a Mapper object? According to the official manual of Mybatis, in addition to initializing and starting Mybatis, the application also needs to define some interfaces, which define the method of accessing the database, and the XML configuration file with the same name needs to be placed under the package path of the interface. The getMapper method of SqlSession connects the application with Mybatis. When the application accesses getMapper, Mybatis generates a proxy object, which is called Mapper object, based on the interface type passed in and the corresponding XML configuration file. After the application obtains the Mapper object, it should access the Mybatis's SqlSession object through this Mapper object, thus achieving the purpose of inserting it into the Mybatis process. The sample code is as follows:

[java] view plain copy

SqlSession session= sqlSessionFactory.openSession ()

UserDao userDao = session.getMapper (UserDao.class)

UserDto user = new UserDto ()

User.setUsername ("iMybatis")

List users = userDao.queryUsers (user)

Corresponding APIs:

[java] view plain copy

Public interface UserDao {

Public List queryUsers (UserDto user) throws Exception

}

Corresponding profile:

[html] view plain copy

Select * from t_user t where t.username = # {username}

]] >

Executor

The Executor object is created when the Configuration object is created and is cached in the Configuration object. The main function of the Executor object is to call StatementHandler to access the database and store the query results in the cache, if the cache is configured.

StatementHandler

StatementHandler is the real place to access the database and calls ResultSetHandler to process the query results.

ResultSetHandler

Processing query results

This is the end of this article on "what's the use of Mybatis". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report