use of org.apache.ibatis.parsing.XPathParser in project mybatis-3 by mybatis.
the class XMLLanguageDriver method createSqlSource.
@Override
public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
// issue #3
if (script.startsWith("<script>")) {
XPathParser parser = new XPathParser(script, false, configuration.getVariables(), new XMLMapperEntityResolver());
return createSqlSource(configuration, parser.evalNode("/script"), parameterType);
} else {
// issue #127
script = PropertyParser.parse(script, configuration.getVariables());
TextSqlNode textSqlNode = new TextSqlNode(script);
if (textSqlNode.isDynamic()) {
return new DynamicSqlSource(configuration, textSqlNode);
} else {
return new RawSqlSource(configuration, script, parameterType);
}
}
}
use of org.apache.ibatis.parsing.XPathParser in project jeesuite-libs by vakinge.
the class MybatisMapperParser method parseMapperFile.
// private synchronized static void doParse(){
// if(caches.isEmpty()){
// try {
// URL resource = Thread.currentThread().getContextClassLoader().getResource(mapperBaseDir);
// if (resource != null) {
// if (resource.getProtocol().equals("file")) {
// File mapperDir = new File(resource.getPath());
// File[] files = mapperDir.listFiles();
// for (File f : files) {
// if(f.getName().endsWith(mapperFileSuffix)){
// parseMapperFile(new FileInputStream(f));
// }
// }
// } else if (resource.getProtocol().equals("jar")) {
// String jarFilePath = resource.getFile();
//
// //file:/Users/vakinge/.m2/repository/com/jeesuite/demo/demo-dao/1.0-SNAPSHOT/demo-dao-1.0-SNAPSHOT.jar!/mapper;
// jarFilePath = jarFilePath.split("jar!")[0] + "jar";
// jarFilePath = jarFilePath.substring("file:".length());
// log.info("mapper file in jar:{}",jarFilePath);
// jarFilePath = java.net.URLDecoder.decode(jarFilePath, "UTF-8");
//
// JarFile jarFile = new JarFile(jarFilePath);
//
// List<String> fileNames = listFiles(jarFile, mapperFileSuffix);
// if (fileNames != null && fileNames.size() > 0) {
// for (String fileName : fileNames) {
// InputStream inputStream = jarFile.getInputStream(jarFile.getJarEntry(fileName));
// parseMapperFile(inputStream);
// try {inputStream.close();} catch (Exception e) {}
// }
// }
//
// jarFile.close();
// } else {
// log.error("mapper dir is in unsurport protocol");
// }
// } else {
// log.error("can not find mapper dir");
// }
// } catch (Exception e) {
// log.error("解析mapper文件异常", e);
//
// throw new RuntimeException("解析mapper文件异常");
// }
// }
// }
private static void parseMapperFile(String fileName, InputStream inputStream) throws Exception {
XPathParser parser = new XPathParser(inputStream, true, null, new XMLMapperEntityResolver());
XNode evalNode = parser.evalNode("/mapper");
String mapperClass = evalNode.getStringAttribute("namespace");
String entityClass = null;
EntityInfo entityInfo = null;
Map<String, String> includes = new HashMap<>();
List<XNode> children = evalNode.getChildren();
for (XNode xNode : children) {
if ("sql".equalsIgnoreCase(xNode.getName())) {
includes.put(xNode.getStringAttribute("id"), xNode.getStringBody());
continue;
}
if (!"resultMap".equals(xNode.getName()))
continue;
if (!"BaseResultMap".equals(xNode.getStringAttribute("id")))
continue;
entityClass = xNode.getStringAttribute("type");
entityInfo = new EntityInfo(mapperClass, entityClass);
if (entityInfo.getErrorMsg() != null) {
log.warn("==================\n>>{},skip!!!!\n===============", entityInfo.getErrorMsg());
continue;
}
entityInfos.add(entityInfo);
mapperRalateEntitys.put(mapperClass, entityInfo);
//
List<XNode> resultNodes = xNode.getChildren();
for (XNode xNode2 : resultNodes) {
parseResultNode(entityInfo, xNode2);
}
}
if (entityInfo.getErrorMsg() != null) {
return;
}
for (XNode xNode : children) {
if ("select|insert|update|delete".contains(xNode.getName().toLowerCase())) {
String sql = parseSql(fileName, xNode, includes);
entityInfo.addSql(xNode.getStringAttribute("id"), sql);
}
}
inputStream.close();
}
use of org.apache.ibatis.parsing.XPathParser in project yyl_example by Relucent.
the class MybatisBoundSqlExample method main.
public static void main(String[] args) throws IOException {
// 解决一个不同包引起的冲突
System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
Configuration configuration = new Configuration();
MapperBuilderAssistant builderAssistant = new MapperBuilderAssistant(configuration, null);
// 定义 NameSpace
builderAssistant.setCurrentNamespace("sample");
try (InputStream input = MybatisBoundSqlExample.class.getResourceAsStream("sample-mapper.xml")) {
// XML XPath解析器
XPathParser parser = new XPathParser(input);
// 获得select节点
for (XNode node : parser.evalNodes("/mapper/select")) {
// 解析节点
XMLStatementBuilder statementParser = new XMLStatementBuilder(configuration, builderAssistant, node, null);
// 这个操作会创建 MappedStatement ,并保存到configuration中
statementParser.parseStatementNode();
}
}
// 获得 getById
MappedStatement ms = configuration.getMappedStatement("sample.selectList");
// 定义参数
Map<String, String> parameters = new HashMap<>();
parameters.put("id", "1");
// 根据参数获得 BoundSql
BoundSql boundSql = ms.getBoundSql(parameters);
// 打印最终SQL 和 参数
System.out.println("[SQL]");
System.out.println(boundSql.getSql());
System.out.println("[PARAMETER]");
for (ParameterMapping pm : boundSql.getParameterMappings()) {
System.out.println(pm.getProperty() + "=>" + parameters.get(pm.getProperty()));
}
}
Aggregations