Example usage for org.apache.ibatis.parsing XNode getStringAttribute

List of usage examples for org.apache.ibatis.parsing XNode getStringAttribute

Introduction

In this page you can find the example usage for org.apache.ibatis.parsing XNode getStringAttribute.

Prototype

public String getStringAttribute(String name) 

Source Link

Usage

From source file:com.mybatisX.core.MybatisXMLConfigBuilder.java

License:Apache License

/**
 * mybatis?Set?//  w  w  w.j a va  2s . c om
 *
 * @param parent 
 * @param resources
 * @param mapper
 * @throws ClassNotFoundException
 */
private void setResource(XNode parent, Set<String> resources, Set<Class<?>> mapper)
        throws ClassNotFoundException {
    for (XNode child : parent.getChildren()) {
        if ("package".equals(child.getName())) {
            String mapperPackage = child.getStringAttribute("name");
            ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<Class<?>>();
            resolverUtil.find(new ResolverUtil.IsA(Object.class), mapperPackage);
            Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
            for (Class<?> mapperClass : mapperSet) {
                mapper.add(mapperClass);
            }
        } else {
            String resource = child.getStringAttribute("resource");
            String url = child.getStringAttribute("url");
            String mapperClass = child.getStringAttribute("class");
            if (resource != null && url == null && mapperClass == null) {
                resources.add(resource);
            } else if (resource == null && url != null && mapperClass == null) {
                resources.add(url);
            } else if (resource == null && url == null && mapperClass != null) {
                Class<?> mapperInterface = Resources.classForName(mapperClass);
                mapper.add(mapperInterface);
            } else {
                throw new BuilderException(
                        "A mapper element may only specify a url, resource or class, but not more than one.");
            }
        }
    }
}

From source file:com.mybatisX.core.MybatisXMLMapperBuilder.java

License:Apache License

private void configurationElement(XNode context) {
    try {//from   w  w w  .  ja  v a2 s . c  om
        String namespace = context.getStringAttribute("namespace");
        if (StringUtils.isEmpty(namespace)) {
            throw new BuilderException("Mapper's namespace cannot be empty");
        }
        builderAssistant.setCurrentNamespace(namespace);
        cacheRefElement(context.evalNode("cache-ref"));
        cacheElement(context.evalNode("cache"));
        parameterMapElement(context.evalNodes("/mapper/parameterMap"));
        resultMapElements(context.evalNodes("/mapper/resultMap"));
        sqlElement(context.evalNodes("/mapper/sql"));
        buildStatementFromContext(context.evalNodes("select|insert|update|delete"));
    } catch (Exception e) {
        throw new BuilderException("Error parsing Mapper XML. Cause: " + e, e);
    }
}

From source file:com.mybatisX.core.MybatisXMLMapperBuilder.java

License:Apache License

private ResultMapping buildResultMappingFromContext(XNode context, Class<?> resultType, List<ResultFlag> flags)
        throws Exception {
    String property = context.getStringAttribute("property");
    String column = context.getStringAttribute("column");
    String javaType = context.getStringAttribute("javaType");
    String jdbcType = context.getStringAttribute("jdbcType");
    String nestedSelect = context.getStringAttribute("select");
    String nestedResultMap = context.getStringAttribute("resultMap",
            processNestedResultMappings(context, Collections.<ResultMapping>emptyList()));
    String notNullColumn = context.getStringAttribute("notNullColumn");
    String columnPrefix = context.getStringAttribute("columnPrefix");
    String typeHandler = context.getStringAttribute("typeHandler");
    String resultSet = context.getStringAttribute("resultSet");
    String foreignColumn = context.getStringAttribute("foreignColumn");
    boolean lazy = "lazy".equals(
            context.getStringAttribute("fetchType", configuration.isLazyLoadingEnabled() ? "lazy" : "eager"));
    Class<?> javaTypeClass = resolveClass(javaType);
    @SuppressWarnings("unchecked")
    Class<? extends TypeHandler<?>> typeHandlerClass = (Class<? extends TypeHandler<?>>) resolveClass(
            typeHandler);// w  ww .ja va2 s  . c o  m
    JdbcType jdbcTypeEnum = resolveJdbcType(jdbcType);
    return builderAssistant.buildResultMapping(resultType, property, column, javaTypeClass, jdbcTypeEnum,
            nestedSelect, nestedResultMap, notNullColumn, columnPrefix, typeHandlerClass, flags, resultSet,
            foreignColumn, lazy);
}

From source file:com.mybatisX.spring.MybatisMapperRefresh.java

License:Apache License

/**
 * mapper/*from  ww  w.j  a v  a  2s. c om*/
 * 
 * @throws Exception
 */
@SuppressWarnings("rawtypes")
private void refresh(Resource resource) throws Exception {
    this.configuration = sqlSessionFactory.getConfiguration();
    boolean isSupper = configuration.getClass().getSuperclass() == Configuration.class;
    try {
        Field loadedResourcesField = isSupper
                ? configuration.getClass().getSuperclass().getDeclaredField("loadedResources")
                : configuration.getClass().getDeclaredField("loadedResources");
        loadedResourcesField.setAccessible(true);
        Set loadedResourcesSet = ((Set) loadedResourcesField.get(configuration));
        XPathParser xPathParser = new XPathParser(resource.getInputStream(), true, configuration.getVariables(),
                new XMLMapperEntityResolver());
        XNode context = xPathParser.evalNode("/mapper");
        String namespace = context.getStringAttribute("namespace");
        Field field = MapperRegistry.class.getDeclaredField("knownMappers");
        field.setAccessible(true);
        Map mapConfig = (Map) field.get(configuration.getMapperRegistry());
        mapConfig.remove(Resources.classForName(namespace));
        loadedResourcesSet.remove(resource.toString());
        configuration.getCacheNames().remove(namespace);
        cleanParameterMap(context.evalNodes("/mapper/parameterMap"), namespace);
        cleanResultMap(context.evalNodes("/mapper/resultMap"), namespace);
        cleanKeyGenerators(context.evalNodes("insert|update"), namespace);
        cleanSqlElement(context.evalNodes("/mapper/sql"), namespace);
        XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(resource.getInputStream(),
                sqlSessionFactory.getConfiguration(), // sql??
                resource.toString(), sqlSessionFactory.getConfiguration().getSqlFragments());
        xmlMapperBuilder.parse();
        logger.debug("refresh:" + resource + ",success!");
    } catch (Exception e) {
        throw new NestedIOException("Failed to parse mapping resource: '" + resource + "'", e);
    } finally {
        ErrorContext.instance().reset();
    }
}

