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 org.apache.ibatis.type.MappedTypes;
|
|
import java.sql.CallableStatement;
|
import java.sql.PreparedStatement;
|
import java.sql.ResultSet;
|
import java.sql.SQLException;
|
import java.util.List;
|
|
@MappedTypes(List.class)
|
public class GenericListTypeHandler extends BaseTypeHandler<List<?>> {
|
|
@Override
|
public void setNonNullParameter(PreparedStatement ps, int i,
|
List<?> parameter, JdbcType jdbcType) throws SQLException {
|
String json = JSON.toJSONString(parameter, SerializerFeature.WriteMapNullValue);
|
ps.setString(i, json);
|
}
|
|
@Override
|
public List<?> getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
String json = rs.getString(columnName);
|
return json == null ? null : JSON.parseArray(json, Object.class);
|
}
|
|
@Override
|
public List<?> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
String json = rs.getString(columnIndex);
|
return json == null ? null : JSON.parseArray(json, Object.class);
|
}
|
|
@Override
|
public List<?> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
String json = cs.getString(columnIndex);
|
return json == null ? null : JSON.parseArray(json, Object.class);
|
}
|
}
|