在后端处理数据时难免会遇到类型为
List的字段,为了方便(优雅)地处理这个列表类型字段的存储与读取,我们可以自定义Mybatis-Plus提供的BaseTypeHandler类,从而对任意类型的列表数据进行数据库相关处理
List表示任意对象的列表类型,即以下自定义配置可以处理任意类型对象的列表类型,例如:List、List等等(其中User为自定义的对象)
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.util.StringUtils;import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;@Slf4j
@MappedJdbcTypes(JdbcType.VARCHAR) //数据库类型
@MappedTypes({List.class}) //java数据类型
public abstract class ListTypeHandler extends BaseTypeHandler> {@Overridepublic void setNonNullParameter(PreparedStatement ps, int i, List parameter, JdbcType jdbcType) throws SQLException {String content = StringUtils.isEmpty(parameter) ? null : JSON.toJSONString(parameter);ps.setString(i, content);}@Overridepublic List getNullableResult(ResultSet rs, String columnName) throws SQLException {return this.getListByJsonArrayString(rs.getString(columnName));}@Overridepublic List getNullableResult(ResultSet rs, int columnIndex) throws SQLException {return this.getListByJsonArrayString(rs.getString(columnIndex));}@Overridepublic List getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {return this.getListByJsonArrayString(cs.getString(columnIndex));}private List getListByJsonArrayString(String content) {return StringUtils.isEmpty(content) ? new ArrayList<>() : JSON.parseObject(content, this.specificType());}/*** 具体类型,由子类提供* @return 具体类型*/protected abstract TypeReference> specificType();
}
这边以String类型为例
import com.alibaba.fastjson.TypeReference;import java.util.List;public class StringTypeHandler extends ListTypeHandler {// 将ListTypeHandler(T为任意对象),具体为特定的对象String@Overrideprotected TypeReference> specificType() {return new TypeReference>() {};}
}
以List为例,添加@TableField注解,表示该字段需要进行对应的转化
@ApiModelProperty("售后人员")
@TableField(typeHandler = StringTypeHandler.class)
private List afterSaleList;
通过以上操作后,可以实现
List列表类型的添加和修改
在实体类表头添加注解@TableName,一定要配置属性autoResultMap = true
@TableName(value = "base_knowledge", autoResultMap = true)
添加以上配置后,即可完成对List列表类型字段的读取