This commit is contained in:
2026-02-05 19:17:43 +08:00
parent a389a7c5cd
commit c822959a24
4 changed files with 70 additions and 14 deletions

View File

@@ -0,0 +1,53 @@
package org.springblade.common.handler;
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.sql.*;
import java.util.Arrays;
import java.util.List;
@MappedJdbcTypes(JdbcType.ARRAY)
@MappedTypes(List.class)
public class PgBigIntArrayToListTypeHandler extends BaseTypeHandler<List<Long>> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, List<Long> parameter, JdbcType jdbcType) throws SQLException {
// 将 List<Long> 转换为 PostgreSQL 的 bigint[]
Array array = ps.getConnection().createArrayOf("bigint", parameter.toArray());
ps.setArray(i, array);
}
@Override
public List<Long> getNullableResult(ResultSet rs, String columnName) throws SQLException {
// 从 ResultSet 中读取数组并转换为 List<Long>
Array array = rs.getArray(columnName);
if (array == null) {
return null;
}
return Arrays.asList((Long[]) array.getArray());
}
@Override
public List<Long> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
// 从 ResultSet 中按列索引读取数组并转换为 List<Long>
Array array = rs.getArray(columnIndex);
if (array == null) {
return null;
}
return Arrays.asList((Long[]) array.getArray());
}
@Override
public List<Long> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
// 从 CallableStatement 中读取数组并转换为 List<Long>
Array array = cs.getArray(columnIndex);
if (array == null) {
return null;
}
return Arrays.asList((Long[]) array.getArray());
}
}