增加BaseListTypeHandler

This commit is contained in:
金海洋 2024-01-22 18:03:04 +08:00
parent dbcf2f80c5
commit 26f979961a
2 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,54 @@
package cn.axzo.pokonyan.config.mybatisplus.type;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
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 java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* @author haiyangjin
* @date 2023/9/13
*/
@MappedTypes({List.class})
@MappedJdbcTypes(JdbcType.VARCHAR)
public abstract class BaseListTypeHandler<T> extends BaseTypeHandler<List<T>> {
private Class<T> type = getGenericType();
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i,
List<T> list, JdbcType jdbcType) throws SQLException {
preparedStatement.setString(i, JSONArray.toJSONString(list, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty));
}
@Override
public List<T> getNullableResult(ResultSet resultSet, String s) throws SQLException {
return JSONArray.parseArray(resultSet.getString(s), type);
}
@Override
public List<T> getNullableResult(ResultSet resultSet, int i) throws SQLException {
return JSONArray.parseArray(resultSet.getString(i), type);
}
@Override
public List<T> getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
return JSONArray.parseArray(callableStatement.getString(i), type);
}
private Class<T> getGenericType() {
Type t = getClass().getGenericSuperclass();
Type[] params = ((ParameterizedType) t).getActualTypeArguments();
return (Class<T>) params[0];
}
}

View File

@ -0,0 +1,8 @@
package cn.axzo.pokonyan.config.mybatisplus.type;
/**
* @author haiyangjin
* @date 2023/9/13
*/
public class LongListTypeHandler extends BaseListTypeHandler<Long> {
}