增加BaseListTypeHandler
This commit is contained in:
parent
dbcf2f80c5
commit
26f979961a
@ -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];
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
package cn.axzo.pokonyan.config.mybatisplus.type;
|
||||
|
||||
/**
|
||||
* @author haiyangjin
|
||||
* @date 2023/9/13
|
||||
*/
|
||||
public class LongListTypeHandler extends BaseListTypeHandler<Long> {
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user