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 are the design patterns in Mybatis

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "what design patterns are in Mybatis". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

builder design pattern

Builder Pattern uses several simple objects to build a complex object step by step. This type of design pattern is a creation pattern that provides a best way to create objects.

In Mybatis there is SqlSessionFactoryBuilder, building SqlSessionFactory, which uses the Builder pattern.

Also, in Mybatis class names ending in Builder are basically Builder patterns. Here's a very complete builder pattern in Mybatis:

XMLConfigBuilder: XML Configuration Builder, Builder Pattern, Inheriting BaseBuilder.

factory pattern

It is a factory dedicated to creating certain objects. Whatever object you want, just open your mouth. I will create what I can create. You don't need to know how it was created.

Classes ending in Factory in Mybatis basically use the Factory pattern.

Cases in life: Many outsourcing companies do banking systems. As long as the bank gives them the demand, it will do something for me. The outsourcing companies work hard to make the old iron work overtime, and finally they rush out. Outsourcing companies are factories, banks are clients. No matter how you get it out of the client, outsourcing companies don't tell banks.

For example:

SqlSessionFactory: Creates a SqlSession object.

ObjectFactory: Object Factory, all objects are generated by the factory.

MapperProxyFactory: Creates MapperProxy objects.

singleton pattern

Singleton pattern is one of the simplest design patterns in Java. This type of design pattern is a creation pattern that provides a best way to create objects. This pattern involves a single class responsible for creating its own objects while ensuring that only a single object is created. This class provides a way to access its unique objects directly, without instantiating objects of the class.

There is only one sun and one moon.

org.apache.ibatis.logging.LogFactory, logging factory class.

Why single-case mode? The code below:

org.apache.ibatis.executor.ErrorContext,

proxy mode

In Proxy Pattern, one class represents the functionality of another class. This type of design pattern is a structural pattern.

Cases in life: real estate agents, matchmaking agencies, scalpers, etc. are agency models.

The core of Mybatis implementation, such as MapperProxy creates proxy classes for binding Mapper and Mapper.xml we developed, Plugin creates a proxy class for each plugin, etc.

Mybatis, especially the use of dynamic agents is quite a lot, it is recommended that you first learn the proxy model, and then learn dynamic agents (JDK and CGlib these two), if you want to see Mybatis source code, dynamic agents must master.

adapter pattern

Adapter Pattern is used as a bridge between two incompatible interfaces. This type of design pattern is a structural pattern that combines the functionality of two separate interfaces. This pattern involves a single class that is responsible for adding independent or incompatible interface functionality.

In Mybatis, Log requires an adapter for Log4j, JDK, and longging components that are not directly connected to the slf4j interface.

template method pattern

In Template Pattern, an abstract class exposes a template that defines how its methods are executed. Its subclasses can override method implementations as needed, but calls will be made in the manner defined in the abstract class. This type of design pattern is a behavioral pattern. Define the skeleton of an algorithm in an operation and defer some steps to subclasses. Template methods allow subclasses to redefine certain steps of an algorithm without changing its structure.

In Mybatis, for example, parent BaseExecutor, child SimpleExecutor, BatchExecutor, ReuseExecutor. BaseTypeHandler and all subclasses such as IntegrTypeHandler;

Basically, we implement a generic method in the parent class and then create an abstract method, which is left to the child class to implement. This abstract method is also called hook method.

decorator pattern

Decorator Pattern allows you to add new functionality to an existing object without changing its structure. This type of design pattern is a structural pattern that acts as a wrapper around an existing class. This pattern creates a decorative class that wraps the original class and provides additional functionality while maintaining the integrity of the class method signature.

Most of the actual development is used to extend some functionality of older projects. This pattern is generally not used much in new projects.

Case in life: people rely on clothes horse saddle. Beauty camera, no camera, beauty show.

This design pattern focuses on extending existing functionality.

In Mybatis, Cache implementation classes LruCache, FifoCache, etc. are decorated with a PerpetualCache class. A common code format is that a decorated class has an attribute of the decorated class, and this attribute is also a parameter of the constructor method.

chain of responsibility model

The Chain of Responsibility Pattern creates a chain of receiver objects for the request. This pattern gives the type of request and decouples the sender and receiver of the request. This type of design pattern is a behavioral pattern. In this pattern, typically each receiver contains a reference to another receiver. If an object cannot handle the request, it passes the same request on to the next recipient, and so on.

Examples in life:

We initiate an approval in the OA system, showing the project manager, then the department manager, then HR, then the boss.

Interview process, showing team leader interview, project manager interview, department manager interview, HR interview.

In Mybatis, there is an attribute interceptors in the InterceptorChain, which is the plugin that saves all Mybatis. When executing the plugin, it is to traverse these interceptors. A Plugin->B Plugin->C Plugin...

"What design patterns are in Mybatis" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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

Development

Wechat

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

12
Report