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

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

Introduction

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

Prototype

public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException 

Source Link

Document

Returns a resource on the classpath as a Stream object

Usage

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

License:Apache License

/**
 * <p>/*from  ww w . jav  a2  s.  c o m*/
 * ?XML(?resource,?Mybatisxml)
 * </p>
 *
 * @return true , false ?
 */
private boolean loadXmlResource() {
    boolean flag = true;
    // Spring may not know the real resource name so we check a flag
    // to prevent loading again a resource twice
    // this flag is set at XMLMapperBuilder#bindMapperForNamespace
    if (!configuration.isResourceLoaded("namespace:" + type.getName())) {
        String xmlResource = type.getName().replace('.', '/') + ".xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(type.getClassLoader(), xmlResource);
        } catch (IOException e) {
            // ignore, resource is not required
            flag = false;
        }
        if (inputStream != null) {
            MybatisXMLMapperBuilder xmlParser = new MybatisXMLMapperBuilder(inputStream,
                    assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName());
            xmlParser.parse();
        }
    }
    return flag;
}

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

License:Apache License

/**
 * ?XML(?resource,?Mybatisxml)//from w  w w .  j  av  a  2  s  .co  m
 *
 * @return boolean ture,flase?
 */
//TODO  boolean ? xxxMapper.xml
private boolean loadXmlResource() {
    boolean flag = true;
    // Spring may not know the real resource name so we check a flag
    // to prevent loading again a resource twice
    // this flag is set at MybatisXMLMapperBuilder#bindMapperForNamespace
    if (!configuration.isResourceLoaded("namespace:" + type.getName())) {
        String xmlResource = type.getName().replace('.', '/') + ".xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(type.getClassLoader(), xmlResource);
        } catch (IOException e) {
            // ignore, resource is not required
            flag = false;
        }
        if (inputStream != null) {
            MybatisXMLMapperBuilder xmlParser = new MybatisXMLMapperBuilder(inputStream,
                    assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName());
            xmlParser.parse();
        }
    }
    return flag;
}

From source file:com.qwazr.connectors.MybatisConnector.java

License:Apache License

@Override
public void load(File data_directory) throws IOException {

    final File configurationFile;
    if (configuration_file != null) {
        configurationFile = new File(configuration_file);
        if (!configurationFile.exists())
            throw new RuntimeException("The configuration file " + configuration_file + " does not exist");
    } else// w  w w.j  a va 2s.c o m
        configurationFile = null;
    final Properties props;
    if (properties != null) {
        props = new Properties();
        props.putAll(properties);
    } else
        props = null;

    final SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    final InputStream inputStream;
    if (configurationFile != null)
        inputStream = new FileInputStream(configurationFile);
    else
        inputStream = Resources.getResourceAsStream(ClassLoaderManager.classLoader,
                configuration_resource != null ? configuration_resource : default_configuration);
    try {
        if (environment != null) {
            if (props != null)
                sqlSessionFactory = builder.build(inputStream, environment, props);
            else
                sqlSessionFactory = builder.build(inputStream, environment);
        } else {
            if (props != null)
                sqlSessionFactory = builder.build(inputStream, props);
            else
                sqlSessionFactory = builder.build(inputStream);
        }
    } finally {
        IOUtils.close(inputStream);
    }
}

From source file:com.tojsq.cdi.SqlSessionFactoryProvider.java

@Produces
@ApplicationScoped/*  w ww. ja  v a2s.co m*/
public SqlSessionFactory produceFactory() throws IOException {
    InputStream inputStream = Resources.getResourceAsStream(SqlSessionFactoryProvider.class.getClassLoader(),
            MYBATIS_CONFIG_XML);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    return sqlSessionFactory;
}