|
|
|
@ -7,8 +7,13 @@ import org.dom4j.Document;
|
|
|
|
|
import org.dom4j.DocumentException;
|
|
|
|
|
import org.dom4j.Element;
|
|
|
|
|
import org.dom4j.io.SAXReader;
|
|
|
|
|
import org.springframework.util.ResourceUtils;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.Map;
|
|
|
|
@ -16,20 +21,36 @@ import java.util.Map;
|
|
|
|
|
/**
|
|
|
|
|
* <pre>
|
|
|
|
|
*
|
|
|
|
|
* /resources/sqlMapping.xml
|
|
|
|
|
* <?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
|
* <mapping-locations>
|
|
|
|
|
* <!--<file-location path="classpath:sql/user.xml"/>-->
|
|
|
|
|
* <directory-location path="classpath:sql/"/>
|
|
|
|
|
* </mapping-locations>
|
|
|
|
|
*
|
|
|
|
|
* sql map file
|
|
|
|
|
* <?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
|
* <entity-mappings namespace="board">
|
|
|
|
|
* <native-query id="selectBoardList">
|
|
|
|
|
* ...
|
|
|
|
|
* </native-query>
|
|
|
|
|
* </entity-mappings>
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* query xml : mybatis 기본 문법 지원
|
|
|
|
|
* #{var} --> .setParameter("var", "")
|
|
|
|
|
* ? --> .setParameter(1, "")
|
|
|
|
|
* <if text="id != null"> and id in (:ids)</if>
|
|
|
|
|
* <if test="id != null"> and id in (:ids)</if>
|
|
|
|
|
* --> .setParameter("ids", Arrays.asList("id1", "id2")
|
|
|
|
|
*
|
|
|
|
|
* <where>
|
|
|
|
|
* <if text= "">AND ...</if>
|
|
|
|
|
* <if text= "">AND ...</if>
|
|
|
|
|
* <if test= "">AND ...</if>
|
|
|
|
|
* <if test= "">AND ...</if>
|
|
|
|
|
* </where>
|
|
|
|
|
*
|
|
|
|
|
* <switch>
|
|
|
|
|
* <case text=" " >...</case>
|
|
|
|
|
* <case text=" " >...</case>
|
|
|
|
|
* <case test=" " >...</case>
|
|
|
|
|
* <case test=" " >...</case>
|
|
|
|
|
* <otherwise>
|
|
|
|
|
* .....
|
|
|
|
|
* </otherwise>
|
|
|
|
@ -44,8 +65,6 @@ public class QueryGenerator {
|
|
|
|
|
|
|
|
|
|
private static volatile Map<String, Element> classNameRootElementMap;
|
|
|
|
|
|
|
|
|
|
private static final String ROOT_PATH = System.getProperty("user.dir");
|
|
|
|
|
|
|
|
|
|
private QueryGenerator() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -62,7 +81,13 @@ public class QueryGenerator {
|
|
|
|
|
synchronized (QueryGenerator.class) {
|
|
|
|
|
if (classNameRootElementMap == null) {
|
|
|
|
|
classNameRootElementMap = new HashMap<>();
|
|
|
|
|
File confFile = new File(ROOT_PATH + "/src/main/resources/sqlMapping.xml");
|
|
|
|
|
|
|
|
|
|
File confFile = null;
|
|
|
|
|
try {
|
|
|
|
|
confFile = ResourceUtils.getFile("classpath:sqlMapping.xml");
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
SAXReader saxReader = new SAXReader();
|
|
|
|
|
Document document = null;
|
|
|
|
|
try {
|
|
|
|
@ -70,14 +95,20 @@ public class QueryGenerator {
|
|
|
|
|
} catch (DocumentException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Element rootElement = document.getRootElement();
|
|
|
|
|
Iterator iterator = rootElement.elementIterator();
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
|
Element element = (Element) iterator.next();
|
|
|
|
|
String tagName = element.getName();
|
|
|
|
|
String path = ROOT_PATH + element.attributeValue("path");
|
|
|
|
|
String path = element.attributeValue("path");
|
|
|
|
|
if (tagName.equals("file-location") || tagName.equals("directory-location")) {
|
|
|
|
|
loadMappingFile(new File(path));
|
|
|
|
|
try {
|
|
|
|
|
loadMappingFile(ResourceUtils.getFile(path));
|
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new RuntimeException("Invalid tag name, could only be 'file-location' or 'directory-location'");
|
|
|
|
|
}
|
|
|
|
@ -122,8 +153,8 @@ public class QueryGenerator {
|
|
|
|
|
Iterator iterator = rootElement.elementIterator();
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
|
Element element = (Element) iterator.next();
|
|
|
|
|
if (element.getName().equals("named-native-query")) {
|
|
|
|
|
String name = element.attributeValue("name");
|
|
|
|
|
if (element.getName().equals("native-query")) {
|
|
|
|
|
String name = element.attributeValue("id");
|
|
|
|
|
if (name != null && name.trim().equals(queryName.trim())) {
|
|
|
|
|
return element;
|
|
|
|
|
}
|
|
|
|
|