本文整理匯總了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;
}