Search in sources :

Example 1 with XPathParser

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);
        }
    }
}
Also used : XMLMapperEntityResolver(org.apache.ibatis.builder.xml.XMLMapperEntityResolver) XPathParser(org.apache.ibatis.parsing.XPathParser) RawSqlSource(org.apache.ibatis.scripting.defaults.RawSqlSource)

Example 2 with XPathParser

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();
}
Also used : XMLMapperEntityResolver(org.apache.ibatis.builder.xml.XMLMapperEntityResolver) XPathParser(org.apache.ibatis.parsing.XPathParser) HashMap(java.util.HashMap) XNode(org.apache.ibatis.parsing.XNode)

Example 3 with XPathParser

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()));
    }
}
Also used : XPathParser(org.apache.ibatis.parsing.XPathParser) Configuration(org.apache.ibatis.session.Configuration) ParameterMapping(org.apache.ibatis.mapping.ParameterMapping) HashMap(java.util.HashMap) BoundSql(org.apache.ibatis.mapping.BoundSql) InputStream(java.io.InputStream) XNode(org.apache.ibatis.parsing.XNode) MappedStatement(org.apache.ibatis.mapping.MappedStatement) MapperBuilderAssistant(org.apache.ibatis.builder.MapperBuilderAssistant) XMLStatementBuilder(org.apache.ibatis.builder.xml.XMLStatementBuilder)

Aggregations

XPathParser (org.apache.ibatis.parsing.XPathParser)3 HashMap (java.util.HashMap)2 XMLMapperEntityResolver (org.apache.ibatis.builder.xml.XMLMapperEntityResolver)2 XNode (org.apache.ibatis.parsing.XNode)2 InputStream (java.io.InputStream)1 MapperBuilderAssistant (org.apache.ibatis.builder.MapperBuilderAssistant)1 XMLStatementBuilder (org.apache.ibatis.builder.xml.XMLStatementBuilder)1 BoundSql (org.apache.ibatis.mapping.BoundSql)1 MappedStatement (org.apache.ibatis.mapping.MappedStatement)1 ParameterMapping (org.apache.ibatis.mapping.ParameterMapping)1 RawSqlSource (org.apache.ibatis.scripting.defaults.RawSqlSource)1 Configuration (org.apache.ibatis.session.Configuration)1