feat: xml sql parsing
parent
bb1c7f3447
commit
5c71aca459
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue