Example usage for org.apache.ibatis.io Resources classForName

List of usage examples for org.apache.ibatis.io Resources classForName

Introduction

In this page you can find the example usage for org.apache.ibatis.io Resources classForName.

Prototype

public static Class<?> classForName(String className) throws ClassNotFoundException 

Source Link

Document

Loads a class

Usage

From source file:cc.oit.dao.impl.mybatis.session.XMLConfigBuilder.java

License:Apache License

private void typeAliasesElement(XNode parent) {
    if (parent != null) {
        for (XNode child : parent.getChildren()) {
            if ("package".equals(child.getName())) {
                String typeAliasPackage = child.getStringAttribute("name");
                configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
            } else {
                String alias = child.getStringAttribute("alias");
                String type = child.getStringAttribute("type");
                try {
                    Class<?> clazz = Resources.classForName(type);
                    if (alias == null) {
                        typeAliasRegistry.registerAlias(clazz);
                    } else {
                        typeAliasRegistry.registerAlias(alias, clazz);
                    }/*from  w  w w .  j  a va  2s.c  om*/
                } catch (ClassNotFoundException e) {
                    throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e,
                            e);
                }
            }
        }
    }
}

From source file:cc.oit.dao.impl.mybatis.session.XMLConfigBuilder.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);
                    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.");
                }/*  w w w.ja  va2 s  .  c om*/
            }
        }
    }
}

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

License:Apache License

private void loadCustomVfs(Properties props) throws ClassNotFoundException {
    String value = props.getProperty("vfsImpl");
    if (value != null) {
        String[] clazzes = value.split(",");
        for (String clazz : clazzes) {
            if (!clazz.isEmpty()) {
                @SuppressWarnings("unchecked")
                Class<? extends VFS> vfsImpl = (Class<? extends VFS>) Resources.classForName(clazz);
                configuration.setVfsImpl(vfsImpl);
            }/*from ww w .j a v a  2s  .c  o m*/
        }
    }
}

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

License:Apache License

/**
 * mybatis?Set?/*  w ww  .j  av a2s  . c  o m*/
 *
 * @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<>();
            resolverUtil.find(new ResolverUtil.IsA(Object.class), mapperPackage);
            Set<Class<? extends Class<?>>> mapperSet = resolverUtil.getClasses();
            mapper.addAll(mapperSet);
        } 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.baomidou.mybatisplus.MybatisXMLMapperBuilder.java

License:Apache License

private void bindMapperForNamespace() {
    String namespace = builderAssistant.getCurrentNamespace();
    if (namespace != null) {
        Class<?> boundType = null;
        try {//from   ww  w .j  a v  a2 s .  c o m
            boundType = Resources.classForName(namespace);
        } catch (ClassNotFoundException e) {
            // ignore, bound type is not required
        }
        if (boundType != null) {
            if (!configuration.hasMapper(boundType)) {
                // Spring may not know the real resource name so we set a
                // flag
                // to prevent loading again this resource from the mapper
                // interface
                // look at MapperAnnotationBuilder#loadXmlResource
                configuration.addLoadedResource("namespace:" + namespace);
                configuration.addMapper(boundType);
            }
            //TODO  CURD ? SQL
            if (BaseMapper.class.isAssignableFrom(boundType)) {
                GlobalConfiguration.getSqlInjector(configuration).inspectInject(builderAssistant, boundType);
            }
        }
    }
}

From source file:com.baomidou.mybatisplus.spring.MybatisMapperRefresh.java

License:Apache License

/**
 * mapper/*www.java2  s.  co m*/
 *
 * @throws Exception
 */
@SuppressWarnings("rawtypes")
private void refresh(Resource resource)
        throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
    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 (IOException e) {
        logger.error("Refresh IOException :" + e.getMessage());
    } finally {
        ErrorContext.instance().reset();
    }
}

From source file:com.dmm.framework.basedb.apache.ibatis.builder.xml.XMLMapperBuilder.java

