import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
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.springblade.core.tool.jackson.JsonUtil;
import org.springblade.core.tool.utils.StringUtil;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(List.class)
public class ListIntToListLongTypeHandler extends BaseTypeHandler<List<Long>> {
private static ObjectMapper mObjectMapper = new ObjectMapper();
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, List<Long> longs, JdbcType jdbcType) throws SQLException {
String json = JsonUtil.toJson(longs);
preparedStatement.setObject(i, json);
}
@Override
public List<Long> getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
String value = resultSet.getString(columnName);
return getLongs(value);
}
@Override
public List<Long> getNullableResult(ResultSet resultSet, int i) throws SQLException {
String value = resultSet.getString(i);
return getLongs(value);
}
@Override
public List<Long> getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
String value = callableStatement.getString(i);
return getLongs(value);
}
private List<Long> getLongs(String value) {
if (StringUtil.isNotBlank(value)) {
try {
CollectionType type = mObjectMapper.getTypeFactory().constructCollectionType(ArrayList.class, Long.class);
List<Long> longs = mObjectMapper.readValue(value , type);
return longs;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
return null;
}
}
@TableField(typeHandler = ListIntToListLongTypeHandler.class)
private List<Long> couponIds;