From source file:com.tj.mybatisplus.MybatisXMLConfigBuilder.java

License:Apache License

private void reflectionFactoryElement(XNode context) throws Exception {
    if (context != null) {
        String type = context.getStringAttribute("type");
        ReflectorFactory factory = (ReflectorFactory) resolveClass(type).newInstance();
        configuration.setReflectorFactory(factory);
    }/*from w  w  w .  ja  v a2 s  .  co m*/
}

From source file:com.tj.mybatisplus.MybatisXMLConfigBuilder.java

License:Apache License

private void mapperElement(XNode parent) throws Exception {
    if (parent != null) {
        for (XNode child : parent.getChildren()) {
            if ("package".equals(child.getName())) {
                String mapperPackage = child.getStringAttribute("name");
                configuration.addMappers(mapperPackage);
            } else {
                String resource = child.getStringAttribute("resource");
                String url = child.getStringAttribute("url");
                String mapperClass = child.getStringAttribute("class");
                if (resource != null && url == null && mapperClass == null) {
                    ErrorContext.instance().resource(resource);
                    InputStream inputStream = Resources.getResourceAsStream(resource);
                    //TODO
                    MybatisXMLMapperBuilder mapperParser = new MybatisXMLMapperBuilder(inputStream,
                            configuration, resource, configuration.getSqlFragments());
                    mapperParser.parse();
                } else if (resource == null && url != null && mapperClass == null) {
                    ErrorContext.instance().resource(url);
                    InputStream inputStream = Resources.getUrlAsStream(url);
                    //TODO
                    MybatisXMLMapperBuilder mapperParser = new MybatisXMLMapperBuilder(inputStream,
                            configuration, url, configuration.getSqlFragments());
                    mapperParser.parse();
                } else if (resource == null && url == null && mapperClass != null) {
                    Class<?> mapperInterface = Resources.classForName(mapperClass);
                    configuration.addMapper(mapperInterface);
                } else {
                    throw new BuilderException(
                            "A mapper element may only specify a url, resource or class, but not more than one.");
                }/*from w w w.ja v  a 2 s  .c  o m*/
            }
        }
    }
}

From source file:org.alfresco.ibatis.HierarchicalXMLConfigBuilder.java

License:Open Source License

private void databaseIdProviderElement(XNode context) throws Exception {
    DatabaseIdProvider databaseIdProvider = null;
    if (context != null) {
        String type = context.getStringAttribute("type");
        Properties properties = context.getChildrenAsProperties();
        databaseIdProvider = (DatabaseIdProvider) resolveClass(type).newInstance();
        databaseIdProvider.setProperties(properties);
    }// www  .  ja  va2 s .c o  m
    Environment environment = configuration.getEnvironment();
    if (environment != null && databaseIdProvider != null) {
        String databaseId = databaseIdProvider.getDatabaseId(environment.getDataSource());
        configuration.setDatabaseId(databaseId);
    }
}

From source file:org.alfresco.ibatis.HierarchicalXMLConfigBuilder.java

License:Open Source License

private void mapperElement(XNode parent) throws Exception {
    if (parent != null) {
        for (XNode child : parent.getChildren()) {
            if ("package".equals(child.getName())) {
                String mapperPackage = child.getStringAttribute("name");
                configuration.addMappers(mapperPackage);
            } else {
                String resource = child.getStringAttribute("resource");
                String url = child.getStringAttribute("url");
                String mapperClass = child.getStringAttribute("class");
                if (resource != null && url == null && mapperClass == null) {
                    ErrorContext.instance().resource(resource);

                    // // EXTENDED
                    // inputStream = Resources.getResourceAsStream(resource);
                    InputStream inputStream = null;
                    Resource res = resourceLoader.getResource(resource);
                    if (res != null && res.exists()) {
                        inputStream = res.getInputStream();
                    } else {
                        throw new BuilderException("Failed to get resource: " + resource);
                    }//from w  w w. j  a va2s  .com

                    //InputStream inputStream = Resources.getResourceAsStream(resource);
                    XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource,
                            configuration.getSqlFragments());
                    mapperParser.parse();
                } else if (resource == null && url != null && mapperClass == null) {
                    ErrorContext.instance().resource(url);
                    InputStream inputStream = Resources.getUrlAsStream(url);
                    XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url,
                            configuration.getSqlFragments());
                    mapperParser.parse();
                } else if (resource == null && url == null && mapperClass != null) {
                    Class<?> mapperInterface = Resources.classForName(mapperClass);
                    configuration.addMapper(mapperInterface);
                } else {
                    throw new BuilderException(
                            "A mapper element may only specify a url, resource or class, but not more than one.");
                }
            }
        }
    }
}