package com.yuexiang.govcms.xml;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.yuexiang.govcms.util.ConstantUtil;
/**
* 初始化Mybatis property转column Map
*
* @author yangtao
*
* @since 2016年8月22日 下午4:28:30
*/
public class CreateMybatisP2CMap {
private static final String PATH = CreateMybatisP2CMap.class.getResource("").getPath();// 当前目录
/**
* 解析mybatis MapperXML映射文件
*
* @author yangtao
* @since 2016年8月22日 下午5:43:07
*
*/
public static void analysisMybatisMapperXML() {
File file = new File(PATH);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
String fileName = tempList[i].getName();
if (fileName.indexOf(".xml") > 0) {
String mapperName = fileName.replace("Mapper.xml", "");
System.out.println("...................开始解析 " + fileName + " 文件...................");
ConstantUtil.MYBATISP2CMAP.put(mapperName, getMapperColumnByProperty(tempList[i]));
}
}
}
/**
* Mybatis property转column
*
* @author yangtao
* @since 2016年8月22日 下午5:42:36
*
* @param mapperXmlFile
* @return
*/
public static Map<String, String> getMapperColumnByProperty(File mapperXmlFile) {
try {
Document doc = Jsoup.parse(mapperXmlFile, "utf-8");
Elements elements = doc.select("#BaseResultMap > *");
Map<String, String> map = new HashMap<>();
for (Element element : elements) {
map.put(element.attr("property"), element.attr("column"));
}
return map;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}