Basic usage of MyBatis - receiving data of unknown type

The MyBatisPlus framework can receive unknown types of data as follows:

  1. Ways to use annotations

    • In entity classes, @TableField(typeHandler = YourTypeHandler.class)annotations can be used to specify custom type handlers, where YourTypeHandleris your custom type handler class.
    • In the custom type processor class, you need to inherit TypeHandlerthe interface and implement the methods in it, including setParameterand and getResultso on.
    • In setParameterthe method, you can convert the unknown type data to the type supported by the database, and then set it into the PreparedStatement object.
    • In getResultthe method, you can get the value of the database from the ResultSet object and convert it to a Java object.
  2. How to use enum class

    • An enumeration class can be created, which defines the types supported by the database and the corresponding Java types.
    • In the entity class, you can use @TableField(typeEnum = YourTypeEnum.class)annotations to specify a custom enumeration class, which YourTypeEnumis your custom enumeration class.
    • In the custom enumeration class, it is necessary to define the enumeration value corresponding to the type supported by the database, and implement the related conversion method.
    • In the conversion method, data of an unknown type can be converted into a corresponding Java type, or a Java type can be converted into a type supported by the database.

It should be noted that YourTypeHandlerthe and in the above methods YourTypeEnumneed to be named and implemented according to the actual situation.

The sample code is as follows:

// 自定义类型处理器类
public class YourTypeHandler implements TypeHandler<YourType> {
    
    
    @Override
    public void setParameter(PreparedStatement ps, int i, YourType parameter, JdbcType jdbcType) throws SQLException {
    
    
        // 将未知类型数据转换为数据库支持的类型,并设置到PreparedStatement对象中
    }

    @Override
    public YourType getResult(ResultSet rs, String columnName) throws SQLException {
    
    
        // 从ResultSet对象中获取数据库的值,并将其转换为Java对象
    }

    // 其他方法省略...
}

// 自定义枚举类
public enum YourTypeEnum implements IEnum<Integer> {
    
    
    TYPE1(1, YourType.class),
    TYPE2(2, YourType.class);

    private Integer value;
    private Class<? extends YourType> type;

    YourTypeEnum(Integer value, Class<? extends YourType> type) {
    
    
        this.value = value;
        this.type = type;
    }

    @Override
    public Integer getValue() {
    
    
        return value;
    }

    public Class<? extends YourType> getType() {
    
    
        return type;
    }

    public static YourTypeEnum fromValue(Integer value) {
    
    
        for (YourTypeEnum e : YourTypeEnum.values()) {
    
    
            if (e.getValue().equals(value)) {
    
    
                return e;
            }
        }
        return null;
    }

    // 其他方法省略...
}

// 实体类中的字段定义
@TableField(typeHandler = YourTypeHandler.class)
private YourType yourType;

@TableField(typeEnum = YourTypeEnum.class)
private YourType yourType;

The above is an example of using annotations and enumeration classes to receive unknown types of data. You can choose one of the methods to implement according to your actual needs.

Guess you like

Origin blog.csdn.net/qq_41177135/article/details/131772308