解决处理时间戳类型字段时精度丢失的BUG;

This commit is contained in:
datagear 2018-12-02 17:01:14 +08:00
parent 5104bf2797
commit 74750a0645
13 changed files with 180 additions and 104 deletions

View File

@ -1320,10 +1320,15 @@ public class AbstractModelDataAccessObject extends AbstractDataAccessObject
{
int jdbcTypeValue = jdbcType.getValue(propertyModelMapper.getIndex());
if (Types.BIT == jdbcTypeValue || Types.TINYINT == jdbcTypeValue
|| Types.SMALLINT == jdbcTypeValue || Types.INTEGER == jdbcTypeValue
|| Types.BIGINT == jdbcTypeValue || Types.NUMERIC == jdbcTypeValue
|| Types.CHAR == jdbcTypeValue || Types.VARCHAR == jdbcTypeValue)
if (Types.BIGINT == jdbcTypeValue || Types.BIT == jdbcTypeValue
|| Types.BOOLEAN == jdbcTypeValue || Types.CHAR == jdbcTypeValue
|| Types.DATE == jdbcTypeValue || Types.DECIMAL == jdbcTypeValue
|| Types.DOUBLE == jdbcTypeValue || Types.FLOAT == jdbcTypeValue
|| Types.INTEGER == jdbcTypeValue || Types.NULL == jdbcTypeValue
|| Types.NUMERIC == jdbcTypeValue || Types.REAL == jdbcTypeValue
|| Types.SMALLINT == jdbcTypeValue || Types.TIME == jdbcTypeValue
|| Types.TIMESTAMP == jdbcTypeValue || Types.TINYINT == jdbcTypeValue
|| Types.VARCHAR == jdbcTypeValue)
{
add = true;
}

View File

@ -4,9 +4,6 @@
package org.datagear.web.format;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
@ -20,72 +17,11 @@ import org.springframework.format.Formatter;
*/
public abstract class AbstractDateFormatter<T extends Date> implements Formatter<T>
{
private int printStyle = DateFormat.MEDIUM;
public AbstractDateFormatter()
{
super();
}
public int getPrintStyle()
{
return printStyle;
}
public void setPrintStyle(int printStyle)
{
this.printStyle = printStyle;
}
/**
* 获取解析的格式
* 获取解析的格式
*
* @param locale
* @return
*/
public String getParsePattern(Locale locale)
{
DateFormat dateFormat = getParseDateFormat(locale);
if (dateFormat instanceof SimpleDateFormat)
return ((SimpleDateFormat) dateFormat).toPattern();
else
return "";
}
@Override
public String print(T object, Locale locale)
{
return getParseDateFormat(locale).format(object);
}
/**
* 将文本解析为{@linkplain Date}
*
* @param text
* @param locale
* @return
* @throws ParseException
*/
protected Date parseToDate(String text, Locale locale) throws ParseException
{
DateFormat dateFormat = getParseDateFormat(locale);
try
{
return dateFormat.parse(text);
}
catch (Exception e)
{
throw new ParseException(text, 0);
}
}
/**
* 获取解析类
*
* @param locale
* @return
*/
protected abstract DateFormat getParseDateFormat(Locale locale);
public abstract String getParsePattern(Locale locale);
}

View File

@ -6,6 +6,7 @@ package org.datagear.web.format;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
@ -17,18 +18,87 @@ import java.util.Locale;
*/
public class DateFormatter extends AbstractDateFormatter<Date>
{
private int printStyle = DateFormat.MEDIUM;
private String candidateParsePattern = "yyyy-MM-dd";
public DateFormatter()
{
super();
}
@Override
public Date parse(String text, Locale locale) throws ParseException
public int getPrintStyle()
{
return parseToDate(text, locale);
return printStyle;
}
public void setPrintStyle(int printStyle)
{
this.printStyle = printStyle;
}
public String getCandidateParsePattern()
{
return candidateParsePattern;
}
public void setCandidateParsePattern(String candidateParsePattern)
{
this.candidateParsePattern = candidateParsePattern;
}
@Override
public String print(Date object, Locale locale)
{
return getParseDateFormat(locale).format(object);
}
@Override
public String getParsePattern(Locale locale)
{
DateFormat dateFormat = getParseDateFormat(locale);
if (dateFormat instanceof SimpleDateFormat)
return ((SimpleDateFormat) dateFormat).toPattern();
else
return candidateParsePattern;
}
@Override
public Date parse(String text, Locale locale) throws ParseException
{
try
{
return parseToDate(text, locale);
}
catch (ParseException e)
{
return new SimpleDateFormat(this.candidateParsePattern).parse(text);
}
}
/**
* 将文本解析为{@linkplain Date}
*
* @param text
* @param locale
* @return
* @throws ParseException
*/
protected Date parseToDate(String text, Locale locale) throws ParseException
{
DateFormat dateFormat = getParseDateFormat(locale);
try
{
return dateFormat.parse(text);
}
catch (Exception e)
{
throw new ParseException(text, 0);
}
}
protected DateFormat getParseDateFormat(Locale locale)
{
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);

View File

@ -5,7 +5,6 @@
package org.datagear.web.format;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Locale;
@ -17,6 +16,8 @@ import java.util.Locale;
*/
public class SqlDateFormatter extends AbstractDateFormatter<Date>
{
public static final String PATTERN = "yyyy-MM-dd";
public SqlDateFormatter()
{
super();
@ -25,16 +26,25 @@ public class SqlDateFormatter extends AbstractDateFormatter<Date>
@Override
public Date parse(String text, Locale locale) throws ParseException
{
java.util.Date date = parseToDate(text, locale);
return new Date(date.getTime());
try
{
return Date.valueOf(text);
}
catch (Exception e)
{
throw new ParseException(text, 0);
}
}
@Override
protected DateFormat getParseDateFormat(Locale locale)
public String print(Date object, Locale locale)
{
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
return object.toString();
}
return dateFormat;
@Override
public String getParsePattern(Locale locale)
{
return PATTERN;
}
}

View File

@ -5,9 +5,7 @@
package org.datagear.web.format;
import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
/**
@ -18,6 +16,8 @@ import java.util.Locale;
*/
public class SqlTimeFormatter extends AbstractDateFormatter<Time>
{
public static final String PATTERN = "hh:mm:ss";
public SqlTimeFormatter()
{
super();
@ -26,16 +26,25 @@ public class SqlTimeFormatter extends AbstractDateFormatter<Time>
@Override
public Time parse(String text, Locale locale) throws ParseException
{
Date date = parseToDate(text, locale);
return new Time(date.getTime());
try
{
return Time.valueOf(text);
}
catch (Exception e)
{
throw new ParseException(text, 0);
}
}
@Override
protected DateFormat getParseDateFormat(Locale locale)
public String print(Time object, Locale locale)
{
DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
return object.toString();
}
return dateFormat;
@Override
public String getParsePattern(Locale locale)
{
return PATTERN;
}
}

View File

@ -5,9 +5,7 @@
package org.datagear.web.format;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
/**
@ -18,6 +16,8 @@ import java.util.Locale;
*/
public class SqlTimestampFormatter extends AbstractDateFormatter<Timestamp>
{
public static final String PATTERN = "yyyy-MM-dd hh:mm:ss[.fff...]";
public SqlTimestampFormatter()
{
super();
@ -26,16 +26,25 @@ public class SqlTimestampFormatter extends AbstractDateFormatter<Timestamp>
@Override
public Timestamp parse(String text, Locale locale) throws ParseException
{
Date date = parseToDate(text, locale);
return new Timestamp(date.getTime());
try
{
return Timestamp.valueOf(text);
}
catch (Exception e)
{
throw new ParseException(text, 0);
}
}
@Override
protected DateFormat getParseDateFormat(Locale locale)
public String print(Timestamp object, Locale locale)
{
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
return object.toString();
}
return dateFormat;
@Override
public String getParsePattern(Locale locale)
{
return PATTERN;
}
}

View File

@ -53,10 +53,6 @@ public class FastJsonConfigFactory
this.objectSerializerMap.put(java.sql.Timestamp.class, new LocaleSqlTimestampSerializer());
this.objectSerializerMap.put(java.util.Date.class, new LocaleDateSerializer());
this.objectSerializerMap.put(Class.class, new ClassSerializer(true));
// this.objectSerializerMap.put(DefaultDynamicBean.class, new
// DynamicBeanObjectSerializer());
// this.objectSerializerMap.put(DynamicBean.class, new
// DynamicBeanObjectSerializer());
this.objectSerializerMap.put(SizeOnlyList.class, new SizeOnlyCollectionSerializer());
this.objectSerializerMap.put(SizeOnlySet.class, new SizeOnlyCollectionSerializer());
this.objectSerializerMap.put(SizeOnlyQueue.class, new SizeOnlyCollectionSerializer());

View File

@ -21,13 +21,19 @@ import com.alibaba.fastjson.serializer.ObjectSerializer;
*/
public class LocaleDateSerializer implements ObjectSerializer
{
private DateFormatter dateFormatter = new DateFormatter();
private DateFormatter dateFormatter;
public LocaleDateSerializer()
{
super();
}
public LocaleDateSerializer(DateFormatter dateFormatter)
{
super();
this.dateFormatter = dateFormatter;
}
public DateFormatter getDateFormatter()
{
return dateFormatter;

View File

@ -21,13 +21,19 @@ import com.alibaba.fastjson.serializer.ObjectSerializer;
*/
public class LocaleSqlDateSerializer implements ObjectSerializer
{
private SqlDateFormatter sqlDateFormatter = new SqlDateFormatter();
private SqlDateFormatter sqlDateFormatter;
public LocaleSqlDateSerializer()
{
super();
}
public LocaleSqlDateSerializer(SqlDateFormatter sqlDateFormatter)
{
super();
this.sqlDateFormatter = sqlDateFormatter;
}
public SqlDateFormatter getSqlDateFormatter()
{
return sqlDateFormatter;

View File

@ -21,13 +21,19 @@ import com.alibaba.fastjson.serializer.ObjectSerializer;
*/
public class LocaleSqlTimeSerializer implements ObjectSerializer
{
private SqlTimeFormatter sqlTimeFormatter = new SqlTimeFormatter();
private SqlTimeFormatter sqlTimeFormatter;
public LocaleSqlTimeSerializer()
{
super();
}
public LocaleSqlTimeSerializer(SqlTimeFormatter sqlTimeFormatter)
{
super();
this.sqlTimeFormatter = sqlTimeFormatter;
}
public SqlTimeFormatter getSqlTimeFormatter()
{
return sqlTimeFormatter;

View File

@ -22,13 +22,19 @@ import com.alibaba.fastjson.serializer.ObjectSerializer;
*/
public class LocaleSqlTimestampSerializer implements ObjectSerializer
{
private SqlTimestampFormatter sqlTimestampFormatter = new SqlTimestampFormatter();
private SqlTimestampFormatter sqlTimestampFormatter;
public LocaleSqlTimestampSerializer()
{
super();
}
public LocaleSqlTimestampSerializer(SqlTimestampFormatter sqlTimestampFormatter)
{
super();
this.sqlTimestampFormatter = sqlTimestampFormatter;
}
public SqlTimestampFormatter getSqlTimestampFormatter()
{
return sqlTimestampFormatter;

View File

@ -298,6 +298,18 @@
<property name="sqlExpressionResolver" ref="sqlExpressionResolver" />
</bean>
<bean id="localeDateSerializer" class="org.datagear.web.json.fastjson.LocaleDateSerializer">
<property name="dateFormatter" ref="dateFormatter" />
</bean>
<bean id="localeSqlDateSerializer" class="org.datagear.web.json.fastjson.LocaleSqlDateSerializer">
<property name="sqlDateFormatter" ref="sqlDateFormatter" />
</bean>
<bean id="localeSqlTimeSerializer" class="org.datagear.web.json.fastjson.LocaleSqlTimeSerializer">
<property name="sqlTimeFormatter" ref="sqlTimeFormatter" />
</bean>
<bean id="localeSqlTimestampSerializer" class="org.datagear.web.json.fastjson.LocaleSqlTimestampSerializer">
<property name="sqlTimestampFormatter" ref="sqlTimestampFormatter" />
</bean>
<bean id="fileSerializer" class="org.datagear.web.json.fastjson.FileSerializer">
<property name="directory" ref="blobFileManagerDirectory" />
<property name="deleteHeadSeparator" value="true" />
@ -305,6 +317,10 @@
<bean id="fastJsonConfigFactory" class="org.datagear.web.json.fastjson.FastJsonConfigFactory">
<property name="objectSerializerStringMap">
<map>
<entry key="java.util.Date" value-ref="localeDateSerializer" />
<entry key="java.sql.Date" value-ref="localeSqlDateSerializer" />
<entry key="java.sql.Time" value-ref="localeSqlTimeSerializer" />
<entry key="java.sql.Timestamp" value-ref="localeSqlTimestampSerializer" />
<entry key="java.io.File" value-ref="fileSerializer" />
</map>
</property>

View File

@ -18,4 +18,5 @@
新增视图数据管理功能;
新增添加操作时的批量执行功能;
数据删除操作改为弹出确认对话框而非双击直接删除;
解决处理时间戳类型字段时精度丢失的BUG
优化表结构加载效率;