MyBatis數據庫字段映射Java枚舉

有時候咱們須要將數據庫的字段對Java的枚舉類型進行映射,好比說咱們有一個汽車配件類型的枚舉java

public enum ProductType implements Localisable {
    TYPE1("配件"),
    TYPE2("車品");

    private String value;
    private ProductType(String value) {
        this.value = value;
    }

    @Override
    public String getValue() {
        return this.value;
    }
}

該枚舉類型實現了一個接口數據庫

public interface Localisable {
    String getValue();
}

有一個配件分類的實體類,包含了該枚舉字段(此處只包含部分字段屬性)mybatis

/**
 * 配件分類
 */
@AllArgsConstructor
@NoArgsConstructor
public class ProviderProductLevel {
    @Getter
    @Setter
    private Long id;
    @Getter
    @Setter
    private String code;
    @Getter
    @Setter
    private String name;
    @Getter
    @Setter
    private Integer sort;
    @Getter
    @Setter
    private Integer level;
    @Getter
    @Setter
    private ProductType productType;
    @Getter
    @Setter
    private String pictureUrl;
}

而在數據庫中的表結構以下app

dao方法以下(經過id查找一個配件分類,並實例化)ide

@Mapper
public interface LevelDao {
    ProviderProductLevel findLevel1(Long id);
}

mapper映射文件以下測試

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cloud.productprovider.dao.LevelDao">
    <resultMap id="level1Map" type="com.cloud.productprovider.composite.ProviderProductLevel">
        <id property="id" column="id" />
        <result property="code" column="code" />
        <result property="name" column="name" />
        <result property="sort" column="sort" />
        <result property="productType" column="product_type" typeHandler="com.cloud.productprovider.untils.DbEnumTypeHandler" />
    </resultMap>
    <select id="findLevel1" resultMap="level1Map" parameterType="java.lang.Long"
            resultType="com.cloud.productprovider.composite.ProviderProductLevel">
        select id,code,name,sort,product_type from product_level
        <where>
            id=#{id}
        </where>
    </select>
</mapper>

咱們能夠看到這裏有一個映射處理器typeHandler="com.cloud.productprovider.untils.DbEnumTypeHandler"this

該映射處理器的代碼以下spa

@AllArgsConstructor
public class DbEnumTypeHandler extends BaseTypeHandler<Localisable> {
    private Class<Localisable> type;
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Localisable parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i,parameter.getValue());
    }

    @Override
    public Localisable getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String value = rs.getString(columnName);
        if (rs.wasNull()) {
            return null;
        }else {
            return convert(value);
        }
    }

    @Override
    public Localisable getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String value = rs.getString(columnIndex);
        if (rs.wasNull()) {
            return null;
        }else {
            return convert(value);
        }
    }

    @Override
    public Localisable getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String value = cs.getString(columnIndex);
        if (cs.wasNull()) {
            return null;
        }else {
            return convert(value);
        }
    }

    private Localisable convert(String value) {
        Localisable[] dbEnums = type.getEnumConstants();
        for (Localisable dbEnum : dbEnums) {
            if (dbEnum.getValue().equals(value)) {
                return dbEnum;
            }
        }
        return null;
    }
}

經測試返回的結果對象的Json字符串以下code

{"code":"0000001","id":1,"name":"油品","productType":"TYPE1","sort":1}xml

相關文章
相關標籤/搜索