Dozer時間変換の問題
Dozer時間変換の問題
Dozerを使用する理由はpo,dtoからvoへの相互転送が不要な場合は1つずつsetし,getはエンティティごとにxmlを1つ構成すると開発効率が低いためである.インスタンスシーン テスト 印刷ログ
直接コードを貼りましょう
JAr maven版
DozerUtils.java
dozer-date.xml
StringToDateConverter.JAvaこのパスはdozer-dadteに構成する.xmlで
Dozerを使用する理由はpo,dtoからvoへの相互転送が不要な場合は1つずつsetし,getはエンティティごとにxmlを1つ構成すると開発効率が低いためである.
@ResponseBody
public 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 po
testEntity 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版
<dependency>
<groupId>net.sf.dozergroupId>
<artifactId>dozerartifactId>
<version>5.5.1version>
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 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 map ( Object source, Class 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 List mapList ( Collection sourceList, Class destinationClass ) {
List destinationList = new ArrayList();
for (Object sourceObject : sourceList) {
T destinationObject = dozer.map(sourceObject, destinationClass);
destinationList.add(destinationObject);
}
return destinationList;
}
}
dozer-date.xml
"1.0" encoding="UTF-8"?>
"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">
"com.gls.test.StringToDateConverter" >
java.lang.String
java.util.Date
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);
}
@Override
public String convertFrom(Date source, String destination) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS");
destination = formatter.format(source);
return destination;
}
@Override
public 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;
}
}