当前位置: 首页>>代码示例>>Java>>正文


Java Resources.getResourceAsStream方法代码示例

本文整理汇总了Java中org.apache.ibatis.io.Resources.getResourceAsStream方法的典型用法代码示例。如果您正苦于以下问题:Java Resources.getResourceAsStream方法的具体用法?Java Resources.getResourceAsStream怎么用?Java Resources.getResourceAsStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.ibatis.io.Resources的用法示例。


在下文中一共展示了Resources.getResourceAsStream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: loadXmlResource

import org.apache.ibatis.io.Resources; //导入方法依赖的package包/类
/**
 * <p>
 * 是否存在XML(该方法并不能客观的判断resource的路径,只是Mybatis默认认为的xml路径)
 * </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;
}
 
开发者ID:Caratacus,项目名称:mybatis-plus-mini,代码行数:30,代码来源:MybatisMapperAnnotationBuilder.java

示例2: mapperElement

import org.apache.ibatis.io.Resources; //导入方法依赖的package包/类
private void mapperElement(XNode parent) throws Exception {
    /**
     * 定义集合 用来分类放置mybatis的Mapper与XML 按顺序依次遍历
     */
    if (parent != null) {
        //指定在classpath中的mapper文件
        Set<String> resources = new HashSet<>();
        //指向一个mapper接口
        Set<Class<?>> mapperClasses = new HashSet<>();
        setResource(parent, resources, mapperClasses);
        // 依次遍历 首先 resource 然后 mapper
        for (String resource : resources) {
            ErrorContext.instance().resource(resource);
            InputStream inputStream = Resources.getResourceAsStream(resource);
            //TODO
            MybatisXMLMapperBuilder mapperParser = new MybatisXMLMapperBuilder(inputStream, configuration, resource,
                    configuration.getSqlFragments());
            mapperParser.parse();
        }
        for (Class<?> mapper : mapperClasses) {
            //TODO
            configuration.addMapper(mapper);
        }
    }
}
 
开发者ID:Caratacus,项目名称:mybatis-plus-mini,代码行数:26,代码来源:MybatisXMLConfigBuilder.java

示例3: BaseDao

import org.apache.ibatis.io.Resources; //导入方法依赖的package包/类
BaseDao() {
    //判断SessionFactory是否初始化过,初始化过后就不重复初始化,保证系统的运行效率。
    if (!isInit) {
        try {
            //读入Mybatis配置文件
            inputStream = Resources.getResourceAsStream(resource);
            //通过配置文件构建sql数据工厂
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            System.out.println("getConnectionSuccful");
            isInit = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        System.out.println("already init");
    }
}
 
开发者ID:okingjerryo,项目名称:WeiMusicCommunity-server,代码行数:18,代码来源:BaseDao.java

示例4: build

import org.apache.ibatis.io.Resources; //导入方法依赖的package包/类
/**
 * Create an instance of MyBatis.
 *
 * @param environment The dropwizard environment
 * @param config A Mybatis config object
 * @param dataSource
 * @param name The name of this mybatis factory used for metrics
 * @return An instance of MyBatis.
 */
public final SqlSessionFactory build(Environment environment,
        MyBatisConfiguration config,
        ManagedDataSource dataSource,
        String name) {

    SqlSessionFactory sessionFactory = null;

    // Try to use the mybatis configuration file if it is specified and exists.
    try (InputStream inputStream = Resources.getResourceAsStream(config.getConfigFile())) {
        sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    } catch (IOException ioe) {
        // Build session factory from configuration values given in the dropwizard config.
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        org.apache.ibatis.mapping.Environment myBatisEnvironment =
                new org.apache.ibatis.mapping.Environment(ENV_NAME, transactionFactory, dataSource);
        Configuration mybatisConfiguration = new Configuration(myBatisEnvironment);
        sessionFactory = new SqlSessionFactoryBuilder().build(mybatisConfiguration);
    }

    environment.lifecycle().manage(dataSource);
    environment.healthChecks().register(name,
            new MyBatisHealthCheck(sessionFactory, config.getConfig().getValidationQuery()));

    return sessionFactory;
}
 
开发者ID:cvent,项目名称:dropwizard-mybatis,代码行数:35,代码来源:MyBatisFactory.java

示例5: loadXmlResource

import org.apache.ibatis.io.Resources; //导入方法依赖的package包/类
private void loadXmlResource() {
  // 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
    }
    if (inputStream != null) {
      XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName());
      xmlParser.parse();
    }
  }
}
 
