本文整理汇总了Java中org.apache.ibatis.io.Resources类的典型用法代码示例。如果您正苦于以下问题:Java Resources类的具体用法?Java Resources怎么用?Java Resources使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Resources类属于org.apache.ibatis.io包,在下文中一共展示了Resources类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: bindMapperForNamespace
import org.apache.ibatis.io.Resources; //导入依赖的package包/类
private void bindMapperForNamespace() {
String namespace = builderAssistant.getCurrentNamespace();
if (namespace != null) {
Class<?> boundType = null;
try {
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)) {
GlobalConfigUtils.getSqlInjector(configuration).inspectInject(builderAssistant, boundType);
}
}
}
}
示例2: 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;
}
示例3: typeAliasesElement
import org.apache.ibatis.io.Resources; //导入依赖的package包/类
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);
}
} catch (ClassNotFoundException e) {
throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e);
}
}
}
}
}
示例4: propertiesElement
import org.apache.ibatis.io.Resources; //导入依赖的package包/类
private void propertiesElement(XNode context) throws Exception {
if (context != null) {
Properties defaults = context.getChildrenAsProperties();
String resource = context.getStringAttribute("resource");
String url = context.getStringAttribute("url");
if (resource != null && url != null) {
throw new BuilderException("The properties element cannot specify both a URL and a resource based property file reference. Please specify one or the other.");
}
if (resource != null) {
defaults.putAll(Resources.getResourceAsProperties(resource));
} else if (url != null) {
defaults.putAll(Resources.getUrlAsProperties(url));
}
Properties vars = configuration.getVariables();
if (vars != null) {
defaults.putAll(vars);
}
parser.setVariables(defaults);
configuration.setVariables(defaults);
}
}
示例5: 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);
}
}
}
示例6: propertiesElement
import org.apache.ibatis.io.Resources; //导入依赖的package包/类
private void propertiesElement(XNode context) throws Exception {
if (context != null) {
Properties defaults = context.getChildrenAsProperties();
String resource = context.getStringAttribute("resource");
String url = context.getStringAttribute("url");
if (resource != null && url != null) {
throw new BuilderException("The properties element cannot specify both a URL and a resource based property file reference. Please specify one or the other.");
}
if (resource != null) {
defaults.putAll(Resources.getResourceAsProperties(resource));
} else if (url != null) {
defaults.putAll(Resources.getUrlAsProperties(url));
}
Properties vars = configuration.getVariables();
if (vars != null) {
defaults.putAll(vars);
}
parser.setVariables(defaults);
configuration.setVariables(defaults);
}
}
示例7: 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");
}
}
示例8: setUp
import org.apache.ibatis.io.Resources; //导入依赖的package包/类
@BeforeClass
public static void setUp() throws Exception {
System.out.println("Starting up CmsCmProcessor tests");
String resource = "mybatis-config.xml";
Reader reader = Resources.getResourceAsReader( resource );
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(reader);
session = sf.openSession();
clazzMapper = session.getMapper(ClazzMapper.class);
ciMapper = session.getMapper(CIMapper.class);
mdValidator = new CmsMdValidator();
cmValidator = new CmsCmValidator();
mdProcessor = new CmsMdProcessor();
mdProcessor.setClazzMapper( clazzMapper );
mdProcessor.setMdValidator(mdValidator);
cmProcessor = new CmsCmProcessor();
cmProcessor.setCiMapper(ciMapper);
cmProcessor.setMdProcessor(mdProcessor);
cmProcessor.setCmValidator(cmValidator);
RelationMapper relationMapper=session.getMapper(RelationMapper.class);
mdProcessor.setRelationMapper(relationMapper);
}
示例9: 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;
}
示例10: setUp
import org.apache.ibatis.io.Resources; //导入依赖的package包/类
@BeforeClass
public static void setUp() throws Exception {
// create a SqlSessionFactory
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/call_setters_on_nulls/mybatis-config-2.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.close();
// populate in-memory database
SqlSession session = sqlSessionFactory.openSession();
Connection conn = session.getConnection();
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/call_setters_on_nulls/CreateDB.sql");
ScriptRunner runner = new ScriptRunner(conn);
runner.setLogWriter(null);
runner.runScript(reader);
reader.close();
session.close();
}
示例11: classForName
import org.apache.ibatis.io.Resources; //导入依赖的package包/类
@Override
public Class classForName(String className, Map context) throws ClassNotFoundException {
Class<?> result = null;
if ((result = classes.get(className)) == null) {
try {
result = Resources.classForName(className);
} catch (ClassNotFoundException e1) {
if (className.indexOf('.') == -1) {
result = Resources.classForName("java.lang." + className);
classes.put("java.lang." + className, result);
}
}
classes.put(className, result);
}
return result;
}
示例12: setUp
import org.apache.ibatis.io.Resources; //导入依赖的package包/类
@BeforeClass
public static void setUp() throws Exception {
Connection conn = null;
try {
Class.forName("org.hsqldb.jdbcDriver");
conn = DriverManager.getConnection("jdbc:hsqldb:mem:gname", "sa", "");
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/quotedcolumnnames/CreateDB.sql");
ScriptRunner runner = new ScriptRunner(conn);
runner.setLogWriter(null);
runner.setErrorLogWriter(null);
runner.runScript(reader);
conn.commit();
reader.close();
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/quotedcolumnnames/MapperConfig.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.close();
} finally {
if (conn != null) {
conn.close();
}
}
}
示例13: setUp
import org.apache.ibatis.io.Resources; //导入依赖的package包/类
@BeforeClass
public static void setUp() throws Exception {
// create a SqlSessionFactory
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/extendresultmap/mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.close();
// populate in-memory database
SqlSession session = sqlSessionFactory.openSession();
Connection conn = session.getConnection();
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/extendresultmap/CreateDB.sql");
ScriptRunner runner = new ScriptRunner(conn);
runner.setLogWriter(null);
runner.runScript(reader);
reader.close();
session.close();
}
示例14: testGetMessageForEmptyDatabase
import org.apache.ibatis.io.Resources; //导入依赖的package包/类
@BeforeClass
public static void testGetMessageForEmptyDatabase() throws Exception {
final String resource = "org/apache/ibatis/submitted/initialized_collection_property/mybatis-config.xml";
Reader reader = Resources.getResourceAsReader(resource);
factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = factory.openSession();
Connection conn = session.getConnection();
ScriptRunner runner = new ScriptRunner(conn);
runner.setLogWriter(null);
runner.setErrorLogWriter(null);
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/initialized_collection_property/create.sql");
runner.runScript(reader);
session.close();
}
示例15: initDatabase
import org.apache.ibatis.io.Resources; //导入依赖的package包/类
@Before
public void initDatabase() throws Exception {
Connection conn = null;
try {
Class.forName("org.hsqldb.jdbcDriver");
conn = DriverManager.getConnection("jdbc:hsqldb:mem:force_flush_on_select", "sa", "");
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/force_flush_on_select/CreateDB.sql");
ScriptRunner runner = new ScriptRunner(conn);
runner.setLogWriter(null);
runner.setErrorLogWriter(null);
runner.runScript(reader);
conn.commit();
reader.close();
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/force_flush_on_select/ibatisConfig.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.close();
} finally {
if (conn != null) {
conn.close();
}
}
}