实体类直接用list的话则会报错如下:

Type handler was null on parameter mapping for property 'specPrices'. It was either not specified and/or could not be found for the javaType (java.util.List) : jdbcType (null) combination.

解决方法:
在字段上添加以下注解

  @TableField(jdbcType = JdbcType.VARCHAR, insertStrategy = NOT_NULL, typeHandler = ListToStringHandler.class)

ListToStringHandler是我自己写的,如下。

package com.bmkj.project.common.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

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

/**
 * <p>
 * lsit转换string
 * </p>
 *
 * @author zsh
 * @since 2020/11/25
 */
public class ListToStringHandler  extends BaseTypeHandler<List> {

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, List list, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, JSON.toJSONString(list));
    }

    @Override
    public List getNullableResult(ResultSet resultSet, String s) throws SQLException {
        JSONArray jsonArray = JSONArray.parseArray( resultSet.getString(s));
        return jsonArray;
    }

    @Override
    public List getNullableResult(ResultSet resultSet, int i) throws SQLException {
        JSONArray jsonArray = JSONArray.parseArray( resultSet.getString(i));
        return jsonArray;
    }

    @Override
    public List getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        JSONArray jsonArray = JSONArray.parseArray( callableStatement.getString(i));
        return jsonArray;
    }
}

目前只是适用于添加和修改,如果查询的花还要在实体类上这样写:

@TableName(value = "dy_product_detail", autoResultMap = true)
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