introduction
MyBatis is relatively straightforward to handle enum types, and it provides a flexible way to handle Java enums (enum
) Mapping between type and database. In MyBatis, you can handle enum types in two ways: using the name of the enum (name
) or enumeration sequence number (ordinal
). In addition, for more complex requirements, MyBatis allows you to implement it byTypeHandler
Interface custom enumeration type processor.
Use an enumeration name or an ordinal
By default, when MyBatis handles enum types, the name of the enum is used (name
) match and map with string values in the database. If your database design uses enumeration names to store it, this method is very direct and convenient.
For example, consider the following enum:
public enum Status { ACTIVE, INACTIVE, DELETED; }
In MyBatis' mapping file, if you have a returnStatus
For enumeration queries, you can write them directly like this:
<select resultType="Status"> SELECT status FROM some_table WHERE id = #{id} </select>
MyBatis will automatically return the databasestatus
String values mapped toStatus
on the name of the enum.
If you tend to use the enumeration's ordinal number (where each enum constant is in the declaration, starting from 0), you need to customize itTypeHandler
To achieve this.
Custom TypeHandler
When you need to process enum types in a non-standard way (for example, using enumeratedordinal
value, or a field defined in an enum), you can implement itTypeHandler
Interface customizes the persistence logic of enumerations.
The following is a mapped using enumeration numbersTypeHandler
Example:
@MappedTypes() public class StatusTypeHandler extends BaseTypeHandler<Status> { @Override public void setNonNullParameter(PreparedStatement ps, int i, Status parameter, JdbcType jdbcType) throws SQLException { (i, ()); } @Override public Status getNullableResult(ResultSet rs, String columnName) throws SQLException { int ordinal = (columnName); if (()) { return null; } return ()[ordinal]; } @Override public Status getNullableResult(ResultSet rs, int columnIndex) throws SQLException { int ordinal = (columnIndex); if (()) { return null; } return ()[ordinal]; } @Override public Status getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { int ordinal = (columnIndex); if (()) { return null; } return ()[ordinal]; } }
Customize in thisTypeHandler
In, we coveredsetNonNullParameter
Methods to define how to defineStatus
Enumeration settings toPreparedStatement
and coveredgetNullableResult
How to define the method fromResultSet
Read data and convert it toStatus
enumerate.
Register TypeHandler in MyBatis configuration
DefinedTypeHandler
After that, you need to register in the MyBatis configuration file:
<typeHandlers> <typeHandler handler=""/> </typeHandlers>
In-depth analysis
MyBatis actually passes when processing enum typesTypeHandler
mechanism to implement. All enum type processing is ultimately done through the correspondingTypeHandler
to complete the conversion and mapping of data.
-
Default enumeration processing: For enum types, MyBatis is used internally by default
EnumTypeHandler
(Enum-basedname
Processing) andEnumOrdinalTypeHandler
(Enum-basedordinal
deal with). -
Custom processing logic: By implementing
TypeHandler
Interface, developers can customize the processing logic of enumeration types to meet different business needs. This mechanism provides extremely high flexibility.
Summarize
MyBatis byTypeHandler
The mechanism provides flexible support for enumeration types. Whether using the name or sequence number of the enum, or the need to implement more complex enumeration processing logic, MyBatis can provide corresponding solutions. By customizingTypeHandler
, almost any form of enum type persistence strategy can be implemented.
The above is the detailed explanation of the method of MyBatis processing enum types. For more information about MyBatis processing enum types, please pay attention to my other related articles!