JRT引用的jackjson作为json处理库。由于JRT.ORM要求表不用datetime类型,把日期和时间用Int存储,所以ORM要支持日期时间的转换。为什么要把日期时间不用datetime而用Int,比如日期:20240117,时间就是从0点到当前的秒数。因为不用datetime兼容性好,不会因为不同库datetime函数不同而要处理差异。用Int查询效率高。
所以需要json序列化到实体时候能把日期和时间串序列化成整数。实体转json时候要能把日期和时间的整数转换成日期和时间串。
日期反序列化
package JRT.Core.JsonAttributes;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
/**
* 把JSON串转换对象,在日期属性上加上下面注解
* @JsonSerialize(using = JRTDateSerializer.class)
* @JsonDeserialize(using = JRTDateDeserializer.class)
*/
public class JRTDateDeserializer extends JsonDeserializer<Integer> {
@Override
public Integer deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
String val = node.asText();
return JRT.Core.Util.TimeParser.TransDate(val);
}
}
日期序列化
package JRT.Core.JsonAttributes;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
/**
* 把对象转换成JSON串,在日期属性上加上下面注解
* @JsonSerialize(using = JRTDateSerializer.class)
* @JsonDeserialize(using = JRTDateDeserializer.class)
*/
public class JRTDateSerializer extends JsonSerializer<Integer> {
@Override
public void serialize(Integer dateInt, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(JRT.Core.Util.TimeParser.DateIntToStr(dateInt));
}
}
时间反序列化
package JRT.Core.JsonAttributes;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
/**
* 把JSON串转换对象,在时间属性上加上下面注解
* @JsonSerialize(using = JRTDateSerializer.class)
* @JsonDeserialize(using = JRTDateDeserializer.class)
*/
public class JRTTimeDeserializer extends JsonDeserializer<Integer> {
@Override
public Integer deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
String val = node.asText();
return JRT.Core.Util.TimeParser.TransTime(val);
}
}
时间序列化
package JRT.Core.JsonAttributes;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
/**
* 把对象转换成JSON串,在时间属性上加上下面注解
* @JsonSerialize(using = JRTDateSerializer.class)
* @JsonDeserialize(using = JRTDateDeserializer.class)
*/
public class JRTTimeSerializer extends JsonSerializer<Integer> {
@Override
public void serialize(Integer timeStr, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(JRT.Core.Util.TimeParser.TimeIntToStr(timeStr));
}
}
布尔反序列化
package JRT.Core.JsonAttributes;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;
/**
* 把JSON串转换对象,在布尔属性上加上下面注解
* @JsonDeserialize(using = JRTBoolDeserializer.class)
*/
public class JRTBoolDeserializer extends JsonDeserializer<Boolean> {
@Override
public Boolean deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
if(node.isBoolean())
{
Boolean val=node.asBoolean();
return val;
}
else if(node.isInt())
{
int val=node.asInt();
if(val==0)
{
return false;
}
else {
return true;
}
}
else
{
String val = node.asText();
if(val.equals("0"))
{
return false;
}
else if(val.equals("1"))
{
return true;
}
else if(val.equals("false"))
{
return false;
}
else if(val.equals("true"))
{
return true;
}
return false;
}
}
}
由于ORM自己组json时候也需要知道哪些列是日期列,哪些是时间列,哪些是布尔列,所以自己实现了几个注解,这里不用jackjson的注解来判断日期时间就是为了可能变json库做预留,自己可控注解更合适
日期注解
//特性,为通用码表提供支持
package JRT.Core.CustomAttributes;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 标识属性是日期的特性
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DateAttribute {
}
时间注解
//特性,为通用码表提供支持
package JRT.Core.CustomAttributes;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 标识属性是时间的特性
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TimeAttribute {
}
布尔注解
//特性,为通用码表提供支持
package JRT.Core.CustomAttributes;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 标识属性是布尔的特性
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface BoolAttribute {
}
模板表实体
package JRT.Model.Entity;
import JRT.Core.CustomAttributes.*;
import JRT.Core.JsonAttributes.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
/**
*[功能描述:本代码由LIS内部代码生成工具生成,请不要手动修改,如要修改,请写修改变更记录]
*[创建者:JRT.Entity生成器]
*/
@UniqueAttribute(ColNames = "RowID")
@TableAttribute(Name="dbo.JRT_PrintTemplate")
public class JRTPrintTemplate
{
/**
* 主键
*/
@NotNullAttribute
@IdAttribute(Name = "RowID")
@LengthAttribute(MaxLen = 10)
public int RowID;
/**
* 模板代码
*/
@NotNullAttribute
@LengthAttribute(MaxLen = 100)
public String Code;
/**
* 模板名称
*/
@NotNullAttribute
@LengthAttribute(MaxLen = 100)
public String CName;
/**
* 产品组唯一标识
*/
@NotNullAttribute
@LengthAttribute(MaxLen = 30)
public String ProductGroup;
/**
* 产品组存的业务ID
*/
@NotNullAttribute
@LengthAttribute(MaxLen = 30)
public String ProductBllID;
/**
* 模板纸张
*/
@FrekeyAttribute(Name = "JRTPrintPaper", RefColumnName = "RowID", AssociaField = "CName")
@LengthAttribute(MaxLen = 10)
public Integer JRTPrintPaperDR;
/**
* 边距
*/
@LengthAttribute(MaxLen = 10)
public Double ResultLineSpacing;
/**
* 结果行数
*/
@LengthAttribute(MaxLen = 10)
public Integer ResultRows;
/**
* 结果列
*/
@LengthAttribute(MaxLen = 10)
public Integer ResultCols;
/**
* 纸张方向
*/
@LengthAttribute(MaxLen = 10)
public String PaperLayout;
/**
* 下边距
*/
@LengthAttribute(MaxLen = 10)
public Double PaperBottom;
/**
* 左边距
*/
@LengthAttribute(MaxLen = 10)
public Double PaperLeft;
/**
* 右边距
*/
@LengthAttribute(MaxLen = 10)
public Double PaperRight;
/**
* 上边距
*/
@LengthAttribute(MaxLen = 10)
public Double PaperTop;
/**
* 微生物边距
*/
@LengthAttribute(MaxLen = 10)
public Double MicResultLineSpacing;
/**
* 微生物行数
*/
@LengthAttribute(MaxLen = 10)
public Integer MicResultRows;
/**
* 微生物列数
*/
@LengthAttribute(MaxLen = 10)
public Integer MicResultCols;
/**
* 边距
*/
@LengthAttribute(MaxLen = 10)
public Double ArchivesMargin;
/**
* 双列填充类型
*/
@LengthAttribute(MaxLen = 10)
public String DoubleColFillType;
/**
* 序号
*/
@LengthAttribute(MaxLen = 10)
public Integer Sequence;
/**
* 父模板
*/
@FrekeyAttribute(Name = "JRTPrintTemplate", RefColumnName = "RowID", AssociaField = "CName")
@LengthAttribute(MaxLen = 10)
public Integer ParentTemplateDR;
/**
* 截止日期
*/
@JsonSerialize(using = JRTDateSerializer.class)
@JsonDeserialize(using = JRTDateDeserializer.class)
@DateAttribute
@LengthAttribute(MaxLen = 10)
public Integer EndDate;
/**
* 截止时间
*/
@JsonSerialize(using = JRTTimeSerializer.class)
@JsonDeserialize(using = JRTTimeDeserializer.class)
@TimeAttribute
@LengthAttribute(MaxLen = 10)
public Integer EndTime;
/**
* 截止说明
*/
@LengthAttribute(MaxLen = 50)
public String EndRemark;
}
JRT的完善持续推进中,抽取任何空余时间来打磨