2024.10.4

mybatis 中表字段的映射实体类

package com.ruoyi.system.handler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.system.domain.ProductDetails;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class ProductDetailsTypeHandler extends BaseTypeHandler<List<ProductDetails>> {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, List<ProductDetails> parameter, JdbcType jdbcType) throws SQLException {
        try {
            ps.setString(i, objectMapper.writeValueAsString(parameter));
        } catch (JsonProcessingException e) {
            throw new SQLException("Failed to convert list to JSON string.", e);
        }
    }

    @Override
    public List<ProductDetails> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String json = rs.getString(columnName);
        if (json == null) {
            return null;
        }
        try {
            return objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, ProductDetails.class));
        } catch (IOException e) {
            throw new SQLException("Failed to convert JSON string to list.", e);
        }
    }

    @Override
    public List<ProductDetails> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String json = rs.getString(columnIndex);
        if (json == null) {
            return null;
        }
        try {
            return objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, ProductDetails.class));
        } catch (IOException e) {
            throw new SQLException("Failed to convert JSON string to list.", e);
        }
    }

    @Override
    public List<ProductDetails> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String json = cs.getString(columnIndex);
        if (json == null) {
            return null;
        }
        try {
            return objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, ProductDetails.class));
        } catch (IOException e) {
            throw new SQLException("Failed to convert JSON string to list.", e);
        }
    }
}

posted @ 2024-10-04 23:23  258333  阅读(4)  评论(0编辑  收藏  举报