feat: sql parser 적용

dev
minuk926 2 years ago
parent 09d2d5219c
commit a9dc20a197

@ -27,7 +27,7 @@ plugins {
// buildscript
group = 'com.xit'
version = '0.0.1-SNAPSHOT'
description = 'xit-opst-bo'
description = 'xit-opst-be'
sourceCompatibility = '11'
// WAS : bootWar.enabled = false

@ -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;
}

@ -13,6 +13,6 @@ public class CaseSqlNode extends AbstractSqlNode {
@Override
public boolean display() {
return ExpressionUtils.isTrueExpression(this.element.attributeValue("text"), parameterMap);
return ExpressionUtils.isTrueExpression(this.element.attributeValue("test"), parameterMap);
}
}

@ -12,10 +12,10 @@ public class IfSqlNode extends AbstractSqlNode {
@Override
public boolean display() {
String expression = element.attributeValue("text");
String expression = element.attributeValue("test");
if (expression == null || expression.trim().isEmpty()) {
return false;
}
return ExpressionUtils.isTrueExpression(this.element.attributeValue("text"), parameterMap);
return ExpressionUtils.isTrueExpression(this.element.attributeValue("test"), parameterMap);
}
}

@ -26,7 +26,7 @@ public class SimpleSqlNodeFactory {
case "otherwise":
result = new OtherwiseSqlNode(element, parameterMap);
break;
case "named-native-query":
case "native-query":
result = new BasicSqlNode(element, parameterMap);
break;
default:

@ -131,11 +131,14 @@ public class DBUtils {
field.setAccessible(true);
String name = field.getName();
boolean isSameType = entry.getValue().getClass().equals(field.getType());
//boolean isSameType = entry.getValue().getClass().equals(field.getType());
boolean isSameName = entry.getKey().equals(name);
if (isSameType && isSameName) {
field.set(instance, map.get(name));
//if (isSameType && isSameName) {
if (isSameName) {
if(field.getType() == Integer.class) field.set(instance, Integer.parseInt(String.valueOf(map.get(name))));
else if(field.getType() == Long.class) field.set(instance, Long.parseLong(String.valueOf(map.get(name))));
else field.set(instance, map.get(name));
break;
}
}
@ -171,4 +174,6 @@ public class DBUtils {
}
return convertList;
}
//private static void convertType()
}

@ -462,16 +462,13 @@ public class MpowerUtils {
for (Field fd : clsFields) {
fd.setAccessible(true);
boolean isSameType = feilds.get(j).getClass().equals(fd.getType());
boolean isSameName = feilds.get(j).equals(fd.getName());
if (isSameType && isSameName) {
fd.set(instance, mpower.getString("list1", i, j));
if (feilds.get(j).equals(fd.getName())) {
if(fd.getType() == Integer.class) fd.set(instance, Integer.parseInt(mpower.getString("list1", i, j)));
else if(fd.getType() == Long.class) fd.set(instance, Long.parseLong(mpower.getString("list1", i, j)));
else fd.set(instance, mpower.getString("list1", i, j));
break;
}
}
//m.put(feilds.get(j), mpower.getString("list1", i, j));
}
}
list.add(instance);

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings namespace="board">
<named-native-query name="selectBoardList">
<native-query id="selectBoardList">
/* board-mapper|selectBoardList|julim */
SELECT MCB.ci_code,
MU.name,
@ -18,14 +18,20 @@
LEFT OUTER JOIN min_userinfo MU
ON MCB.ci_id = MU.userid
<where>
<if text="ciTitle != null and ciTitle != ''">AND INSTR(MCB.ci_title, #{ciTitle}) > 0</if>
<if text="ciName != null and ciName != ''">AND MCB.ci_name like #{ciName}||'%'</if>
<if text="ciContents != null and ciContents != ''">AND INSTR(MCB.ci_contents, #{ciContents}) > 0</if>
<if test="ciTitle != null and ciTitle != ''">
AND INSTR(MCB.ci_title, #{ciTitle}) &gt; 0
</if>
<if test="ciName != null and ciName != ''">
AND MCB.ci_name like #{ciName}||'%'
</if>
<if test="ciContents != null and ciContents != ''">
AND INSTR(MCB.ci_contents, #{ciContents}) &gt; 0
</if>
</where>
ORDER BY MCB.ci_ref DESC,
MCB.ci_step ASC,
MCB.ci_code DESC
</named-native-query>
</native-query>

@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings namespace="refreshToken">
<named-native-query name="selectRefreshToken">
<native-query id="selectRefreshToken">
/* refreshToken-mapper|selectRefreshToken|julim */
SELECT key,
value
FROM refresh_token
WHERE key = #{key}
</named-native-query>
</native-query>
<named-native-query name="insertRefreshToken">
<native-query id="insertRefreshToken">
/* refreshToken-mapper|insertRefreshToken|julim */
INSERT
INTO refresh_token (
@ -18,12 +18,12 @@
#{key},
#{value}
)
</named-native-query>
</native-query>
<named-native-query name="updateRefreshToken">
<native-query id="updateRefreshToken">
/* refreshToken-mapper|updateRefreshToken|julim */
UPDATE refresh_token
SET value = #{value}
WHERE key = #{key}
</named-native-query>
</native-query>
</entity-mappings>

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<mapping-locations>
<!-- specify the file location for one xml file which includes sql queries-->
<!-- <file-location path="/src/main/resources/sql/orm.xml"/>-->
<!-- <file-location path="classpath:sql/orm.xml"/>-->
<!-- specify the directory location, which will include all xml files for sql queries under the directory-->
<directory-location path="/src/main/resources/sql/"/>
<directory-location path="classpath:sql/"/>
</mapping-locations>
Loading…
Cancel
Save