参考 : https://blog.youkuaiyun.com/sinat_29774479/article/details/102745582
TestStruce 添加 uses=DateMapper.class
@Mapper(componentModel = "spring",uses=DateMapper.class)
public interface TestStruce {
DateMapper
import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class DateMapper {
public String asString(Date date) {
return date != null ? new SimpleDateFormat("yyyy-MM-dd")
.format(date) : null;
}
public Date asDate(String date) {
try {
return date != null ? new SimpleDateFormat("yyyy-MM-dd")
.parse(date) : null;
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
public Date asDate(Long date) {
try {
return new Date(date);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
END。
本文介绍了一个Spring框架下的日期映射器组件的具体实现,包括将日期转换为字符串及从字符串转换为日期的方法。该组件使用了SimpleDateFormat进行日期格式化,并处理了可能发生的ParseException异常。
1万+

被折叠的 条评论
为什么被折叠?



