This commit is contained in:
Looly 2023-08-07 13:47:43 +08:00
parent 6f287b9524
commit 4a265b5d83
5 changed files with 140 additions and 45 deletions

View File

@ -178,6 +178,8 @@ public class RegisterConverter implements Converter, Serializable {
defaultConverterMap.put(XMLGregorianCalendar.class, new XMLGregorianCalendarConverter());
defaultConverterMap.put(java.util.Date.class, DateConverter.INSTANCE);
defaultConverterMap.put(DateTime.class, DateConverter.INSTANCE);
// 日期时间 java.sql
defaultConverterMap.put(java.sql.Date.class, DateConverter.INSTANCE);
defaultConverterMap.put(java.sql.Time.class, DateConverter.INSTANCE);
defaultConverterMap.put(java.sql.Timestamp.class, DateConverter.INSTANCE);

View File

@ -13,14 +13,11 @@
package org.dromara.hutool.core.convert.impl;
import org.dromara.hutool.core.convert.AbstractConverter;
import org.dromara.hutool.core.convert.ConvertException;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.convert.impl.stringer.BlobStringer;
import org.dromara.hutool.core.convert.impl.stringer.ClobStringer;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.util.CharsetUtil;
import org.dromara.hutool.core.xml.XmlUtil;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
@ -68,9 +65,9 @@ public class StringConverter extends AbstractConverter {
} else if (value instanceof org.w3c.dom.Node) {
return XmlUtil.toStr((org.w3c.dom.Node) value);
} else if (value instanceof java.sql.Clob) {
return clobToStr((java.sql.Clob) value);
return ClobStringer.INSTANCE.apply(value);
} else if (value instanceof java.sql.Blob) {
return blobToStr((java.sql.Blob) value);
return BlobStringer.INSTANCE.apply(value);
} else if (value instanceof Type) {
return ((Type) value).getTypeName();
}
@ -78,42 +75,4 @@ public class StringConverter extends AbstractConverter {
// 其它情况
return convertToStr(value);
}
/**
* Clob字段值转字符串
*
* @param clob {@link java.sql.Clob}
* @return 字符串
* @since 5.4.5
*/
private static String clobToStr(final java.sql.Clob clob) {
Reader reader = null;
try {
reader = clob.getCharacterStream();
return IoUtil.read(reader);
} catch (final java.sql.SQLException e) {
throw new ConvertException(e);
} finally {
IoUtil.closeQuietly(reader);
}
}
/**
* Blob字段值转字符串
*
* @param blob {@link java.sql.Blob}
* @return 字符串
* @since 5.4.5
*/
private static String blobToStr(final java.sql.Blob blob) {
InputStream in = null;
try {
in = blob.getBinaryStream();
return IoUtil.read(in, CharsetUtil.UTF_8);
} catch (final java.sql.SQLException e) {
throw new ConvertException(e);
} finally {
IoUtil.closeQuietly(in);
}
}
}

View File

@ -0,0 +1,60 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.core.convert.impl.stringer;
import org.dromara.hutool.core.convert.ConvertException;
import org.dromara.hutool.core.io.IoUtil;
import org.dromara.hutool.core.util.CharsetUtil;
import java.io.InputStream;
import java.sql.Blob;
import java.util.function.Function;
/**
* Blob转String
*
* @author looly
* @since 6.0.0
*/
public class BlobStringer implements Function<Object, String> {
/**
* 单例
*/
public static ClobStringer INSTANCE = new ClobStringer();
@Override
public String apply(final Object o) {
return blobToStr((Blob) o);
}
/**
* Blob字段值转字符串
*
* @param blob {@link java.sql.Blob}
* @return 字符串
* @since 5.4.5
*/
private static String blobToStr(final java.sql.Blob blob) {
InputStream in = null;
try {
in = blob.getBinaryStream();
return IoUtil.read(in, CharsetUtil.UTF_8);
} catch (final java.sql.SQLException e) {
throw new ConvertException(e);
} finally {
IoUtil.closeQuietly(in);
}
}
}

View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.core.convert.impl.stringer;
import org.dromara.hutool.core.convert.ConvertException;
import org.dromara.hutool.core.io.IoUtil;
import java.io.Reader;
import java.sql.Clob;
import java.util.function.Function;
/**
* Clob转String
*
* @author looly
* @since 6.0.0
*/
public class ClobStringer implements Function<Object, String> {
/**
* 单例
*/
public static ClobStringer INSTANCE = new ClobStringer();
@Override
public String apply(final Object o) {
return clobToStr((Clob) o);
}
/**
* Clob字段值转字符串
*
* @param clob {@link java.sql.Clob}
* @return 字符串
* @since 5.4.5
*/
private static String clobToStr(final Clob clob) {
Reader reader = null;
try {
reader = clob.getCharacterStream();
return IoUtil.read(reader);
} catch (final java.sql.SQLException e) {
throw new ConvertException(e);
} finally {
IoUtil.closeQuietly(reader);
}
}
}

View File

@ -0,0 +1,16 @@
/*
* Copyright (c) 2023 looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
/**
* 针对对象转换为String的封装
*/
package org.dromara.hutool.core.convert.impl.stringer;