Application of custom enumeration

Reference: https://segmentfault.com/a/1190000010755321

One: the scene,

1 OPEN(10),          // Open 
2      CLOSE(11),          // Close 
3      OFF_LINE(12),      // Offline 
4      FAULT(200),      // Fault 
5      UNKNOWN(255);      // Unknown

to represent 5 states.

  The foreground incoming is OPEN. Then what is saved in the database is 10. (insert)

  The query in the database is 10. The output is OPEN (query)

One: 1) Entity class

package com.ssm.bean;

import com.ssm.enums.ComputerState;

public  class Process {
     private Integer id;
     private String name;
     private ComputerState status; //is an enumeration class type
     public Process() {
    }

    public Process(Integer id, String name, ComputerState status) {
        this.id = id;
        this.name = name;
        this.status = status;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ComputerState getStatus() {
        return status;
    }

    public void setStatus(ComputerState status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Process{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", status=" + status +
                '}';
    }
}

2): Write a ComputerState enumeration class:

package com.ssm.enums;

public enum ComputerState implements BaseCodeEnum{
    OPEN( 10),          // open 
    CLOSE(11),          // close 
    OFF_LINE(12),      // offline 
    FAULT(200),      // fault 
    UNKNOWN(255);      // unknown

    private int code;
    ComputerState(int code) { this.code = code; }

    @Override
    public int getCode() {
        return this.code;
    }
}

3): Implemented BaseCodeEnum enumeration class.

/**
 * The interface has only one method that returns the code, and the return value will be stored in the database.
 */
public interface BaseCodeEnum {
    int getCode();
}

4): Customize the CodeEnumTypeHandler converter.

package com.ssm.enums;

import com.ssm.utils.CodeEnumUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CodeEnumTypeHandler<E extends Enum<?> & BaseCodeEnum> extends BaseTypeHandler<BaseCodeEnum> {

    private Class<E> type;

    public CodeEnumTypeHandler(Class<E> type) {
        if (type == null) {
            throw new IllegalArgumentException("Type argument cannot be null");
        }
        this.type = type;
    }

    /**
     * When used to define setting parameters, how to convert Java type parameters to corresponding database types
     *    When saving, what is passed in is: ComputerState.OPEN What is saved to the database is: 10 
     * @param ps
     * @param i
     * @param parameter
     * @param jdbcType
     * @throws SQLException
     */
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, BaseCodeEnum parameter, JdbcType jdbcType) throws SQLException {
        ps.setInt(i, parameter.getCode());
    }

    /**
     * Used to define how to convert the database type to the corresponding Java type when the field data is obtained by the field name
     *
     *        In the database is 10 The output value is: OPEN 
     * @param rs
     * @param columnName
     * @return
     * @throws SQLException
     */
    @Override
    public BaseCodeEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
        int code = rs.getInt(columnName);
        return rs.wasNull() ? null : codeOf(code);
    }

    /**
     * Used to define how to convert the database type to the corresponding Java type when the field data is obtained through the field index
     * This is indexed by field. Haven't figured out how to use it yet.
     * @param lol
     * @param columnIndex
     * @return
     * @throws SQLException
     */
    @Override
    public BaseCodeEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
       /* int code = rs.getInt(columnIndex);
        return rs.wasNull() ? null : codeOf(code);*/
       return null;
    }

    /**
     * After calling the stored procedure with the definition, how to convert the database type to the corresponding Java type
     * @param cs
     * @param columnIndex
     * @return
     * @throws SQLException
     */
    @Override
    public BaseCodeEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        /*int code = cs.getInt(columnIndex);
        return cs.wasNull() ? null : codeOf(code);*/
        return null;
    }

    private E codeOf(int code){
        try {
            return CodeEnumUtil.codeOf(type, code);
        } catch (Exception ex) {
            throw new IllegalArgumentException("Cannot convert " + code + " to " + type.getSimpleName() + " by code value.", ex);
        }
    }
}

5): Configure in mybatis-config.xml

<!--Use the enum class. The query from the database is 10, which is converted into OPEN (query) in the enumeration class. Pass is: OPEN Save to database is: 10 (save) -->
    <typeHandlers>
        <typeHandler handler="com.ssm.enums.CodeEnumTypeHandler" javaType="com.ssm.enums.ComputerState"/>
    </typeHandlers>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325020181&siteId=291194637