From 5c71aca459618fa0073aec721ef16850321ff213 Mon Sep 17 00:00:00 2001 From: "Jonguk. Lim" Date: Sat, 25 Jun 2022 19:00:16 +0900 Subject: [PATCH] feat: xml sql parsing --- .../ctgy/v2/service/impl/BoardService.java | 9 +- .../com/xit/core/config/MvcWebConfig.java | 3 +- src/main/java/com/xit/core/util/CommUtil.java | 160 ++---------------- src/main/java/com/xit/core/util/DBUtils.java | 100 +++++++++++ src/main/resources/config/conf.yml | 2 +- src/main/resources/logback-spring.xml | 8 +- .../biz/ctgy => sql}/board2-mapper.xml | 41 +++-- .../com/xit/core/JasyptApplicationTests.java | 8 +- 8 files changed, 166 insertions(+), 165 deletions(-) create mode 100644 src/main/java/com/xit/core/util/DBUtils.java rename src/main/resources/{mybatis-mapper/biz/ctgy => sql}/board2-mapper.xml (50%) diff --git a/src/main/java/com/xit/biz/ctgy/v2/service/impl/BoardService.java b/src/main/java/com/xit/biz/ctgy/v2/service/impl/BoardService.java index 7283be0..d61d8f8 100644 --- a/src/main/java/com/xit/biz/ctgy/v2/service/impl/BoardService.java +++ b/src/main/java/com/xit/biz/ctgy/v2/service/impl/BoardService.java @@ -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> 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) diff --git a/src/main/java/com/xit/core/config/MvcWebConfig.java b/src/main/java/com/xit/core/config/MvcWebConfig.java index 6c0d364..42e8070 100644 --- a/src/main/java/com/xit/core/config/MvcWebConfig.java +++ b/src/main/java/com/xit/core/config/MvcWebConfig.java @@ -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 { diff --git a/src/main/java/com/xit/core/util/CommUtil.java b/src/main/java/com/xit/core/util/CommUtil.java index 75bc38e..0b89b36 100644 --- a/src/main/java/com/xit/core/util/CommUtil.java +++ b/src/main/java/com/xit/core/util/CommUtil.java @@ -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 parameterMappings = new ArrayList<>(boundSql.getParameterMappings()); - Map 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 parameterMappings = new ArrayList<>(boundSql.getParameterMappings()); - Map 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 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); - } - } } diff --git a/src/main/java/com/xit/core/util/DBUtils.java b/src/main/java/com/xit/core/util/DBUtils.java new file mode 100644 index 0000000..349b183 --- /dev/null +++ b/src/main/java/com/xit/core/util/DBUtils.java @@ -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 parameterMappings = new ArrayList<>(boundSql.getParameterMappings()); + Map 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); + } + } +} diff --git a/src/main/resources/config/conf.yml b/src/main/resources/config/conf.yml index 3d295c9..2f77382 100644 --- a/src/main/resources/config/conf.yml +++ b/src/main/resources/config/conf.yml @@ -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 diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml index d863169..0070350 100644 --- a/src/main/resources/logback-spring.xml +++ b/src/main/resources/logback-spring.xml @@ -4,12 +4,12 @@ - - + + - - + + diff --git a/src/main/resources/mybatis-mapper/biz/ctgy/board2-mapper.xml b/src/main/resources/sql/board2-mapper.xml similarity index 50% rename from src/main/resources/mybatis-mapper/biz/ctgy/board2-mapper.xml rename to src/main/resources/sql/board2-mapper.xml index 1bdd13c..8c7d6a3 100644 --- a/src/main/resources/mybatis-mapper/biz/ctgy/board2-mapper.xml +++ b/src/main/resources/sql/board2-mapper.xml @@ -1,5 +1,6 @@ - + - \ No newline at end of file + + + \ No newline at end of file diff --git a/src/test/java/com/xit/core/JasyptApplicationTests.java b/src/test/java/com/xit/core/JasyptApplicationTests.java index 6a81ba7..52cf954 100644 --- a/src/test/java/com/xit/core/JasyptApplicationTests.java +++ b/src/test/java/com/xit/core/JasyptApplicationTests.java @@ -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();