License:Apache License

private void bindMapperForNamespace() {
    String namespace = builderAssistant.getCurrentNamespace();
    if (namespace != null) {
        Class<?> boundType = null;
        try {/*from   ww  w. j  a v a2  s  . c  o m*/
            boundType = Resources.classForName(namespace);
        } catch (ClassNotFoundException e) {
            // ignore, bound type is not required
        }
        if (boundType != null) {
            if (!configuration.hasMapper(boundType)) {
                // Spring may not know the real resource name so we set a
                // flag
                // to prevent loading again this resource from the mapper
                // interface
                // look at MapperAnnotationBuilder#loadXmlResource
                configuration.addLoadedResource("namespace:" + namespace);
                configuration.addMapper(boundType);
            }
        }
    }
}

From source file:com.glaf.core.jdbc.QueryHelper.java

License:Apache License

public List<Map<String, Object>> getResults(ResultSet rs) {
    logger.debug("--------------use mybatis results----------------");
    try {//from   w  ww  .  j a v  a 2 s  .c o  m
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        List<String> columns = new ArrayList<String>();
        List<TypeHandler<?>> typeHandlers = new ArrayList<TypeHandler<?>>();
        ResultSetMetaData rsmd = rs.getMetaData();
        for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
            columns.add(rsmd.getColumnLabel(i + 1));
            try {
                Class<?> type = Resources.classForName(rsmd.getColumnClassName(i + 1));
                TypeHandler<?> typeHandler = typeHandlerRegistry.getTypeHandler(type);
                if (typeHandler == null) {
                    typeHandler = typeHandlerRegistry.getTypeHandler(Object.class);
                }
                typeHandlers.add(typeHandler);
            } catch (Exception ex) {
                ex.printStackTrace();
                typeHandlers.add(typeHandlerRegistry.getTypeHandler(Object.class));
            }
        }
        while (rs.next()) {
            Map<String, Object> row = new HashMap<String, Object>();
            for (int i = 0, n = columns.size(); i < n; i++) {
                String name = columns.get(i);
                TypeHandler<?> handler = typeHandlers.get(i);
                Object value = handler.getResult(rs, name);
                row.put(name, value);
                if (value != null && value instanceof java.util.Date) {
                    java.util.Date date = (java.util.Date) value;
                    row.put(name + "_date", DateUtils.getDate(date));
                    row.put(name + "_datetime", DateUtils.getDateTime(date));
                }
            }
            list.add(row);
        }
        return list;
    } catch (SQLException ex) {
        logger.error(ex);
        ex.printStackTrace();
        throw new RuntimeException(ex);
    } finally {
        try {
            if (rs != null) {
                rs.close();
                rs = null;
            }
        } catch (SQLException e) {
        }
    }
}

From source file:com.jdy.ddj.common.utils.SqlSessionFactoryBean.java

License:Apache License

/**
 * ??/*from  ww w. j  ava  2s .com*/
 * @param parser
 * @param configuration
 */
private void parseTypeAliasesElement(XPathParser parser, Configuration configuration) {
    XNode typeAliasesElement = parser.evalNode("/configuration/typeAliases");
    if (typeAliasesElement != null) {
        for (XNode child : typeAliasesElement.getChildren()) {
            if ("package".equals(child.getName())) {
                String typeAliasPackage = child.getStringAttribute("name");
                configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
            } else {
                String alias = child.getStringAttribute("alias");
                String type = child.getStringAttribute("type");
                try {
                    Class<?> clazz = Resources.classForName(type);
                    if (alias == null) {
                        configuration.getTypeAliasRegistry().registerAlias(clazz);
                    } else {
                        configuration.getTypeAliasRegistry().registerAlias(alias, clazz);
                    }
                } catch (ClassNotFoundException e) {
                    throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e,
                            e);
                }
            }
        }
    }
}

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

License:Apache License

/**
 * mybatis?Set?/*ww  w.j  a  v a2 s. 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.");
            }
        }
    }
}