💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
**1. 两个字段类型不一致的实体类** ```java @Data public class Kite { private Date createTime; private String updateTime; } @Data public class KiteDto { private String createTime; private LocalDate updateTime; } ``` **2. 映射接口** ```java @Mapper public interface KiteMapper { KiteMapper INSTANCE = Mappers.getMapper(KiteMapper.class); /** * 使用dateFormat指定日期格式 */ @Mapping(source = "createTime", target = "createTime", dateFormat = "yyyy-MM-dd") @Mapping(source = "updateTime", target = "updateTime", dateFormat = "yyyy-MM-dd") KiteDto toDto(Kite source); } ``` **3. 测试** ```java @Test public void testKiteMapper() { Kite kite = new Kite(); kite.setCreateTime(new Date()); kite.setUpdateTime("2021-10-21"); KiteDto kiteDto = KiteMapper.INSTANCE.toDto(kite); //Kite:Kite(createTime=Fri Oct 15 09:15:35 CST 2021, updateTime=2021-10-21) System.out.println("Kite:" + kite); //KiteDto:KiteDto(createTime=2021-10-15, updateTime=2021-10-21) System.out.println("KiteDto:" + kiteDto); } ``` **4. 查看映射接口被Mapstruct编译后的代码** ```java public class KiteMapperImpl implements KiteMapper { public KiteMapperImpl() { } public KiteDto toDto(Kite source) { if (source == null) { return null; } else { KiteDto kiteDto = new KiteDto(); if (source.getCreateTime() != null) { kiteDto.setCreateTime((new SimpleDateFormat("yyyy-MM-dd")).format(source.getCreateTime())); } if (source.getUpdateTime() != null) { kiteDto.setUpdateTime(LocalDate.parse(source.getUpdateTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd"))); } return kiteDto; } } } ```