本文整理汇总了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;
}
示例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);
}
}
}
示例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");
}
}
示例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;
}
示例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();
}
}
}
示例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());
}
示例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));
}
示例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) {
}
}
}
}
示例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();
}
}
}
示例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();
}
示例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();
}
示例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() + "!");
}
示例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;
}
示例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);
}
示例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;
}