java从数据库中查出来二次封装基于XML

实现类似于mybatis的配置关系一对多或者一对一的实现:基于<XML>实现:
先上个配置文件
 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper>

	<resultMap ID="manyMap" type="com.zfsy.entity.ProductDetailEntity">
		<id property="xh" column="xh"/>
		<result property="spmc" column="spmc"/>
		<result property="dj" column="dj"/>
		<result property="sppj" column="sppj"/>
		<result property="dw" column="dw"/>
		<result property="spms" column="spms"/>
		<collection property="images" ofType="com.zfsy.entity.ImageUrl">
			<!-- <id property="xh" column="bxh"/> -->
			<result property="lx" column="lx"/>
			<result property="tpurl" column="tpurl"/>
		</collection>
		 <association property="goodsType" ofType="com.stark.app.model.GoodsType">
			<id property="xh" column="cxh"/>
			<result property="mc" column="mc"/>
			<result property="ico" column="ico"/>
		</association>  
	</resultMap>

</mapper>

/** 
* @author 
* @version 创建时间:2017年3月20日 下午3:19:04 
* @description 对应xml中resultMap下的一条条记录
*/
public class ResultSetMapping {
	private String property;
	private String column;
	//private String collectionType;
	private String javaType;
	//对应collection和association标签下的result
	private List<ResultSetMapping> composites;
	
	/*public String getCollectionType() {
		return collectionType;
	}
	public void setCollectionType(String collectionType) {
		this.collectionType = collectionType;
	}*/
	public List<ResultSetMapping> getComposites() {
		return composites;
	}
	public void setComposites(List<ResultSetMapping> composites) {
		this.composites = composites;
	}
	public String getProperty() {
		return property;
	}
	public void setProperty(String property) {
		this.property = property;
	}
	public String getColumn() {
		return column;
	}
	public void setColumn(String column) {
		this.column = column;
	}
	public String getJavaType() {
		return javaType;
	}
	public void setJavaType(String javaType) {
		this.javaType = javaType;
	}
	
}


/** 
* @author 
* @version 创建时间:2017年3月20日 下午5:03:09 
* @description 对应整个resultMap配置文件的一些关键信息
*/
public class ResultMapper {
	private String id;
	private String classType;
	private List<ResultSetMapping> rsMapping;
	private boolean hasNest;
	