开发者ID:yuexiahandao,项目名称:MybatisCode,代码行数:19,代码来源:MapperAnnotationBuilder.java

示例6: shouldTestXPathParserMethods

import org.apache.ibatis.io.Resources; //导入方法依赖的package包/类
@Test
public void shouldTestXPathParserMethods() throws Exception {
  String resource = "resources/nodelet_test.xml";
  InputStream inputStream = Resources.getResourceAsStream(resource);
  XPathParser parser = new XPathParser(inputStream, false, null, null);
  assertEquals((Long)1970l, parser.evalLong("/employee/birth_date/year"));
  assertEquals((short) 6, (short) parser.evalShort("/employee/birth_date/month"));
  assertEquals((Integer) 15, parser.evalInteger("/employee/birth_date/day"));
  assertEquals((Float) 5.8f, parser.evalFloat("/employee/height"));
  assertEquals((Double) 5.8d, parser.evalDouble("/employee/height"));
  assertEquals("${id_var}", parser.evalString("/employee/@id"));
  assertEquals(Boolean.TRUE, parser.evalBoolean("/employee/active"));
  assertEquals("<id>${id_var}</id>", parser.evalNode("/employee/@id").toString().trim());
  assertEquals(7, parser.evalNodes("/employee/*").size());
  XNode node = parser.evalNode("/employee/height");
  assertEquals("employee/height", node.getPath());
  assertEquals("employee[${id_var}]_height", node.getValueBasedIdentifier());
}
 
开发者ID:yuexiahandao,项目名称:MybatisCode,代码行数:19,代码来源:XPathParserTest.java

示例7: mappedStatementWithOptions

import org.apache.ibatis.io.Resources; //导入方法依赖的package包/类
@Test
public void mappedStatementWithOptions() throws Exception {
  Configuration configuration = new Configuration();
  String resource = "org/apache/ibatis/builder/AuthorMapper.xml";
  InputStream inputStream = Resources.getResourceAsStream(resource);
  XMLMapperBuilder builder = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
  builder.parse();

  MappedStatement mappedStatement = configuration.getMappedStatement("selectWithOptions");
  assertThat(mappedStatement.getFetchSize(), is(200));
  assertThat(mappedStatement.getTimeout(), is(10));
  assertThat(mappedStatement.getStatementType(), is(StatementType.PREPARED));
  assertThat(mappedStatement.getResultSetType(), is(ResultSetType.SCROLL_SENSITIVE));
  assertThat(mappedStatement.isFlushCacheRequired(), is(false));
  assertThat(mappedStatement.isUseCache(), is(false));

}
 
开发者ID:yuexiahandao,项目名称:MybatisCode,代码行数:18,代码来源:XmlMapperBuilderTest.java

示例8: addSqlMappings

import org.apache.ibatis.io.Resources; //导入方法依赖的package包/类
private static void addSqlMappings(Configuration conf, String mapperFilePath) {
	InputStream is = null;
	try {
		//is = new FileInputStream(mapperFilePath);
		is = Resources.getResourceAsStream(mapperFilePath);
		XMLMapperBuilder xmlParser = new XMLMapperBuilder(is, conf, "mapper.xml", conf.getSqlFragments());
		xmlParser.parse();
	} catch (IOException ex) {
		ex.printStackTrace();
	} finally {
		if(is != null){
			try {
				is.close();
			} catch (IOException e) {
			}
		}
	}
}
 
开发者ID:gncloud,项目名称:fastcatsearch3,代码行数:19,代码来源:MyBatisTest.java

示例9: loadXmlResource

import org.apache.ibatis.io.Resources; //导入方法依赖的package包/类
private void loadXmlResource() {
    // 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
        }
        if (inputStream != null) {
            XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName());
            xmlParser.parse();
        }
    }
}
 
开发者ID:baomidou,项目名称:mybatis-plus,代码行数:19,代码来源:MybatisMapperAnnotationBuilder.java

示例10: createDB

import org.apache.ibatis.io.Resources; //导入方法依赖的package包/类
/**
 * 创建数据库
 * @param resource
 * @throws ClassNotFoundException
 * @throws SQLException
 * @throws IOException
 */
