日期转换器
定义日期Mapper
public class DateMapper {
public String toString(Date date) {
return date != null ? new SimpleDateFormat("yyyy-MM-dd").format(date) : null;
}
public Date toDate(String date) {
try {
return date != null ? new SimpleDateFormat("yyyy-MM-dd").parse(date) : null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
定义对象转换Mapper
/**
* 自定义日期格式转换映射器
* uses = DateMapper.class
*/
@Mapper(uses = DateMapper.class,componentModel="spring")
public interface User5Mapper extends BaseMapper<UserDTO, UserVO1> {
}
对象中的 Date 字段转换成不同格式的时间文本,比如转换成 yyyy-MM-dd 和 yyyy/MM/dd 两种格式
@Named("dateMapper1")
public class DateMapper1 {
public String toString(Date date) {
return date != null ? new SimpleDateFormat("yyyy-MM-dd").format(date) : null;
}
public Date toDate(String date) {
try {
return date != null ? new SimpleDateFormat("yyyy-MM-dd").parse(date) : null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@Named("dateMapper2")
public class DateMapper2 {
public String toString(Date date) {
return date != null ? new SimpleDateFormat("yyyy/MM/dd").format(date) : null;
}
public Date toDate(String date) {
try {
return date != null ? new SimpleDateFormat("yyyy/MM/dd").parse(date) : null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@Mapper(uses = {
DateMapper1.class,
DateMapper2.class
},componentModel="spring")
public interface User6Mapper extends BaseMapper<UserDTO, UserVO1> {
@Mappings({
@Mapping(source = "createTime", target = "createTime", qualifiedByName = {"dateMapper1"}),
@Mapping(source = "updateTime", target = "updateTime", qualifiedByName = {"dateMapper2"})
})
@Override
UserVO1 to(UserDTO var1);
}
数值转换器
private Double wallet;
private String wallet;
@Mapping(source = "wallet", target = "wallet", numberFormat = "$#.00")
BigDecimal转换
private BigDecimal deposit;
private String deposit;
@Mapping(source = "deposit", target = "deposit", numberFormat = "#.##E0")