mybatis根据property获取column

mybatis根据property获取column

mybatis根据类的属性获取xml文件中对应的column

mybatis获取xml文件中property对应的column

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

蕃薯耀 2016年4月29日 15:44:59 星期五

http://fanshuyao.iteye.com/

xml解析采用Dom4j(Dom4j使用详情见:http://fanshuyao.iteye.com/blog/2279679)

可以在附件中下载

package xxx.xxx.common.utils;

import java.util.Date;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;



public class XmlUtils {
	
	/**
	 * 根据类的属性名找表的列名(取一个的时候可以使用此方法)
	 * @param fileName 类对应的Mapper xml文件
	 * @param id  唯一的id
	 * <p>
	 * 如:resultMap id="BaseResultMap" type="com.chinagas.org.beans.User" 中的id
	 * </p>
	 * @param property 属性名(对应的Java对象属性名)
	 * @return
	 */
	public static String getMapperColumnByProperty(String fileName, String id, String property){
		try {
			SAXReader saxReader = new SAXReader();  
	        Document document = saxReader.read(XmlUtils.class.getClassLoader().getResourceAsStream(fileName));  
	        if(document != null){
	            Element root = document.getRootElement();
	            if(root != null){
	            	@SuppressWarnings("unchecked")
					List<Element> resultMaps = root.elements("resultMap");
	            	for (Element resultMap : resultMaps) {
	            		if(resultMap != null && resultMap.attributeValue("id").equals(id)){
	            			@SuppressWarnings("unchecked")
							List<Element> properties = resultMap.elements();
	            			for (Element prop : properties) {
								if(prop != null && prop.attributeValue("property").equals(property)){
									return prop.attributeValue("column");
								}
							}
		            	}
					}
	            }  
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 返回ResultMap对应Element对象(取2次以上的时候,建议先把Element对象找到,再根据此Element对象再去找column,效率高很多)
	 * @param fileName 类对应的Mapper xml文件
	 * @param id  唯一的id
	 * <p>
	 * 如:resultMap id="BaseResultMap" type="com.chinagas.org.beans.User" 中的id
	 * </p>
	 * @return
	 */
	public static Element getResultMapElement(String fileName, String id){
		try {
			SAXReader saxReader = new SAXReader();  
	        Document document = saxReader.read(XmlUtils.class.getClassLoader().getResourceAsStream(fileName));  
	        if(document != null){
	            Element root = document.getRootElement();
	            if(root != null){
	            	@SuppressWarnings("unchecked")
					List<Element> resultMaps = root.elements("resultMap");
	            	for (Element resultMap : resultMaps) {
	            		if(resultMap != null && resultMap.attributeValue("id").equals(id)){
	            			return resultMap;
		            	}
					}
	            }  
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 在Element根据property找表的列名(和方法getResultMapElement()结合使用,多次取Column时效率高出很多倍)
	 * @param resultMapElement Mapper xml文件解析后得到的Element对象(方法:getResultMapElement())
	 * @param property 属性名(对应的Java对象属性名)
	 * @return
	 */
	public static String getMapperColumnByElement(Element resultMapElement, String property){
		try {
			if(resultMapElement != null){
    			@SuppressWarnings("unchecked")
				List<Element> properties = resultMapElement.elements();
    			for (Element prop : properties) {
					if(prop != null && prop.attributeValue("property").equals(property)){
						return prop.attributeValue("column");
					}
				}
        	}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public static void main(String[] args) {
		long startTime = new Date().getTime();
		
		/*System.out.println(getMapperColumnByProperty("UserMapper.xml","BaseResultMap", "userName"));
		System.out.println(getMapperColumnByProperty("UserMapper.xml","BaseResultMap", "loginName"));
		System.out.println(getMapperColumnByProperty("UserMapper.xml","BaseResultMap", "orgName"));
		System.out.println(getMapperColumnByProperty("UserMapper.xml","BaseResultMap", "sex"));*/
		
		Element e = getResultMapElement("UserMapper.xml","BaseResultMap");
		System.out.println(getMapperColumnByElement(e, "userName"));
		System.out.println(getMapperColumnByElement(e, "loginName"));
		System.out.println(getMapperColumnByElement(e, "orgName"));
		System.out.println(getMapperColumnByElement(e, "sex"));
		
		long endTime = new Date().getTime();
		System.out.println("所用的时间间隔是:"+ (endTime-startTime));
		
	}
	
	
}

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

蕃薯耀 2016年4月29日 15:44:59 星期五

http://fanshuyao.iteye.com/

原文地址:https://www.cnblogs.com/fanshuyao/p/6227136.html