如何将推土机的数据类型映射到不同的数据类型?(How to map with dozer a data type to different data type?)

编程入门 行业动态 更新时间:2024-10-25 00:36:12
如何将推土机的数据类型映射到不同的数据类型?(How to map with dozer a data type to different data type?)

我有一个User和UserDTO类,但在dto类我不想用户LocalDateTime,我想将其转换为长类型。 (因为protobuf不支持日期)。 所以在代码中:

我的用户实体类:

public class User { private String name,password; private LocalDateTime date; //getters-setters, tostring.. }

我的DTO:

public class UserDTO { private String name,password; private long date; //getters-setters, tostring.. }

你会看到实体User中的日期是LocalDateTime,而DTO中的日期很长。 我想使用这个dozermapper:

UserDTO destObject = mapper.map(user, UserDTO.class);

LocalDateTime将代码更改为long:

private static long setDateToLong(LocalDateTime date) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); String dateString = date.format(formatter); return Long.parseLong(dateString); }

映射器是否可能知道将LocalDateTime更改为long? 我可以以某种方式配置它吗? 感谢帮助!

I have a User and UserDTO class but in dto class i dont want to user LocalDateTime, i want to convert it to long type. (because protobuf not support date). So in code:

My User entity class:

public class User { private String name,password; private LocalDateTime date; //getters-setters, tostring.. }

My DTO:

public class UserDTO { private String name,password; private long date; //getters-setters, tostring.. }

And you see that the date in entity User is LocalDateTime, and in DTO is long. I want to use this dozermapper:

UserDTO destObject = mapper.map(user, UserDTO.class);

The LocalDateTime changing code to long:

private static long setDateToLong(LocalDateTime date) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); String dateString = date.format(formatter); return Long.parseLong(dateString); }

Is it possible that the mapper knows that change the LocalDateTime to long? Can i somehow configure it? Thanks for the helps!

最满意答案

最后我发现了一个从LocalDateTime创建到String的溶剂,并从String返回到LocalDateTime。 我必须创建一个转换器:

public class DozerConverter implements CustomConverter { @Override public Object convert(Object destination, Object source, Class destClass, Class sourceClass) { DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; if(source instanceof String) { String sourceTime = (String) source; return LocalDateTime.parse(sourceTime, formatter); } else if (source instanceof LocalDateTime) { LocalDateTime sourceTime = (LocalDateTime) source; return sourceTime.toString(); } return null; }

}

在我的自定义XML中,我必须添加像这样的自定义转换器属性:

<?xml version="1.0" encoding="UTF-8"?> <mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd"> <mapping> <class-a>mypackage.UserDTO</class-a> <class-b>mypackage.User</class-b> <field custom-converter="mypackage.DozerConverter"> <a>lastLoggedInTime</a> <b>lastLoggedInTime</b> </field> </mapping> </mappings>

我认为它可以与任何数据类型一起工作,只需要编写更多的转换器,或者将此转换器编写为智能。

Finally i found a sollution which is create from LocalDateTime to String and back from String to LocalDateTime. I have to create a converter:

public class DozerConverter implements CustomConverter { @Override public Object convert(Object destination, Object source, Class destClass, Class sourceClass) { DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; if(source instanceof String) { String sourceTime = (String) source; return LocalDateTime.parse(sourceTime, formatter); } else if (source instanceof LocalDateTime) { LocalDateTime sourceTime = (LocalDateTime) source; return sourceTime.toString(); } return null; }

}

And in my custom xml i have to add the custom-converter attribute like this one:

<?xml version="1.0" encoding="UTF-8"?> <mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd"> <mapping> <class-a>mypackage.UserDTO</class-a> <class-b>mypackage.User</class-b> <field custom-converter="mypackage.DozerConverter"> <a>lastLoggedInTime</a> <b>lastLoggedInTime</b> </field> </mapping> </mappings>

I think it can work with any data types, just have to write more converter, or write this converter smart.

更多推荐

LocalDateTime,long,date,UserDTO,class,电脑培训,计算机培训,IT培训"/> <meta na

本文发布于:2023-08-06 00:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1442049.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据类型   推土机   如何将   map   data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!