下面展示一些 内联代码片

数据库保存数据,类型为List<String> ,字段只需设置为varchar[],然后导入下述类,并在实体类的对应字段位置加上@Type(type = "XX.ListToStringArrayUserType"),这里写保存工具类的包路径
/**
 * @Author ZGM
 * @DateTime 2021/8/26
 * @description
 */
public class ListToStringArrayUserType implements UserType, ParameterizedType, Serializable {
    protected static final int[] SQL_TYPES = {Types.ARRAY};

    /**
     * 默认 ArrayList
     */
    private Class listType;


    @Override
    public int[] sqlTypes() {
        return new int[]{Types.ARRAY};
    }

    @Override
    public Class returnedClass() {
        return listType;
    }

    @Override
    public boolean equals(Object o, Object o1) throws HibernateException {
        if (o == null) {
            return o1 == null;
        }
        return o.equals(o1);
    }

    @Override
    public int hashCode(Object o) throws HibernateException {
        return o.hashCode();
    }

    @Override
    public Object nullSafeGet(ResultSet resultSet, String[] strings, SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws HibernateException, SQLException {
        if (resultSet.getArray(strings[0]) == null) {
            return null;
        }

        Array array = resultSet.getArray(strings[0]);

        List<String> result = newList();

        String[] javaArray = (String[]) array.getArray();

        result.addAll(Arrays.asList(javaArray));

        return result;
    }

    @Override
    public void nullSafeSet(PreparedStatement preparedStatement, Object o, int i, SharedSessionContractImplementor sharedSessionContractImplementor) throws HibernateException, SQLException {
        Connection connection = preparedStatement.getConnection();
        if (o == null) {
            preparedStatement.setNull(i, SQL_TYPES[12]);
        } else {
            List castObject = (List) o;
            Object[] intArr = castObject.toArray();
            Array array = connection.createArrayOf("VARCHAR", intArr);
            preparedStatement.setArray(i, array);
        }
    }


    @Override
    public Object deepCopy(Object o) throws HibernateException {
        if (o == null) return null;
        List copyList = newList();
        copyList.addAll((List) o);
        return copyList;
    }

    @Override
    public boolean isMutable() {
        return true;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Serializable disassemble(Object o) throws HibernateException {
        return ((Serializable) o);
    }

    @Override
    public Object assemble(Serializable serializable, Object o) throws HibernateException {
        return serializable;
    }

    @Override
    public Object replace(Object o, Object o1, Object o2) throws HibernateException {
        return o;
    }

    @Override
    public void setParameterValues(Properties properties) {

        String listType = (String) properties.get("listType");
        if (!Strings.isNullOrEmpty(listType)) {
            try {
                this.listType = Class.forName(listType);
            } catch (ClassNotFoundException e) {
                throw new HibernateException(e);
            }
        } else {
            this.listType = java.util.ArrayList.class;
        }

    }

    private List newList() {
        try {
            return (List) listType.newInstance();
        } catch (Exception e) {
            throw new HibernateException(e);
        }
    }
}

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