feat: sql parser 적용

dev
minuk926 2 years ago
parent 09d2d5219c
commit a9dc20a197

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

@ -7,8 +7,13 @@ import org.dom4j.Document;
import org.dom4j.DocumentException; import org.dom4j.DocumentException;
import org.dom4j.Element; import org.dom4j.Element;
import org.dom4j.io.SAXReader; import org.dom4j.io.SAXReader;
import org.springframework.util.ResourceUtils;
import java.io.File; 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.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
@ -16,20 +21,36 @@ import java.util.Map;
/** /**
* <pre> * <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 * query xml : mybatis
* #{var} --> .setParameter("var", "") * #{var} --> .setParameter("var", "")
* ? --> .setParameter(1, "") * ? --> .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") * --> .setParameter("ids", Arrays.asList("id1", "id2")
* *
* <where> * <where>
* <if text= "">AND ...</if> * <if test= "">AND ...</if>
* <if text= "">AND ...</if> * <if test= "">AND ...</if>
* </where> * </where>
* *
* <switch> * <switch>
* <case text=" " >...</case> * <case test=" " >...</case>
* <case text=" " >...</case> * <case test=" " >...</case>
* <otherwise> * <otherwise>
* ..... * .....
* </otherwise> * </otherwise>
@ -44,8 +65,6 @@ public class QueryGenerator {
private static volatile Map<String, Element> classNameRootElementMap; private static volatile Map<String, Element> classNameRootElementMap;
private static final String ROOT_PATH = System.getProperty("user.dir");
private QueryGenerator() { private QueryGenerator() {
} }
@ -62,7 +81,13 @@ public class QueryGenerator {
synchronized (QueryGenerator.class) { synchronized (QueryGenerator.class) {
if (classNameRootElementMap == null) { if (classNameRootElementMap == null) {
classNameRootElementMap = new HashMap<>(); 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(); SAXReader saxReader = new SAXReader();
Document document = null; Document document = null;
try { try {
@ -70,14 +95,20 @@ public class QueryGenerator {
} catch (DocumentException e) { } catch (DocumentException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
Element rootElement = document.getRootElement(); Element rootElement = document.getRootElement();
Iterator iterator = rootElement.elementIterator(); Iterator iterator = rootElement.elementIterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
Element element = (Element) iterator.next(); Element element = (Element) iterator.next();
String tagName = element.getName(); 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")) { 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 { } else {
throw new RuntimeException("Invalid tag name, could only be 'file-location' or 'directory-location'"); 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(); Iterator iterator = rootElement.elementIterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
Element element = (Element) iterator.next(); Element element = (Element) iterator.next();
if (element.getName().equals("named-native-query")) { if (element.getName().equals("native-query")) {
String name = element.attributeValue("name"); String name = element.attributeValue("id");
if (name != null && name.trim().equals(queryName.trim())) { if (name != null && name.trim().equals(queryName.trim())) {
return element; return element;
} }

@ -13,6 +13,6 @@ public class CaseSqlNode extends AbstractSqlNode {
@Override @Override
public boolean display() { 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 @Override
public boolean display() { public boolean display() {
String expression = element.attributeValue("text"); String expression = element.attributeValue("test");
if (expression == null || expression.trim().isEmpty()) { if (expression == null || expression.trim().isEmpty()) {
return false; 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": case "otherwise":
result = new OtherwiseSqlNode(element, parameterMap); result = new OtherwiseSqlNode(element, parameterMap);
break; break;
case "named-native-query": case "native-query":
result = new BasicSqlNode(element, parameterMap); result = new BasicSqlNode(element, parameterMap);
break; break;
default: default:

@ -131,11 +131,14 @@ public class DBUtils {
field.setAccessible(true); field.setAccessible(true);
String name = field.getName(); 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); boolean isSameName = entry.getKey().equals(name);
if (isSameType && isSameName) { //if (isSameType && isSameName) {
field.set(instance, map.get(name)); 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; break;
} }
} }
@ -171,4 +174,6 @@ public class DBUtils {
} }
return convertList; return convertList;
} }
//private static void convertType()
} }

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

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

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

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<mapping-locations> <mapping-locations>
<!-- specify the file location for one xml file which includes sql queries--> <!-- 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--> <!-- 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> </mapping-locations>
Loading…
Cancel
Save