在Hibernate的映射文件中,java数据类型与RDBMS对应关系如下:
原始类型
ps:在oracle中,不同长度的number类型我们可以映射为不同的java类型
长度 java类型 1 Boolean 2至4之间 Byte 4至8之间 Integer 8以上 Long
日期和时间类型
ps: oralce日期和时间类型映射时,需要注意以下问题:
现有如下Entity:
@Data
public class MaterialPo implements Serializable{
Long id;
String label;
String type;
String imgUrl;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
Date time;
String documentUrl;
Long parentid;
Byte isleaf;
public MaterialPo(){}
}
}
time
映射配置
1、若配置如下:
<property name="time" type="timestamp">
<column name="TIME"/>
</property>
从数据库中获取到的materialPo中time的类型为java.sql.Timestamp
,其展现形式为:
2、若配置如下:
<property name="time" type="date">
<column name="TIME"/>
</property>
从数据库中获取到的materialPo中time的类型为java.sql.date
,其展现形式为:
可以看到以java.sql.date形式获取对象时,会将日期进行截断,若想要展现完整的日期格式,使用Timestamp类型获取,若不想要后面带有的精度,对Timestamp再次进行格式化处理。
eg: 下面是一个本人编写的dozer进行映射javaBean的工具类,其中的传入对象的Date类型的数据是从数据库中通过timstamp
映射配置获取到的,此处注释处做了重新格式化的处理
/**
* @author yanzy
* @description 通用javaBean映射处理工具
* @date 2018-04-13 15:31
* @created by intelliJ IDEA
*/
@Component
public class MapperUtil {
public static Object po2Dto(Object S,String targetClassName) throws ClassNotFoundException, IllegalAccessException, ParseException, NoSuchFieldException {
Assert.notNull(S,String.format("------->SourceClass %s is null",S.getClass().getName()));
Mapper mapper = new DozerBeanMapper();
Object targetClass = mapper.map(S, Class.forName(targetClassName));
Field[] fields = S.getClass().getDeclaredFields();
//处理映射过程中的date类型数据的展示形式:
// 去除通过Hibernate从数据库中去出Date类型数据映射成的java.sql.Timestamp存在精度,使得展示结果后面带有`.0`的问题
for(Field field : fields){
if(field.getType() == java.util.Date.class){
SimpleDateFormat formmatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formatTimeString = null;
field.setAccessible(true);
if (field.get(S)!=null) {
formatTimeString = formmatter.format(field.get(S));
}
try {
Field timeField = targetClass.getClass().getDeclaredField("time");
timeField.setAccessible(true);
timeField.set(targetClass,formatTimeString);
} catch (Exception e){
}
}
}
return targetClass;
}
}
二进制和大型数据对象
JDK相关类型
oracle字段 Hibernate映射类型 java类型
Item | Value | Qty |
---|---|---|
Computer | $1600 | 5 |
Phone | $12 | 12 |
Pipe | $1 | 234 |