feat: xml sql parsing

dev
Jonguk. Lim 2 years ago
parent bb1c7f3447
commit 5c71aca459

@ -14,6 +14,7 @@ import com.xit.core.oauth2.utils.HeaderUtil;
import com.xit.core.support.jpa.JpaUtil;
import com.xit.core.util.Checks;
import com.xit.core.util.CommUtil;
import com.xit.core.util.DBUtils;
import com.xit.core.util.mpower.MpowerUtil;
import com.xit.core.util.mpower.MpowerUtils;
import lombok.AllArgsConstructor;
@ -66,15 +67,15 @@ public class BoardService implements IBoardService {
@Transactional(readOnly = true)
public List<Map<String, Object>> findAll(final BoardDto dto, Pageable pageable) {
String sql = CommUtil.getMybatisSql(sqlSessionTemplate, "board.selectBoardList", dto);
System.out.println(DBUtils.getXmlSql("sql/board2-mapper", "selectBoardList"));
String sql = DBUtils.getMybatisSql(sqlSessionTemplate, "board.selectBoardList", dto);
MpowerUtils sendXml = new MpowerUtils();
sendXml.setFeilds("ciCode, ciName, ciContentno, ciTitle, ciContents, ciNalja, ciStep, ciRevel, ciRef, ciHit, ciPass, ciId");
//sendXml.setQuery(sql);
sendXml.setQuery(selectBoardListSql);
return sendXml.selectCustomQuery();
return null;
//return sendXml.selectCustomQuery();
}
// @Transactional(readOnly = true)

@ -5,6 +5,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.web.HateoasPageableHandlerMethodArgumentResolver;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ -14,7 +15,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
*/
@Configuration
// @DataJpaTest를 사용하려면 @EnableWebMvc 사용하면 안된다
//@EnableWebMvc
@EnableWebMvc
@EnableAsync
public class MvcWebConfig implements WebMvcConfigurer {

@ -4,31 +4,19 @@ import com.rits.cloning.Cloner;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.session.SqlSession;
import org.mozilla.universalchardet.UniversalDetector;
import org.springframework.http.ResponseCookie;
import org.springframework.mobile.device.Device;
import org.springframework.mobile.device.DeviceUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLEncoder;
@ -58,6 +46,22 @@ public class CommUtil {
else return Class.forName(className).newInstance();
}
/**
* Get Charset
* @param bytes check String bytes
* @return String charset String
*/
public static String detectCharset(byte[] bytes) {
UniversalDetector detector = new UniversalDetector(null);
detector.handleData(bytes, 0, bytes.length);
detector.dataEnd();
String detectedCharset = detector.getDetectedCharset();
if (detectedCharset != null && detector.isDone() && Charset.isSupported(detectedCharset)) {
return detectedCharset;
}
return detectedCharset;
}
/**
* Encoding Type return
* @param str String
@ -658,138 +662,6 @@ public class CommUtil {
}
}
public static String detectCharset(byte[] bytes) {
UniversalDetector detector = new UniversalDetector(null);
detector.handleData(bytes, 0, bytes.length);
detector.dataEnd();
String detectedCharset = detector.getDetectedCharset();
if (detectedCharset != null && detector.isDone() && Charset.isSupported(detectedCharset)) {
return detectedCharset;
}
return detectedCharset;
}
public static String getMybatisSql(SqlSession sqlSession, String id, Object param) {
BoundSql boundSql = sqlSession.getConfiguration().getMappedStatement(id).getBoundSql(param);
List<ParameterMapping> parameterMappings = new ArrayList<>(boundSql.getParameterMappings());
Map<String, Object> parameters = new HashMap<>();
if (param instanceof Map) {
parameters.putAll((Map) param);
} else {
for (ParameterMapping parameterMapping : parameterMappings) {
try {
String field = parameterMapping.getProperty();
//try {
String methodNm = "get"+field.substring(0,1).toUpperCase()+field.substring(1);
Method method = param.getClass().getMethod(methodNm);
parameters.put(field, method.invoke(param));
//parameters.put(field, param.getClass().getMethod("get"+field.substring(0,1).toUpperCase()+field.substring(1)).invoke(param));
} catch (NoSuchMethodException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
String sql = boundSql.getSql();
for (ParameterMapping par : parameterMappings) {
Object parameter = parameters.get(par.getProperty());
if (parameter == null)
sql = sql.replaceFirst("\\?", "NULL");
else
sql = sql.replaceFirst("\\?", "'" + parameter.toString() + "'");
}
return sql;
}
/**
*
* @param sqlSession SqlSessionTemplate
* @param id SQL id
* @param param Object
*/
public static String getXmlSql(SqlSession sqlSession, String id, Object param) {
BoundSql boundSql = sqlSession.getConfiguration().getMappedStatement(id).getBoundSql(param);
List<ParameterMapping> parameterMappings = new ArrayList<>(boundSql.getParameterMappings());
Map<String, Object> parameters = new HashMap<>();
if (param instanceof Map) {
parameters.putAll((Map) param);
} else {
for (ParameterMapping parameterMapping : parameterMappings) {
try {
String field = parameterMapping.getProperty();
//try {
String methodNm = "get"+field.substring(0,1).toUpperCase()+field.substring(1);
Method method = param.getClass().getMethod(methodNm);
parameters.put(field, method.invoke(param));
//parameters.put(field, param.getClass().getMethod("get"+field.substring(0,1).toUpperCase()+field.substring(1)).invoke(param));
} catch (NoSuchMethodException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
String sql = boundSql.getSql();
for (ParameterMapping par : parameterMappings) {
Object parameter = parameters.get(par.getProperty());
if (parameter == null)
sql = sql.replaceFirst("\\?", "NULL");
else
sql = sql.replaceFirst("\\?", "'" + parameter.toString() + "'");
}
return sql;
}
public static String getXmlSql(String xmlFileName, String nodeName) {
String sql = null;
try {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("test.csv");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
//builder.p
Document document = builder.parse(is);
//Document document = builder.parse(new File(xmlFileName));
//List<Employee> employees = new ArrayList<>();
NodeList nodeList = document.getDocumentElement().getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element elem = (Element) node;
return elem.getElementsByTagName(nodeName)
.item(0).getChildNodes().item(0).getNodeValue();
}
}
return sql;
} catch (ParserConfigurationException | IOException | SAXException e) {
throw new RuntimeException(e);
}
}
}

@ -0,0 +1,100 @@
package com.xit.core.util;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.session.SqlSession;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DBUtils {
/**
*
* @param sqlSession SqlSessionTemplate
* @param id SQL id
* @param param Object
*/
public static String getMybatisSql(SqlSession sqlSession, String id, Object param) {
BoundSql boundSql = sqlSession.getConfiguration().getMappedStatement(id).getBoundSql(param);
List<ParameterMapping> parameterMappings = new ArrayList<>(boundSql.getParameterMappings());
Map<String, Object> parameters = new HashMap<>();
if (param instanceof Map) {
parameters.putAll((Map) param);
} else {
for (ParameterMapping parameterMapping : parameterMappings) {
try {
String field = parameterMapping.getProperty();
//try {
String methodNm = "get"+field.substring(0,1).toUpperCase()+field.substring(1);
Method method = param.getClass().getMethod(methodNm);
parameters.put(field, method.invoke(param));
//parameters.put(field, param.getClass().getMethod("get"+field.substring(0,1).toUpperCase()+field.substring(1)).invoke(param));
} catch (NoSuchMethodException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (InvocationTargetException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
String sql = boundSql.getSql();
for (ParameterMapping par : parameterMappings) {
Object parameter = parameters.get(par.getProperty());
if (parameter == null)
sql = sql.replaceFirst("\\?", "NULL");
else
sql = sql.replaceFirst("\\?", "'" + parameter.toString() + "'");
}
return sql;
}
public static String getXmlSql(String xmlFileName, String nodeName) {
StringBuilder sb = new StringBuilder();
try {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream(xmlFileName + ".xml");
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
XMLEventReader reader = xmlInputFactory.createXMLEventReader(is);
while (reader.hasNext()) {
XMLEvent nextEvent = reader.nextEvent();
if (nextEvent.isStartElement()) {
StartElement startElement = nextEvent.asStartElement();
if(startElement.getName().getLocalPart().equals(nodeName)){
nextEvent = reader.nextEvent();
sb.append(nextEvent.asCharacters().getData());
}
}
if (nextEvent.isEndElement()) {
EndElement endElement = nextEvent.asEndElement();
if (endElement.getName().getLocalPart().equals(nodeName)) {
return sb.toString();
}
}
}
return sb.toString();
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}
}

@ -17,7 +17,7 @@ api:
file:
cmm:
upload:
root: c:/data/file/upload
root: /data/file/upload
# root: /Users/minuk/data/file/upload
# 공지사항
path: /kangnamSIM/simUpFile

@ -4,12 +4,12 @@
<springProfile name="local">
<property name="LOG_PATH" value="/data/xit/logs"/>
<!-- <property name="LOG_PATH" value="/Users/minuk/data/xit/logs"/>-->
<!-- <property name="LOG_PATH" value="/data/xit/logs"/>-->
<property name="LOG_PATH" value="/Users/minuk/data/xit/logs"/>
</springProfile>
<springProfile name="dev,prod">
<property name="LOG_PATH" value="/data/xit/logs"/>
<!-- <property name="LOG_PATH" value="/Users/minuk/data/xit/logs"/>-->
<!-- <property name="LOG_PATH" value="/data/xit/logs"/>-->
<property name="LOG_PATH" value="/Users/minuk/data/xit/logs"/>
</springProfile>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<select id="selectBoardList" parameterType="com.xit.biz.ctgy.dto.BoardDto" resultType="camelCaseLinkedMap">
<sqlMapper>
<selectBoardList>
/* board-mapper|selectBoardList|julim */
SELECT MCB.ci_code,
MU.name,
@ -16,17 +17,37 @@
FROM min_civ_board680 MCB
LEFT OUTER JOIN min_userinfo MU
ON MCB.ci_id = MU.userid
<where>
<if test="ciTitle != null and ciTitle != ''">
WHERE 1=1
AND INSTR(MCB.ci_title, #{ciTitle}) > 0
AND MCB.ci_name like #{ciName}||'%'
ORDER BY MCB.ci_ref DESC,
MCB.ci_step ASC,
MCB.ci_code DESC
</selectBoardList>
<select id="selectBoardList2" parameterType="com.xit.biz.ctgy.dto.BoardDto" resultType="camelCaseLinkedMap">
/* board-mapper|selectBoardList2|julim */
SELECT MCB.ci_code,
MU.name,
MCB.ci_contentno,
MCB.ci_title,
MCB.ci_contents,
MCB.ci_nalja,
MCB.ci_step,
MCB.ci_revel,
MCB.ci_ref,
MCB.ci_hit,
MCB.ci_pass,
MCB.ci_id
FROM min_civ_board680 MCB
LEFT OUTER JOIN min_userinfo MU
ON MCB.ci_id = MU.userid
WHERE 1=1
AND INSTR(MCB.ci_title, #{ciTitle}) > 0
</if>
<if test='ciName != null and ciName != ""'>
AND MCB.ci_name like #{ciName}||'%'
</if>
</where>
ORDER BY MCB.ci_ref DESC,
MCB.ci_step ASC,
MCB.ci_code DESC
</select>
</mapper>
</sqlMapper>

@ -1,5 +1,6 @@
package com.xit.core;
import com.xit.core.util.CommUtil;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@ -24,7 +25,12 @@ class JasyptApplicationTests {
System.out.println(jasyptEncoding(password));
}
public String jasyptEncoding(String value) {
@Test
void xmlSql() {
CommUtil.getXmlSql("/sql/board2-mapper", "select");
}
private String jasyptEncoding(String value) {
String key = "xit_jasypt_key";
StandardPBEStringEncryptor pbeEnc = new StandardPBEStringEncryptor();

Loading…
Cancel
Save