typeHandler type converter

In JDBC, you need to set the parameters of the precompiled SQL statement in the PreparedStatement object. After the SQL is executed, the data of the database will be obtained through the ResultSet object, and these MyBatis are implemented through typeHandler according to the type of data.

In typeHandler, it is divided into jdbcType and javaType, among which jdbcType is used to define database type, and javaType is used to define  Java  type, then the role of typeHandler is to undertake the mutual conversion between jdbcType and javaType.

As shown in Figure 1. In many cases, we don't need to configure typeHandler, jdbcType, javaType, because MyBatis will detect what type of typeHandler should be used for processing, but some scenarios cannot be detected.

For those scenarios that need to use custom enumerations, or scenarios where the database uses special data types, you can use a custom typeHandler to handle the conversion between types.

The role of typeHandler


Figure 1 The role of typeHandler


Like aliases, there are system-defined typeHandlers and custom typeHandlers in MyBatis. MyBatis will decide which typeHandler to use to process these conversion rules according to the javaType and the jdbcType of the database. The typeHandler provided by the system can cover the requirements of most scenarios, but it is not enough in some cases. For example, we have special conversion rules, such as enumeration classes.
 

 

Guess you like

Origin blog.csdn.net/unbelievevc/article/details/132289642