java 注解 entity_java反射+注解实现Entity类与Dto类相互转换

1 import java.lang.reflect.Field;2 import java.lang.reflect.Method;3 import java.sql.Timestamp;4 import java.util.Date;5 import org.apache.commons.lang.StringUtils;6 import com.ctccbs.common.annotation.RelMapper;7

8 public classRelationMapperUtils {9 /**10 * Entity and Dto Mapper11 * @param entry12 * @param dto13 * @param enToDto14 * ture : Entity To Dto (defult)15 * false : Dto To Entry16 * Rule:17 * 实现相互转换前提: Dto field name(dto和entry的field name相同并且 类上有@RelMapper) 或 field的@RelMapper(value="Entity field name") 满足其一即可转换18 * @return19 * @throws Exception20 */

21 public staticObject entryAndDtoMapper(Object entity, Object dto) throws Exception{22 return EnAndDtoMapper(entity, dto,true);23 }24

25 public staticObject entryAndDtoMapper(Object entity, Object dto,boolean enToDto) throws Exception{26 return EnAndDtoMapper(entity, dto,false);27 }28 //last version

29 public staticObject EnAndDtoMapper(Object entry, Object dto,boolean enToDto) throws Exception{30 if(enToDto == true ? entry == null : dto == null){ return null;}31 Class extends Object> entryclazz = entry.getClass(); //获取entity类

32 Class extends Object> dtoclazz = dto.getClass(); //获取dto类

33 boolean dtoExistAnno = dtoclazz.isAnnotationPresent(RelMapper.class); //判断类上面是否有自定义注解

34 Field [] dtofds = dtoclazz.getDeclaredFields(); //dto fields

35 Field [] entryfds = entryclazz.getDeclaredFields(); //entity fields

36 Method entrys[] = entryclazz.getDeclaredMethods(); //entity methods

37 Method dtos[] = dtoclazz.getDeclaredMethods(); //dto methods

38 String mName,fieldName,dtoFieldType=null,entFieldType=null,dtoMapName = null,dtoFieldName =null;Object value = null;39 for(Method m : (enToDto ? dtos : entrys)) { //当 enToDto=true 此时是Entity转为Dto,遍历dto的属性

40 if((mName=m.getName()).startsWith("set")) { //只进set方法

41 fieldName = mName.toLowerCase().charAt(3) + mName.substring(4,mName.length()); //通过set方法获得dto的属性名

42 tohere:43 for(Field fd: dtofds) {44 fd.setAccessible(true); //setAccessible是启用和禁用访问安全检查的开关

45 if(fd.isAnnotationPresent(RelMapper.class)||dtoExistAnno){ //判断field上注解或类上面注解是否存在46 //获取与Entity属性相匹配的映射值(两种情况:1.该field上注解的value值(Entity的field name 和Dto 的field name 不同) 2.该field本身(本身则是Entity的field name 和Dto 的field name 相同))

47 dtoMapName = fd.isAnnotationPresent(RelMapper.class) ? (fd.getAnnotation(RelMapper.class).value().toString().equals("")?fd.getName().toString():fd.getAnnotation(RelMapper.class).value().toString()):fd.getName().toString();48 if(((enToDto ?fd.getName() : dtoMapName)).toString().equals(fieldName)) {49 dtoFieldType = fd.getGenericType().toString().substring(fd.getGenericType().toString().lastIndexOf(".") + 1); //获取dto属性的类型(如 private String field 结果 = String)

50 for(Field fe : entryfds) {51 fe.setAccessible(true);52 if(fe.getName().toString().equals(enToDto ? dtoMapName : fieldName) ) {//遍历Entity类的属性与dto属性注解中的value值匹配

53 entFieldType = fe.getGenericType().toString().substring(fe.getGenericType().toString().lastIndexOf(".") + 1); //获取Entity类属性类型

54 dtoFieldName = enToDto ?dtoMapName : fd.getName().toString();55 breaktohere;56 }57 }58 }59 }60 }61 if(dtoFieldName!= null && !dtoFieldName.equals("null")) {62 for(Method md : (enToDto ?entrys : dtos)) {63 if(md.getName().toUpperCase().equals("GET"+dtoFieldName.toUpperCase())){64 dtoFieldName = null;65 if(md.invoke(enToDto ? entry : dto) == null) { break;} //去空操作66 //Entity类field 与Dto类field类型不一致通过TypeProcessor处理转换

67 value = (entFieldType.equals(dtoFieldType))? md.invoke(enToDto ? entry : dto) :TypeProcessor(entFieldType, dtoFieldType,md.invoke(enToDto ? entry : dto),enToDto ? true : false);68 m.invoke(enToDto ? dto : entry, value); //得到field的值 通过invoke()赋值给要转换类的对应属性

69 value = null;70 break;71 }72 }73 }74 }75 }76 return enToDto ?dto : entry;77 }78

79 //类型转换处理

80 public staticObject TypeProcessor(String entFieldType,String dtoFieldType, Object obj,boolean enToDto) {81 if(entFieldType.equals(dtoFieldType)) returnobj;82

83 if(!entFieldType.equals(dtoFieldType)) {84 switch(entFieldType) {85 case "Date":86 return (enToDto)?TypeConverter.dateToString((Date) obj):TypeConverter.stringToDate(obj.toString());87 case "Timestamp":88 returnTypeConverter.timestampToTimestampString((Timestamp)obj);89 case "Integer":90 return (enToDto) ?obj.toString() : Integer.parseInt((String)obj) ;91 }92 }

93 returnobj;94 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值