	public boolean isHasNest() {
		return hasNest;
	}
	public void setHasNest(boolean hasNest) {
		this.hasNest = hasNest;
	}
	public ResultMapper(String id, String classType, List<ResultSetMapping> rsMapping) {
		super();
		this.id = id;
		this.classType = classType;
		this.rsMapping = rsMapping;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getClassType() {
		return classType;
	}
	public void setClassType(String classType) {
		this.classType = classType;
	}
	public List<ResultSetMapping> getRsMapping() {
		return rsMapping;
	}
	public void setRsMapping(List<ResultSetMapping> rsMapping) {
		this.rsMapping = rsMapping;
	}
	
}

/**
 * @author 
 * @version 创建时间:2017年3月20日 下午3:37:17
 * @description 读取配置文件 封装数据到resultMapper
 */
public class Configuration {
	public ResultMapper loadXml(String path, String mapId) {
		SAXReader saxReader = new SAXReader();
		List<ResultSetMapping> mappings = new ArrayList<ResultSetMapping>();
		try {
			Document document = saxReader.read(this.getClass().getClassLoader().getResourceAsStream(path));
			Element element = document.getRootElement();
			Element node = element.elementByID(mapId);
			mappings = configMappings(node, mappings);
			ResultMapper mapper = new ResultMapper(node.element("id").attributeValue("property"), node.attributeValue("type"), mappings);
			if(node.element("collection")!=null){
				mapper.setHasNest(true);
			}
			return mapper;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	private List<ResultSetMapping> configMappings(Element node, List<ResultSetMapping> mappings2) {
		ResultSetMapping resultSetMapping;
		ResultSetMapping cresultSetMapping;
		List<ResultSetMapping> cResultSetMapping;
		List<Element> elements = node.elements();
		for (Element element : elements) {
			resultSetMapping = new ResultSetMapping();
			if (element.getName().equals("collection")||element.getName().equals("association")){
				// resultSetMapping.setColumn(element.attributeValue("column"));
				cResultSetMapping = new ArrayList<ResultSetMapping>();
				resultSetMapping.setJavaType(element.attributeValue("ofType"));
				List<Element> celements = element.elements();

				for (Element element2 : celements) {
					cresultSetMapping = new ResultSetMapping();
					cresultSetMapping.setColumn(element2.attributeValue("column"));
					cresultSetMapping.setProperty(element2.attributeValue("property"));
					cResultSetMapping.add(cresultSetMapping);
				}
				resultSetMapping.setComposites(cResultSetMapping);
			} 
			resultSetMapping.setColumn(element.attributeValue("column"));
			resultSetMapping.setProperty(element.attributeValue("property"));
			mappings2.add(resultSetMapping);
		}
		return mappings2;
	}

}

/**
 * @author 
 * @version 创建时间:2017年3月20日 下午5:13:53
 * @description 组装数据
 */
public class BeanFactory {
	private Map<String, List<Object>> maps = new HashMap<String, List<Object>>();
	
	private String id;

	@SuppressWarnings("unchecked")
	public <T, E> List<E> getBeans(List<T> lists, ResultMapper resultMapper) {

		try {

			// Map<String, Object> objectMap = new HashMap<String, Object>();
			List<E> dataList = new ArrayList<E>();
			String id = resultMapper.getId();
			this.id = id;
			E targetObject = null;
			List<Object> composites;

			List<ResultSetMapping> resultSetMappings = resultMapper.getRsMapping();

			for (T t : lists) {

				if (!maps.containsKey(getValue(id, t))) {
					targetObject = (E) Class.forName(resultMapper.getClassType()).newInstance();
					// composites = new ArrayList<Object>();
					setData(targetObject, t, resultSetMappings);
					// maps.put(getValue(id, t).toString(), composites);
					dataList.add(targetObject);
				} else if (resultMapper.isHasNest()) {
					composites = maps.get(getValue(id, t));
					composites = setComposites(composites, t, resultSetMappings);
				}
				/*
				 * else { composites = maps.get(getValue(id, t)); composites =
				 * setComposites(composites, t, resultSetMappings); }
				 */
			}

			return dataList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private List<Object> setComposites(List<Object> composites, Object obj, List<ResultSetMapping> resultSetMappings) {
		String javaType;
		Object object;
		List<ResultSetMapping> compositesMappings;
		for (ResultSetMapping mapping : resultSetMappings) {
			javaType = mapping.getJavaType();
			if (javaType != null) {
				try {
					object = Class.forName(javaType).newInstance();
					compositesMappings = mapping.getComposites();
					setData(object, obj, compositesMappings);
					composites.add(object);
					return composites;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

	private <E> void setData(E targetObject, Object obj, List<ResultSetMapping> resultSetMappings) {
		String property;
		String javaType;
		Object value;
		Object object;
		String column;
		// Object nestObject;
		List<Object> composites;
		for (ResultSetMapping mapping : resultSetMappings) {
			property = mapping.getProperty();
			javaType = mapping.getJavaType();
			column = mapping.getColumn();
			if (javaType == null) {
				value = getValue(column, obj);
				setValue(value, property, targetObject);
			} else {
				try {
					// nestObject = getValue(property, targetObject);
					Field field = targetObject.getClass().getDeclaredField(property);
					if (field.getType().isAssignableFrom(List.class)) {
						composites = maps.get(getValue(id, obj));
						if (composites == null) {
							composites = new ArrayList<Object>();
							maps.put(getValue(id, obj).toString(), composites);
						}
						composites = setComposites(composites, obj, resultSetMappings);
						setValue(composites, property, targetObject);
					} else {
						object = Class.forName(javaType).newInstance();
						setAssociation(object, obj, mapping.getComposites());
						setValue(object, property, targetObject);
					}
				} catch (Exception e) {
				}
			}
		}

	}

	private void setAssociation(Object nestObject, Object obj, List<ResultSetMapping> composites) {
		String property;
		Object value;
		String column;
		for (ResultSetMapping mapping : composites) {
			property = mapping.getProperty();
			column = mapping.getColumn();
			value = getValue(column, obj);

			setValue(value, property, nestObject);
		}
	}

	private <E> void setValue(Object value, String property, E obj) {
		try {
			Field field = obj.getClass().getDeclaredField(property);
			field.setAccessible(true);
			field.set(obj, value);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Object getValue(String property, Object obj) {
		try {
			Field field = obj.getClass().getDeclaredField(property);
			field.setAccessible(true);
			return field.get(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
}




猜你喜欢

转载自chen-sai-201607223902.iteye.com/blog/2365022