package org.sxkj.common.handler;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
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;
|
import java.util.List;
|
|
public class CustomFastjsonTypeHandler extends BaseTypeHandler<List<Object>> {
|
|
@Override
|
public void setNonNullParameter(PreparedStatement ps, int i,
|
List<Object> parameter, JdbcType jdbcType) throws SQLException {
|
String json = JSON.toJSONString(parameter, SerializerFeature.WriteMapNullValue);
|
ps.setString(i, json); // 使用setString而非setObject
|
}
|
|
@Override
|
public List<Object> getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
String json = rs.getString(columnName);
|
return json == null ? null : JSON.parseArray(json, Object.class);
|
}
|
|
@Override
|
public List<Object> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
String json = rs.getString(columnIndex);
|
return json == null ? null : JSON.parseArray(json, Object.class);
|
}
|
|
@Override
|
public List<Object> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
String json = cs.getString(columnIndex);
|
return json == null ? null : JSON.parseArray(json, Object.class);
|
}
|
|
}
|