In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "the design principle of mybatis framework". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The predecessor of mybatis is iBatis, which is derived from the combination of "Internet" and "abatis". It is an excellent persistence layer framework that supports customized SQL, stored procedures, and advanced mapping. Mybatis avoids almost all JDBC code and manually setting parameters and getting result sets, and it can use simple XML or annotations to configure and map native information, mapping interfaces and Java's POJO to records in the database.
Architecture Design of mybatis
The functional architecture of Mybatis is divided into three layers:
Interface layer: provides external use of the interface API, developers through these local API to manipulate the database. When the interface layer receives the call request, it will call the data processing layer to complete the specific data processing.
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.
Framework support layer: responsible for the most basic functional support, including connection management, transaction management, configuration loading, and caching, which are common things, extracted as the most basic components. It provides the most basic support for the upper data processing layer.
The running process of mybatis
Load configuration: the configuration comes from two places, the configuration file and the comments. The configuration information of SQL is loaded into MappedStatement objects (including incoming parameter mapping configuration, executed SQL statements, result mapping configuration) and stored in memory.
SQL parsing: when the API interface layer receives the call request, it will receive the ID and the incoming object (which can be Map, JavaBean or basic data type) of the incoming SQL. Mybatis will find the corresponding MappedStatement according to the ID of the SQL, and then parse the MappedStatement according to the passed parameter object. After parsing, you can get the final SQL statement and parameters to be executed.
SQL execution: take the final SQL and parameters to the database for execution, and get the results of operating the database.
Result mapping: the results of operating the database are converted according to the mapped configuration, which can be converted to HashMap, JavaBean or basic data types, and the final result is returned.
The directory of handwritten mybatis
Let's take a look at the author's handwritten mybatis file directory. Check the instructions on the Bootstrap file, then run the file, and breakpoint debugging will take you step by step to understand the principles of the mybatis framework. (many places have been annotated to facilitate beginners to learn more about the details of the framework.)
Please check the code address, https://github.com/y277an/java-you-need-know. Let's take a look at the genuine code structure.
Considering that transaction interferes with understanding the principles of the framework, all the code is deleted.
Mybatis directory structure and content
The following directory compares the source code of mybatis-3.5.1.
Annotations . Comment table of contents. Include all annotations, such as @ SELECT,@UPDATE, etc.
Binding . The instance reflection of the Mapper class generates the tool catalog.
Builder . Mainly annotations, constructors and converters for mapper and SqlSource.
Cache . Mybatis internal cache interface. Some specific caching strategies are implemented, such as FifoCache, LruCache, BlockingCache, LoggingCache and so on.
DataSource . Data source factory class and its implementation. The implementation classes include JndiDataSourceFactory, PooledDataSourceFactory, and UnpooledDataSourceFactory. Data source implementation classes: UnpooledDataSource and PooledDataSource.
Exceptions . The exception classes customized by Mybatis inherit from RuntimeException.
Executor . Actuator-related packages. It includes Key generator, loader (including proxies for Cglib and Javassist, result loader), parameter processor interface, result processor, result set (resultSet) processor, Statement processor (implementation class: BaseStatementHandler, CallableStatementHandler, PreparedStatementHandler, RoutingStatementHandler, SimpleStatementHandler), executor (SimpleExecutor, ReuseExecutor, CachingExecutor, BatchExecutor, BaseExecutor).
Io . Mainly defined several VFS (VFS, DefaultVFS, ClassLoaderWrapper).
Jdbc . Operations related to Sql. Such as Sql runners, script runners, and Sql wrapper classes.
Logging . All types of log adapters implement the Log interface. StdOutImpl, Slf4jImpl, NoLoggingImpl, Log4j2Impl, Log4jImpl, Jdk14LoggingImpl, BaseJdbcLogger and JakartaCommonsLoggingImpl.
Mapping . Mainly interface parameters, sql and mapping classes that return results. The main classes include: MappedStatement, ParameterMap, ParameterMapping, ResultMap, ResultMapping, BoundSql and SqlSource.
Parsing . Variable resolution, such as parsing ${}, # {}, etc.
Plugin . It mainly contains the definition interface of the plug-in. Such as Interceptor, Plugin and InterceptorChain.
Reflection . There are mainly tool methods and object factory classes for reflection operations, as well as some commonly used wrapper classes, such as BaseWrapper, BeanWrapper, CollectionWrapper, MapWrapper, and ObjectWrapper.
Scripting . A processor that performs driver and dynamic Sql parsing.
Session . Mainly SqlSession and SqlSessionFactory.
Transaction . It is mainly the jdbc transaction operation class which is simply encapsulated by mybatis.
Type . The processor for each type of data. Used to dynamically set parameters and transform data, such as set and get operations that IntegerTypeHandler uses to handle values of type Integer. In addition to the eight basic types, there are also commonly used collection and Map types, and various time types of processors have been added.
Core members and responsibilities of MyBatis
From the perspective of MyBatis code implementation, the core components of MyBatis are as follows
SqlSession . As the main top-level API of MyBatis work, it represents the session that interacts with the database and completes the necessary database addition, deletion, modification and query function.
Executor . MyBatis executor, which is the core of MyBatis scheduling, is responsible for the generation of SQL statements and the maintenance of query cache.
StatementHandler . Encapsulates the JDBC Statement operation, responsible for JDBCstatement operations, such as setting parameters, transforming Statement result sets into List collections.
ParameterHandler . Responsible for converting the parameters passed by the user into the parameters required by JDBC Statement.
ResultSetHandler . Responsible for converting the ResultSet result set objects returned by JDBC into collections of type List.
TypeHandler is responsible for mapping and converting between java data types and jdbc data types.
MappedStatement . MappedStatement maintains the encapsulation of a node.
SqlSource . It is responsible for generating the SQL statement dynamically according to the parameterObject passed by the user, encapsulating the information into the BoundSql object and returning it.
BoundSql . Represents the dynamically generated SQL statement and the corresponding parameter information.
Configuration . All configuration information for MyBatis is maintained in the Configuration object.
This is the end of the content of "the Design principles of the mybatis Framework". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.