SoFunction
Updated on 2025-03-04

Detailed explanation of the method of MyBatis processing enum types

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 byTypeHandlerInterface 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 returnStatusFor 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 databasestatusString values ​​mapped toStatuson 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 itTypeHandlerTo achieve this.

Custom TypeHandler

When you need to process enum types in a non-standard way (for example, using enumeratedordinalvalue, or a field defined in an enum), you can implement itTypeHandlerInterface customizes the persistence logic of enumerations.

The following is a mapped using enumeration numbersTypeHandlerExample:

@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 thisTypeHandlerIn, we coveredsetNonNullParameterMethods to define how to defineStatusEnumeration settings toPreparedStatementand coveredgetNullableResultHow to define the method fromResultSetRead data and convert it toStatusenumerate.

Register TypeHandler in MyBatis configuration

DefinedTypeHandlerAfter that, you need to register in the MyBatis configuration file:

<typeHandlers>
  <typeHandler handler=""/>
</typeHandlers>

In-depth analysis

MyBatis actually passes when processing enum typesTypeHandlermechanism to implement. All enum type processing is ultimately done through the correspondingTypeHandlerto complete the conversion and mapping of data.

  • Default enumeration processing: For enum types, MyBatis is used internally by defaultEnumTypeHandler(Enum-basednameProcessing) andEnumOrdinalTypeHandler(Enum-basedordinaldeal with).
  • Custom processing logic: By implementingTypeHandlerInterface, developers can customize the processing logic of enumeration types to meet different business needs. This mechanism provides extremely high flexibility.

Summarize

MyBatis byTypeHandlerThe 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!