add BaseSetTypeHandler

This commit is contained in:
金海洋 2024-03-04 09:45:05 +08:00
parent 26f979961a
commit a00136a774
2 changed files with 129 additions and 0 deletions

View File

@ -0,0 +1,64 @@
package cn.axzo.pokonyan.config.mybatisplus.type;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.Strings;
import com.google.common.collect.Sets;
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.Set;
/**
* @author haiyangjin
* @date 2024/3/4
*/
@MappedTypes({Set.class})
@MappedJdbcTypes(JdbcType.VARCHAR)
public abstract class BaseSetTypeHandler<T> extends BaseTypeHandler<Set<T>> {
private Class<T> type = getGenericType();
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i,
Set<T> set, JdbcType jdbcType) throws SQLException {
preparedStatement.setString(i, JSONArray.toJSONString(Sets.newLinkedHashSet(set), SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty));
}
@Override
public Set<T> getNullableResult(ResultSet resultSet, String s) throws SQLException {
return toLinkedHashSet(resultSet.getString(s));
}
@Override
public Set<T> getNullableResult(ResultSet resultSet, int i) throws SQLException {
return toLinkedHashSet(resultSet.getString(i));
}
@Override
public Set<T> getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
return toLinkedHashSet(callableStatement.getString(i));
}
private Class<T> getGenericType() {
Type t = getClass().getGenericSuperclass();
Type[] params = ((ParameterizedType) t).getActualTypeArguments();
return (Class<T>) params[0];
}
private Set<T> toLinkedHashSet(String dbValue) {
if (Strings.isNullOrEmpty(dbValue)) {
return Sets.newLinkedHashSet();
}
return Sets.newLinkedHashSet(JSONArray.parseArray(dbValue, type));
}
}

View File

@ -0,0 +1,65 @@
package cn.axzo.pokonyan.config.mybatisplus.type;
/**
* @author haiyangjin
* @date 2024/3/4
*/
import com.google.common.base.Splitter;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
/**
* 1. 用于将数据库中的, 逗号分割的String, 转换为Set.
* 2. 存储时将Set直接存String, 多个使用逗号分割
* @author haiyangjin
*/
@MappedJdbcTypes({JdbcType.VARCHAR})
public class SetTypeHandler extends BaseTypeHandler<Set<String>> {
private static final String DELIMITER = ",";
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Set<String> parameter, JdbcType jdbcType) throws SQLException {
String value = Sets.newLinkedHashSet(Optional.ofNullable(parameter).orElse(Collections.emptySet()))
.stream()
.collect(Collectors.joining(DELIMITER));
ps.setString(i, value);
}
@Override
public Set<String> getNullableResult(ResultSet rs, String columnName) throws SQLException {
return getSet(rs.getString(columnName));
}
@Override
public Set<String> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return getSet(rs.getString(columnIndex));
}
@Override
public Set<String> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return getSet(cs.getString(columnIndex));
}
private Set<String> getSet(String dbValue) {
return Splitter.on(",")
.omitEmptyStrings()
.trimResults()
.splitToList(Optional.of(dbValue).orElse(StringUtils.EMPTY))
.stream()
.collect(Collectors.toSet());
}
}