衡水做网站哪家好,网页设计尺寸一般是多少,南通优普网站建设团队,深圳市住房和建设局官网房源MapStruct从0到0.5 
开发的过程#xff0c;经常会用到实体类属性映射#xff0c;同时为了方便#xff0c;开发者也很少自己写专门的属性赋值工具类。索性会直接使用Sprrng提供的BeanUtils工具类#xff0c;然后在性能上和字段属性赋值上的问题#xff0c;一直是为开发者所…MapStruct从0到0.5 
开发的过程经常会用到实体类属性映射同时为了方便开发者也很少自己写专门的属性赋值工具类。索性会直接使用Sprrng提供的BeanUtils工具类然后在性能上和字段属性赋值上的问题一直是为开发者所诟病的下面直接上案例。 
1、引入依赖 注意lombok引入一定要在mapstruct依赖的前面。否则会出现无法找到Setter和Getter方法的异常出现。 dependenciesdependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency!-- mapstruct --dependencygroupIdorg.mapstruct/groupIdartifactIdmapstruct-jdk8/artifactIdversion1.5.3.Final/version/dependencydependencygroupIdorg.mapstruct/groupIdartifactIdmapstruct-processor/artifactIdversion1.5.3.Final/version/dependencydependencygroupIdorg.mapstruct/groupIdartifactIdmapstruct/artifactIdversion1.5.3.Final/version/dependency
/dependencies2、编写案例 
引入依赖后则进行日常较为常用的案例测试首先附上基础代码 User.java 
Data
public class User {private Long id;/*** 情况一时间字符串转时间对象*/private String birthDay;/*** 时间字符串转时间对象*/private Date happenDay;/*** 情况二使用工具类指定方法进行处理字段*/private String ids;/*** 情况三忽略字段*/private String ignore;/*** 情况四字段对应*/private String label;
}UserVo.java 
Data
public class UserVo {private Long id;/*** 情况一时间字符串转时间对象*/private Date birthDay;/*** 时间对象转时间字符串*/private String happenDay;/*** 情况二使用工具类指定方法进行处理字段*/private ListString ids;/*** 情况三忽略字段*/private String ignore;/*** 情况四字段对应*/private String value;
} **注意**这里的Mapper注解在mapstruct包下别与myBatis中的注解混淆。 UserMapper.java 
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;Mapper
public interface UserMapper {UserMapper INSTANCE  Mappers.getMapper(UserMapper.class);Mapping(target  birthDay, dateFormat  yyyy-MM-dd)Mapping(target  happenDay, dateFormat  yyyy-MM-dd)Mapping(source  label, target  value)Mapping(target  ignore, ignore  true)Mapping(target  ids, expression  java(ConverterUtil.strToList(user.getIds())))UserVo UserToUserVo(User user);
}ConverterUtil.java 
public class ConverterUtil {public static ListString strToList(String str){return Arrays.asList(str.split(,));}
} 
MapStructTest.java 
public class MapStructTest {public static void main(String[] args) {// 1、创造属性User user  new User();user.setId(1L);user.setBirthDay(2023-08-29);user.setHappenDay(new Date());user.setIds(11,22,33);user.setIgnore(这是一个忽略字段);user.setLabel(将label映射到value上);// 2、测试效果UserVo userVo  UserMapper.INSTANCE.UserToUserVo(user);System.out.println(userVo);}
}下面打印显示结果 
UserVo(id1, birthDayTue Aug 29 00:00:00 CST 2023, happenDay2023-09-09, 
ids[11, 22, 33], ignorenull, value将label映射到value上)Process finished with exit code 03、详细说明 
1、Mapping和Mappings 
Mapping(target  birthDay, dateFormat  yyyy-MM-dd)
Mapping(target  happenDay, dateFormat  yyyy-MM-dd)
Mapping(source  label, target  value)
Mapping(target  ignore, ignore  true)
Mapping(target  ids, expression  java(ConverterUtil.strToList(user.getIds())))
UserVo UserToUserVo(User user);上文中多个Mapping也可以替换为Mapings写法 
Mappings({Mapping(target  birthDay, dateFormat  yyyy-MM-dd),Mapping(target  happenDay, dateFormat  yyyy-MM-dd),Mapping(source  label, target  value),Mapping(target  ignore, ignore  true),Mapping(target  ids, expression java(ConverterUtil.strToList(user.getIds())))})
UserVo UserToUserVo(User user);2、dateFormat 时间类型Date.class和字符串的相互转换 
无论是时间字符串转时间对象还是时间对象转时间字符串都需要补充转换的时间格式 
// birthDay有 string 转 date
Mapping(target  birthDay, dateFormat  yyyy-MM-dd)
// happenDay有 date 转 string
Mapping(target  happenDay, dateFormat  yyyy-MM-dd)3、ignore 字段忽略 
// boolean ignore() default false;
Mapping(target  ignore, ignore  true)4、编写Java方法表达式使用自定义工具类进行字段值转变 
ConverterUtil .class工具类 
public class ConverterUtil {public static ListString strToList(String str){return Arrays.asList(str.split(,));}
}使用格式 
Mapping(target  ids, expression  java(ConverterUtil.strToList(user.getIds())))
UserVo UserToUserVo(User user);5、字段映射 
// User的label  映射到 UserVo的value
Mapping(source  label, target  value)
UserVo UserToUserVo(User user);6、设置属性为常量或者默认值 
// 设置常量
Mapping(source  id, target  id, constant  -1L)
UserVo UserToUserVo(User user);// 设置默认值
Mapping(source  id, target  id, defaultValue 1024L)
UserVo UserToUserVo(User user);4、后续 
以上就是笔者目前在项目所用的一些记实如果想了解更多笔者找到一份比较详细的文章用来参考 MapStruct之Mapping注解的用法 官方文档 文章转载自: http://www.morning.banzou2034.cn.gov.cn.banzou2034.cn http://www.morning.qwnqt.cn.gov.cn.qwnqt.cn http://www.morning.fnnkl.cn.gov.cn.fnnkl.cn http://www.morning.rzsxb.cn.gov.cn.rzsxb.cn http://www.morning.zmwzg.cn.gov.cn.zmwzg.cn http://www.morning.dysgr.cn.gov.cn.dysgr.cn http://www.morning.wtnwf.cn.gov.cn.wtnwf.cn http://www.morning.djxnw.cn.gov.cn.djxnw.cn http://www.morning.qnbzs.cn.gov.cn.qnbzs.cn http://www.morning.rykmf.cn.gov.cn.rykmf.cn http://www.morning.rhjsx.cn.gov.cn.rhjsx.cn http://www.morning.dcpbk.cn.gov.cn.dcpbk.cn http://www.morning.dmnqh.cn.gov.cn.dmnqh.cn http://www.morning.mmhaoma.com.gov.cn.mmhaoma.com http://www.morning.rjxwq.cn.gov.cn.rjxwq.cn http://www.morning.nffwl.cn.gov.cn.nffwl.cn http://www.morning.krlsz.cn.gov.cn.krlsz.cn http://www.morning.ksjnl.cn.gov.cn.ksjnl.cn http://www.morning.tqrjj.cn.gov.cn.tqrjj.cn http://www.morning.jlxqx.cn.gov.cn.jlxqx.cn http://www.morning.mdwtm.cn.gov.cn.mdwtm.cn http://www.morning.nlkjq.cn.gov.cn.nlkjq.cn http://www.morning.ljzqb.cn.gov.cn.ljzqb.cn http://www.morning.cknws.cn.gov.cn.cknws.cn http://www.morning.lftpl.cn.gov.cn.lftpl.cn http://www.morning.fxpyt.cn.gov.cn.fxpyt.cn http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.hkpn.cn.gov.cn.hkpn.cn http://www.morning.tsrg.cn.gov.cn.tsrg.cn http://www.morning.tdnbw.cn.gov.cn.tdnbw.cn http://www.morning.rcjqgy.com.gov.cn.rcjqgy.com http://www.morning.hwxxh.cn.gov.cn.hwxxh.cn http://www.morning.tpqrc.cn.gov.cn.tpqrc.cn http://www.morning.yqfdl.cn.gov.cn.yqfdl.cn http://www.morning.qmwzr.cn.gov.cn.qmwzr.cn http://www.morning.dkqyg.cn.gov.cn.dkqyg.cn http://www.morning.wqpr.cn.gov.cn.wqpr.cn http://www.morning.lfjmp.cn.gov.cn.lfjmp.cn http://www.morning.fdxhk.cn.gov.cn.fdxhk.cn http://www.morning.wnkqt.cn.gov.cn.wnkqt.cn http://www.morning.nshhf.cn.gov.cn.nshhf.cn http://www.morning.qflwp.cn.gov.cn.qflwp.cn http://www.morning.mkydt.cn.gov.cn.mkydt.cn http://www.morning.jhswp.cn.gov.cn.jhswp.cn http://www.morning.cmcjp.cn.gov.cn.cmcjp.cn http://www.morning.qkgwz.cn.gov.cn.qkgwz.cn http://www.morning.zljqb.cn.gov.cn.zljqb.cn http://www.morning.wrqw.cn.gov.cn.wrqw.cn http://www.morning.pmysp.cn.gov.cn.pmysp.cn http://www.morning.rbgwj.cn.gov.cn.rbgwj.cn http://www.morning.easiuse.com.gov.cn.easiuse.com http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn http://www.morning.xfmzk.cn.gov.cn.xfmzk.cn http://www.morning.sryyt.cn.gov.cn.sryyt.cn http://www.morning.prjty.cn.gov.cn.prjty.cn http://www.morning.lanyee.com.cn.gov.cn.lanyee.com.cn http://www.morning.knzdt.cn.gov.cn.knzdt.cn http://www.morning.jqbpn.cn.gov.cn.jqbpn.cn http://www.morning.hbxnb.cn.gov.cn.hbxnb.cn http://www.morning.qnbsx.cn.gov.cn.qnbsx.cn http://www.morning.nqrlz.cn.gov.cn.nqrlz.cn http://www.morning.ndhxn.cn.gov.cn.ndhxn.cn http://www.morning.znqxt.cn.gov.cn.znqxt.cn http://www.morning.sbdqy.cn.gov.cn.sbdqy.cn http://www.morning.mlhfr.cn.gov.cn.mlhfr.cn http://www.morning.lztrt.cn.gov.cn.lztrt.cn http://www.morning.khtyz.cn.gov.cn.khtyz.cn http://www.morning.nkwgy.cn.gov.cn.nkwgy.cn http://www.morning.jopebe.cn.gov.cn.jopebe.cn http://www.morning.mqwnz.cn.gov.cn.mqwnz.cn http://www.morning.kxscs.cn.gov.cn.kxscs.cn http://www.morning.ngcw.cn.gov.cn.ngcw.cn http://www.morning.cwwts.cn.gov.cn.cwwts.cn http://www.morning.wpspf.cn.gov.cn.wpspf.cn http://www.morning.xylxm.cn.gov.cn.xylxm.cn http://www.morning.ffcsr.cn.gov.cn.ffcsr.cn http://www.morning.rqxch.cn.gov.cn.rqxch.cn http://www.morning.cttgj.cn.gov.cn.cttgj.cn http://www.morning.nlkjq.cn.gov.cn.nlkjq.cn http://www.morning.nafdmx.cn.gov.cn.nafdmx.cn