编程知识 cdmana.com

Detailed explanation of type mapping processor in mybatis

The last article combed Configuration Initialization analysis , Continue today typeHandlers part .

Database vendor identification

This is followed by an analysis of , The next analysis is databaseIdProvider node , This node is easy to understand , Literally, it's the database provider's id, That is to say mybatis What kind of database do you need to access .

We all know mybatis Just one. ORM frame , It's not about specific databases , Need to be compatible with multiple databases , therefore mybatis Different statements can be executed according to different database vendors , This multi vendor support is based on databaseId attribute .

Parse the source code configured in the configuration file as shown in the figure below :

 

You can see that the final return is a string to configuration Of databaseId attribute , At the same time, we can also achieve a DatabaseIdProvider, But you should configure it in the alias configuration first .

 

Type processor

Because no matter  MyBatis Preprocessing statement (PreparedStatement) When setting parameters in , Again, when I take a value from the result set , Need to be jdbc Data types and Java Data types are converted to each other , And that's what type processors do ,mybatis The source code is shown in the figure below :

 

typeHandlerElement(root.evalNode("typeHandlers")); It's the parse type mapper code , from typeHandlerElement Method to know that the type parser supports two methods , Package scanning and specific types are supported as well as aliases . Package scanning is similar to alias , Find all the classes under the corresponding package , And then parse it into TypeHandlerRegistry Class .

From the source code above, you can see that to achieve Java Type to jdbc The mapping of types requires three classes :Java type ,JDBC type ,Handler. After the object is reached, it is called TypeHandlerRegistry Of register Method to TypeHandlerRegistry In the middle , So how should the relationships between these objects be mapped ?

 

TypeHandlerRegistry The object is saved with Java The type and JDBC Type correspondence , From the above TypeHandlerRegistry Source code can see a few important attributes can understand mybatis How to implement the mapping between types . The key attributes are explained as follows :

Map<JdbcType, TypeHandler<?>> jdbcTypeHandlerMap: Record JdbcType and TypeHandler The correspondence between , among JdbcType Is an enumerated type , It defines the corresponding JDBC type , This set is mainly used when reading data from the result set , Take data from Jdbc Type conversion to Java type ;

  Map<Type,Map<JdbcType,TypeHandler<?>>> typeHandlerMap : Recorded Java Type to specified JdbcType On conversion , Required TypeHandler object , Because a Java Type may correspond to more than one database type , such as Java Type in the String May be converted to database char、varchar Etc , So there's a one to many relationship , So it's worth using Map To store ;

 TypeHandler<Object> unknownTypeHandler: Some objects of unknown type TypeHandler;

  Map<Class<?>, TypeHandler<?>> allTypeHandlersMap: It's all recorded TypeHandler And the corresponding type of TypeHandler object ;

 

The key is the first two properties ,jdbcTypeHandlerMap Used to transfer query results to Java When using ,typeHandlerMap Used in Java Type transfer jdbc Use .

On the right side of the picture above TypeHandlerRegistry You can see two construction methods in the source code , I will put Configuration Some type mappings are generated in unknownTypeHandler attribute , Then the common type mapping is initialized .

 

Type handler custom

From the above source code can be concluded that to customize a type processor, you only need to implement TypeHandler Interface is fine , And realize its 4 A way , Here we implement a process List Set of processors , The source code is as follows :

 

These four methods are quite simple , The first way is to save the operation yes hold List Object is converted to a string set to save sql In the sentence , The last three methods are to find out the data and then convert them into List.

Once created, it is used , The key code used is shown in the figure below :

 

First, configure the created type in the configuration file , And then insert sql Where you specify the processor for the field , And then you can insert , You can see the results saved to the database and the query results , Basically meet the requirements .

 

summary

database id We rarely configure it ourselves , Unless there is a special need , This is mainly about type processors , Sometimes it works .

Here we just implement the set mapping to the data processor , Sometimes we can also use enumeration to database mapping , Interested students can go to understand , It's also relatively simple .

Configuration The initialization of is still the last mapper That's loaded , I wanted to end this article , Find out mapper Too much content for , The next one is to sort out in detail .

 

Java Programmer's daily learning notes , If your understanding is wrong, please exchange views !

版权声明
本文为[It Lechi]所创,转载请带上原文链接,感谢

Scroll to Top