Dozer 时间转换问题

使用Dozer的原因 是因为po、dto转为vo 的互转
不用的话 就要一个个set,get
如果每个实体都要配置一个xml 的话开发效率很低

  • 实例 场景 (就是这么方便)
  @ResponseBodypublic RtnResult findPageOrderByWhere(@RequestBody FinancingOrderVo vo) {FinancingOrder forder=DozerUtils.map(vo, FinancingOrder.class);......................... 
  • 测试
testEntityVo vo = new testEntityVo();vo.setTdDate("2019-05-25 09:23:45:00");vo.setBytes("1");vo.setBmoney("100.11");vo.setMun("100");vo.setCreaterId("1000");vo.setName("dozer");// vo 转换成potestEntity o = DozerUtils.map(vo, testEntity.class);//转换testEntityVo os = DozerUtils.map(o, testEntityVo.class);System.out.println(o.toJson());System.out.println(os.toJson());
  • 打印日志
{"bmoney":100.11,"bytes":1,"createrId":1000,"mun":100,"name":"dozer","tdDate":1558747425000}{"bmoney":"100.11","bytes":"1","createrId":"1000","mun":"100","name":"dozer","tdDate":"2019-05-25 09:23:45:00"}

直接贴代码吧

jar maven 版

<!-- dozer --><dependency><groupId>net.sf.dozer</groupId><artifactId>dozer</artifactId><version>5.5.1</version></dependency>

DozerUtils.java

package com;import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;import org.dozer.DozerBeanMapper;/*** * @className: DozerUtils* @description: DTO/VO/DO等之间的转换* **/public class DozerUtils {/*** 持有Dozer单例, 避免重复创建DozerMapper消耗资源.*/private static DozerBeanMapper dozer;static {if (dozer == null) {dozer = new DozerBeanMapper();List<String> mappingFileUrls = Arrays.asList("dozer/dozer-date.xml");dozer.setMappingFiles(mappingFileUrls);}}/*** * @title: map* @description: 单个对象相互转换** @param source 源对象* @param destinationClass 目标对象* @return* @date 2017年11月8日 下午6:08:54*/public static <T> T map ( Object source, Class<T> destinationClass ) {return dozer.map(source, destinationClass);}/*** * @title: mapList* @description: 集合对象相互转换** @param sourceList* @param destinationClass* @return* @date 2017年11月8日 下午6:09:41*/@SuppressWarnings("rawtypes")public static <T> List<T> mapList ( Collection sourceList, Class<T> destinationClass ) {List<T> destinationList = new ArrayList<T>();for (Object sourceObject : sourceList) {T destinationObject = dozer.map(sourceObject, destinationClass);destinationList.add(destinationObject);}return destinationList;}}

dozer-date.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.nethttp://dozer.sourceforge.net/schema/beanmapping.xsd"><configuration><custom-converters> <!-- these are always bi-directional --><converter type="com.gls.test.StringToDateConverter" ><class-a>java.lang.String</class-a><class-b>java.util.Date</class-b></converter></custom-converters>     </configuration></mappings>

StringToDateConverter.java 这个路径要配置在dozer-dadte.xml 中

package com.gls.test;import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;import org.dozer.DozerConverter;public class StringToDateConverter extends DozerConverter<String, Date> {public StringToDateConverter() {super(String.class, Date.class);}@Overridepublic String convertFrom(Date source, String destination) {SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS");destination = formatter.format(source);return destination;}@Overridepublic Date convertTo(String source, Date destination) {SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS");ParsePosition pos = new ParsePosition(0);destination = formatter.parse(source, pos);return destination;}}

