实体类转换-映射EntityConverter
来源:互联网 发布:淘宝上如何申请换货 编辑:程序博客网 时间:2024/06/08 03:59
package com.landz.system.common.util;import java.lang.annotation.Annotation;import java.lang.reflect.Field;import java.lang.reflect.Modifier;import java.util.ArrayList;import java.util.List;import org.springframework.data.mongodb.core.query.Update;public class EntityConverter {/** * 实体类转换 (若covertEntity是一个已被赋值的实体转换时会替换相同属性名) * * 根据实体内属性名进行映射 * * 属性名相同 默认属性名相同 则认为属性类型也相同 且不为空 则映射 * * * * @param entity 被转换实体 * @param covertEntity 转换后的实体 * @return * @throws IllegalArgumentException * @throws IllegalAccessException */public <S extends T, T> S converEntity(S entity,S covertEntity) throws IllegalArgumentException, IllegalAccessException{Field[] entityfields = entity.getClass().getDeclaredFields();Field[] covertfields = covertEntity.getClass().getDeclaredFields();boolean breakFlag=false;for(Field covertfield : covertfields){covertfield.setAccessible(true);for(Field entityfield : entityfields){entityfield.setAccessible(true);if(entityfield.getName().equals(covertfield.getName())){//属性名相同if(entityfield.get(entity)!=null){//如果不为空 则放入covertfield.set(covertEntity, entityfield.get(entity));breakFlag=true;}}if(breakFlag){breakFlag=false;break;}}}return covertEntity;}/** * 实体类转换 * * 根据实体内属性名进行映射 * * 属性名相同 默认属性名相同 则认为属性类型也相同 且不为空 则映射 * @param <T> * @param <S> * * * * @param entity 被转换实体 * @param covertEntity 转换后的实体类型 * @return * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InstantiationException */public <T, S> T entityConver(S entity,Class<T> covertEntityClass) throws IllegalArgumentException, IllegalAccessException, InstantiationException{T covertEntity=covertEntityClass.newInstance();Field[] entityfields = entity.getClass().getDeclaredFields();Field[] covertfields = covertEntityClass.getDeclaredFields();boolean breakFlag=false;for(Field covertfield : covertfields){covertfield.setAccessible(true);for(Field entityfield : entityfields){entityfield.setAccessible(true);if(entityfield.getName().equals(covertfield.getName())){//属性名相同if(entityfield.get(entity)!=null){//如果不为空 则放入if((entityfield.getModifiers()&Modifier.FINAL)==Modifier.FINAL){//如果属性是final类型,不赋值break;}covertfield.set(covertEntity, entityfield.get(entity));breakFlag=true;}}if(breakFlag){breakFlag=false;break;}}}return covertEntity;}/** * 转换list * @param entities 要转换的list * @param covertEntityClass 需要转换的 class * @return * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InstantiationException */public <T, S> List<T> listConver( List<S> entities, Class<T> covertEntityClass) throws IllegalArgumentException, IllegalAccessException, InstantiationException{List<T> covertEntitys=new ArrayList<T>();for (S entity : entities) {covertEntitys.add(entityConver(entity,covertEntityClass));}return covertEntitys;}/** * @MethodName : getUpdate * @Description : 获取类中所有属性及属性值 获取update 对象 * @param entity * 操作对象 * @param entityClass * 操作类,用于获取类中的方法 * @return * @throws IllegalAccessException * @throws IllegalArgumentException */public Update getUpdate(Object entity, Class<?> entityClass) throws IllegalArgumentException, IllegalAccessException {Update update = new Update();// 获取类中的所有定义字段Field[] fields = entityClass.getDeclaredFields();// 循环遍历字段,获取字段对应的属性值for (Field field : fields) {// 如果不为空,设置可见性,然后返回field.setAccessible(true);if(!"serialVersionUID".equals(field.getName())){if (field.get(entity) != null) {if (isNeedPass(field)) {// 无带有Transient标示或者主键update.set(field.getName(), field.get(entity));}}}}return update;}/** * 判断是否有 类似 Transient 这种需要跳过的注解 * @param field * @return */private Boolean isNeedPass(Field field){Boolean flag=true;Annotation[] annotations= field.getAnnotations();if(annotations.length>0){for(Annotation annotation:annotations){if("interface org.springframework.data.annotation.Transient".equals(annotation.getClass())){flag=false;break;}}}return flag;}}
0 0
- 实体类转换-映射EntityConverter
- hibernate实体类映射
- 实体类和映射文件
- 实体类转换
- hibernate 映射实体类并操作数据库!
- HIBERNATE 将实体类 映射成数据库表
- hibernate一个实体类映射多张表
- SQLServer映射hibernate到实体类
- Hibernate映射实体类自动导入方法
- java 根据实体类创建映射文件
- jdbc结果集映射成实体类
- Mybatis实体类和表映射
- 6生成实体类和映射文件
- eclipse数据表映射为实体类
- mybatis自动生成映射实体类
- Hibernate--实体类与表映射01
- hibernate实体类映射不到的问题
- 记录Hibernate映射实体类@注解
- STOMP
- 日期格式化
- java获取当前日期是星期几
- 优先队列重载 3种写法
- ios,android提交文件及图片到 PHP服务端
- 实体类转换-映射EntityConverter
- init/initWithFrame/自定义init方法执行顺序
- AndroidStudio下使用Retrofit进行网络请求
- http://androidxref.com
- GitHub 上排名前 100 的 Objective-C 项目简介
- 项目用到的分支总汇
- JAVA加密解密要用到的JAR包
- linux添加开机自启动脚本示例详解
- Elasticsearch Client 初始化方式以及清除数据方式