In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about the integration and use of Spring Boot + Mybatis-Plus. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
I. the principle of automatic injection of SQL
From the previous chapter, we can easily use SysLogMapper, which inherits the BaseMapper interface, to operate CRUD. Let's first take a look at the characteristics of traditional MaBatis:
MyBatis-Plus officially introduced MyBatis-Plus as an enhancement tool for MyBatis, which only makes enhancements and no changes on the basis of MyBatis. Just to simplify development and improve efficiency. At this point, a more intuitive experience is as follows:
Based on MyBatis
You need to write the XXMapper interface and manually write the CRUD method
XXMapper.xml mapping file, and manually write the SQL statement corresponding to each method
Based on MyBatis-Plus
You only need to create the XXMapper interface and inherit the BaseMapper interface to complete all general CRUD operations.
There is no need to create a SQL mapping file
As long as you inherit BaseMapper, you can complete all general CRUD operations. So let's take a look at the BaseMapper interface. Looking at the BaseMapper source code, you can see that there are 17 methods for various CRUD operations defined in the API, which are extremely convenient to achieve single, batch, paging and other operations.
Here you can see that the CRUD methods are defined here, so where is the final SQL statement that executes the operation of the database written? With this confusion in mind, I'll start by using the source SysLogMapper interface. This interface inherits only all the methods defined by the BaseMapper interface.
So how do these methods come true?
Here we first need to know the concept of dynamic agent. There are two kinds of dynamic proxy implementation in Spring: one is based on JDK using interface proxy, the other is based on cglib using class inheritance dynamic proxy. There is no in-depth explanation on the principle of dynamic agent here, interested companions can consult the data to understand, and we will explain and learn about this piece separately later.
We choose debug mode and hit a breakpoint to perform the query operation.
You can see that after sysLogMpper is dynamically proxied, you can see the selSessionFactory object and the configuration object in the object structure. The configuration object is the MyBatis-Plus global configuration object that contains all the configuration information. There is a mappedStatements attribute in the configuration object, which is the SQL statement mapping of the sysLogMpper interface method. MappedStatements is a HashMap,key as the method name, and value corresponds to each MappedStatement object.
We click on a key-value pair to see that there is a DynamicSqlSource class object sqlSource in the MappedStatement object, and the dynamic SQL statement of the CRUD operation method is written in the object.
So how do these dynamic SQL get into the MappedStatement object? The SQL injector is mentioned in the official website documentation, and the default is DefaultSqlInjector. Let's open this category and have a look.
Public class DefaultSqlInjector extends AbstractSqlInjector {public DefaultSqlInjector () {} public List getMethodList (Class mapperClass) {return (List) Stream.of (new Insert (), new Delete (), new DeleteByMap (), new DeleteById (), new DeleteBatchByIds (), new Update (), new UpdateById (), new SelectById (), new SelectBatchByIds (), new SelectByMap (), new SelectOne (), new SelectCount (), new SelectMaps (), new SelectMapsPage (), new SelectObjs (), new SelectList () New SelectPage () .resume (Collectors.toList ()) }}
This class inherits AbstractSqlInjector, overrides the getMethodList method, and returns a collection. In the collection are the method class objects executed by the instantiated CRUD. Let's open the Insert class, which inherits from the AbstractMethod class and overrides the injectMappedStatement method.
Public class Insert extends AbstractMethod {public Insert () {} public MappedStatement injectMappedStatement (Class mapperClass, Class modelClass, TableInfo tableInfo) {KeyGenerator keyGenerator = new NoKeyGenerator (); SqlMethod sqlMethod = SqlMethod.INSERT_ONE; String columnScript = SqlScriptUtils.convertTrim (tableInfo.getAllInsertSqlColumnMaybeIf (), (",") ", (String) null,", ") String valuesScript = SqlScriptUtils.convertTrim (tableInfo.getAllInsertSqlPropertyMaybeIf ((String) null), "(", ")", (String) null, ","); String keyProperty = null; String keyColumn = null; if (StringUtils.isNotEmpty (tableInfo.getKeyProperty () {if (tableInfo.getIdType () = = IdType.AUTO) {keyGenerator = new Jdbc3KeyGenerator (); keyProperty = tableInfo.getKeyProperty () KeyColumn = tableInfo.getKeyColumn ();} else if (null! = tableInfo.getKeySequence ()) {keyGenerator = TableInfoHelper.genKeyGenerator (tableInfo, this.builderAssistant, sqlMethod.getMethod (), this.languageDriver); keyProperty = tableInfo.getKeyProperty (); keyColumn = tableInfo.getKeyColumn () }} / / format SQL statement String sql = String.format (sqlMethod.getSql (), tableInfo.getTableName (), columnScript, valuesScript); SqlSource sqlSource = this.languageDriver.createSqlSource (this.configuration, sql, modelClass); return this.addInsertMappedStatement (mapperClass, modelClass, sqlMethod.getMethod (), sqlSource, (KeyGenerator) keyGenerator, keyProperty, keyColumn);}}
In the injectMappedStatement method, you can see and pass three parameters mapperClass, modelClass, and tableInfo, which are the mapper interface object, the entity class object, and the table information object mapped by the entity, respectively. In the method, the information here is formatted into a sql string according to the insert operation, and the last line calls the inherited addInsertMappedStatement method to encapsulate the above object information, and finally returns the MappedStatement object.
The above is our simple analysis of the principle of automatic injection of MyBatis-Plus into SQL, interested students can further study, welcome to leave a message to discuss with us.
Conditional constructor
In the previous introduction to the methods defined in the BaseMapper interface, we found that the Wrapper object was passed in the method parameters. This is the conditional constructor we're going to talk about next. Before you start to explain how to use the conditional constructor, understand what the conditional constructor is, sharpen the knife without mistakenly cutting firewood!
From the above illustration, you can clearly see the implementation and inheritance relationship between interfaces, abstract classes, and implementation classes. The reason for giving the above diagram, because the next will focus on the use of QueryWrapper, LambdaQueryWrapper, UpdateWrapper, LambdaUpdateWrapper four query and update wrappers, mainly used to deal with sql splicing, sorting, entity parameters for paging query and update, simple and convenient, no additional burden, can effectively improve development efficiency.
You can see the object query, which means two classes, QueryWrapper and LambdaQueryWrapper. The difference between the two classes is that the conditional parameters in QueryWrapper use database fields, not the Java attribute. LambdaQueryWrapper can tell from the class name that it supports the use of JDK8's new features, using method references for querying.
The common conditional parameters are as follows:
SetSqlSelect sets SELECT query field whereWHERE statement, splicing + WHERE conditional andAND statement, splicing + AND field = value andNewAND statement, splicing + AND (field = value) orOR statement, splicing + OR field = value orNewOR statement, splicing + OR (field = value) eq equals = allEq based on map content = ne is not equal to gt greater than or equal to > ge greater than or equal to lt less than
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.