public static void createDB(String resource) throws ClassNotFoundException, SQLException, IOException {
    String driver = properties.getProperty("driver");
    String url = properties.getProperty("url");
    String username = properties.getProperty("username");
    String password = properties.getProperty("password");
    // 获取connection
    Class.forName(driver);
    Connection connection = DriverManager.getConnection(url, username, password);

    Statement statement = connection.createStatement();
    // 获取建表和初始化sql
    InputStream inputStream = Resources.getResourceAsStream(resource);
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    // 读取sql语句执行
    StringBuffer sb = new StringBuffer();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        sb.append(line).append("\n");
        if (line.matches(".*;$")) {
            statement.execute(sb.toString());
            sb.setLength(0);
        }
    }
    bufferedReader.close();
    inputStreamReader.close();
    inputStream.close();
    statement.close();
    connection.close();
}
 
开发者ID:itfsw,项目名称:mybatis-generator-plugin,代码行数:38,代码来源:DBHelper.java

示例11: init

import org.apache.ibatis.io.Resources; //导入方法依赖的package包/类
public static void init() {
	String resource = "mybatis-config.xml";
	InputStream inputStream = null;
	
	try {
		inputStream = Resources.getResourceAsStream(resource);
	} catch (IOException e) {
		e.printStackTrace();
	}
	
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
	session = sqlSessionFactory.openSession();
}
 
开发者ID:zhoukekestar,项目名称:java-notes,代码行数:14,代码来源:DB.java

示例12: main

import org.apache.ibatis.io.Resources; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
	String resource = "mybatis.xml";
	InputStream inputStream = Resources.getResourceAsStream(resource);
	sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

	SqlSession session = sqlSessionFactory.openSession();
	User name = session.selectOne("mapper.user.getUserByID", 2);

	System.out.println("Hello " + name.getName() + "!");
}
 
开发者ID:zhoukekestar,项目名称:java-notes,代码行数:11,代码来源:App.java

示例13: session

import org.apache.ibatis.io.Resources; //导入方法依赖的package包/类
/**
 * 根据mybatis配置获取SqlSession,这个方法在测试Mapper时候不是很靠谱,需检测
 *
 * 此方法未设定mapper一些必要设定的属性,执行会报错 不要使用
 *
 * @return
 * @throws IOException
 */
@Deprecated
public static SqlSession session() throws IOException {
  String resource = "database/mybatis-config-javaapi.xml";
  InputStream inputStream = Resources.getResourceAsStream(resource);
  SqlSessionFactory sqlSessionFactory = (new SqlSessionFactoryBuilder()).build(inputStream);
  SqlSession sqlSession = sqlSessionFactory.openSession();
  return sqlSession;
}
 
开发者ID:MiniPa,项目名称:cjs_ssms,代码行数:17,代码来源:MybatisHelper.java

示例14: create

import org.apache.ibatis.io.Resources; //导入方法依赖的package包/类
/**
 * Factory method to create session factory based on config,
 * environment from config and properties file to populate parameters in config ${param}
 */
public SqlSessionFactory create() throws IOException {
    InputStream inputStream = null;
    Properties properties = null;
    inputStream = Resources.getResourceAsStream(configFile);
    properties = Resources.getResourceAsProperties("mybatis/config/mybatis.properties");
    return sqlSessionFactoryBuilder.build(inputStream, environment, properties);
}
 
开发者ID:Iurii-Dziuban,项目名称:mybatis-samples,代码行数:12,代码来源:CustomSqlSessionFactoryBuilder.java

示例15: getInputSource

import org.apache.ibatis.io.Resources; //导入方法依赖的package包/类
private InputSource getInputSource(String path, String publicId, String systemId) {
  InputSource source = null;
  if (path != null) {
    try {
      InputStream in = Resources.getResourceAsStream(path);
      source = new InputSource(in);
      source.setPublicId(publicId);
      source.setSystemId(systemId);        
    } catch (IOException e) {
      // ignore, null is ok
    }
  }
  return source;
}
 
开发者ID:yuexiahandao,项目名称:MybatisCode,代码行数:15,代码来源:XMLMapperEntityResolver.java


注:本文中的org.apache.ibatis.io.Resources.getResourceAsStream方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。