What is the difference between javaType and ofType in Mybatis?

In MyBatis, javaType and ofType are used to specify the conversion relationship between data types in the database and data types in Java. The main difference between them is as follows:

  1. javaType is used to specify the Java type, while ofType is used to specify the type handler in MyBatis. The Java type refers to the data types in Java, while the type handler is a component in MyBatis used to convert data types from the database into data types in Java.
  2. The javaType attribute can be used in the result or param elements of ResultMap or ParameterMap to specify a Java type for type conversion in query result mapping or parameter mapping. The ofType attribute can be used in the result or param elements of ResultMap or ParameterMap to specify a type handler for data type conversion in query result mapping or parameter mapping.
  3. Both javaType and ofType can be used simultaneously to specify type conversion rules more accurately. If both javaType and ofType are specified, MyBatis will first use the Java type specified by javaType for type conversion, and then use the type handler specified by ofType for secondary conversion.

In general, javaType and ofType are both used to specify the conversion relationship between data types in the database and data types in Java, but their specific usage scenarios and functions are slightly different.

bannerAds