Merge branch 'feature/REQ-2010' into dev
This commit is contained in:
commit
cd6ddffe95
@ -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));
|
||||
}
|
||||
}
|
||||
@ -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());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user