Dozer 时间转换问题相关推荐

  1. Java中的dozer对象转换

    Java中的dozer对象转换 1.dozer介绍 Dozer是Java Bean到Java Bean映射器,它以递归方式将数据从一个对象复制到另一个对象. dozer是用来对两个对象之间属性转换的工 ...

  2. Go 学习笔记(48)— Go 标准库之 time (获取时/分/秒的单位值、标准时间和Unix时间转换、字符串时间和Time类型转换、时区转换、时间的加减/休眠)

    1. 概要说明 import "time" time 包提供了时间的显示和测量用的函数.日历的计算采用的是公历. Go 提供以下几种时间类型: 时间点 Time 时间段 Durat ...

  3. 题目 1470:【蓝桥杯】【入门题】【基础练习VIP】时间转换

    题目 1470:时间转换 蓝桥杯刷题群已成立,微信后台回复[蓝桥杯],即可进入. 如果加入了之前的社群不需要重复加入. 时间限制: 1Sec 内存限制: 128MB 1. 题目描述 给定一个以秒为单位 ...

  4. java时间转换 YYYY yyyy

    java时间转换,特定时间转换'YYYY',时间加一天 本帖只展示转换效果,原因不详,等待有猿人破解 效果图: 演示代码: import java.text.ParseException; impor ...

  5. Python时间转换函数:时间转化为时间戳、时间戳转化为时间、当前日期、当前时间、星期几、前面或者后面多少天、年、月、日等

    Python时间转换函数:时间转化为时间戳.时间戳转化为时间.当前日期.当前时间.星期几.前面或者后面多少天.年.月.日等 #Python时间转换函数:时间转化为时间戳.时间戳转化为时间.当前日期.当 ...

  6. python【蓝桥杯vip练习题库】BASIC-14 时间转换(取余 水题)

    试题 基础练习 时间转换资源限制 时间限制:1.0s 内存限制:512.0MB 问题描述给定一个以秒为单位的时间t,要求用"<H>:<M>:<S>&quo ...

  7. scrapy笔记——python的时间转换

    1 import datetime 2 GMT_FORMAT = '%M %H %d %m %w' 3 datetime.datetime.utcnow().strftime(GMT_FORMAT) ...

  8. C语言基础:时间转换成字符串 strftime的代码

    将内容过程中经常用的一些内容段做个记录,下边内容段是关于C语言基础:时间转换成字符串 strftime的内容,希望能对大伙有一些用处. #include <stdio.h> #includ ...

  9. 北斗导航 | 卫星导航系统时间转换:闰年(附C源代码)

    ================================================ 博主github:https://github.com/MichaelBeechan 博主CSDN:h ...

最新文章

  1. 创建安卓模拟器的两种方式及常用Android命令介绍
  2. android core log,Android 日志系统(Logcat)的实现分析
  3. ReportViewer教程(15)-矩阵报表-6
  4. 计算机复试上机辅导班,软件工程考研辅导班:考研院校2020年计算机/软件工程复试经验总结...
  5. 小心避坑:MySQL分页时使用 limit+order by 会出现数据重复问题
  6. vue 和react
  7. Kotlin入门(20)几种常见的对话框
  8. ubuntu常用软件安装集合:360浏览器、QQ--持续更新
  9. bootstrap 图标和文字对齐
  10. 【单目标优化求解】基于matlab混沌算法求解单目标问题【含Matlab源码 1410期】
  11. Linux sed命令实例
  12. Tensorflow2.0学习-加载和预处理数据 (七)
  13. Docker——Dockerfile构建镜像
  14. 网站被攻击如何修复网站漏洞
  15. 【历史上的今天】7 月 29 日:Win10 七周年;微软和雅虎的搜索协议;微软发行 NT 4.0
  16. Deepin安装应用
  17. 怎样删除Github中的项目
  18. CoreData的使用
  19. Unity中的网络编程
  20. 逐行拆解Guava限流器RateLimiter

热门文章

  1. 气泡变金属泡 挖掘Windows 7隐藏绚丽屏保
  2. 电磁场与电磁波(一)
  3. 文献阅读-ICRA2020-从单眼内窥镜图像中对手术机器人器械的柄姿势估计
  4. 华三交换机升级的ipe文件_H3C设备升级
  5. 木头人的伤感空间日志发布:再美只是瞬间而已~~~
  6. 22考研:考取985、211名校的忠告!
  7. 扁平商务工作汇报PPT模板
  8. 影响辐射生物学效应的因素
  9. 服务器内存型号2400,S26361-F3934-E511 8GB 1Rx4 PC4-2400 RX2540M2服务器内存
  10. 协程:协程的创建和使用。