org.apache.ibatis.io.Resources Java Examples
The following examples show how to use
org.apache.ibatis.io.Resources.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MultipleCrossIncludeTest.java From mybaties with Apache License 2.0 | 6 votes |
@Test public void testMappedStatementCache() throws Exception { Reader configReader = Resources .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/MultipleCrossIncludeMapperConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader); configReader.close(); Configuration configuration = sqlSessionFactory.getConfiguration(); configuration.getMappedStatementNames(); MappedStatement selectPetStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePetMapper.select"); MappedStatement selectPersonStatement = configuration.getMappedStatement("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePersonMapper.select"); Cache cache = selectPetStatement.getCache(); assertEquals("org.apache.ibatis.submitted.xml_external_ref.MultipleCrossIncludePetMapper", cache.getId()); assertSame(cache, selectPersonStatement.getCache()); }
Example #2
Source File: QuotedColumnNamesTest.java From mybaties with Apache License 2.0 | 6 votes |
@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(); } } }
Example #3
Source File: HeavyInitialLoadTest.java From mybaties with Apache License 2.0 | 6 votes |
@BeforeClass public static void initSqlSessionFactory() throws Exception { Connection conn = null; try { Class.forName("org.hsqldb.jdbcDriver"); conn = DriverManager.getConnection("jdbc:hsqldb:mem:heavy_initial_load", "sa", ""); Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/heavy_initial_load/ibatisConfig.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); reader.close(); } finally { if (conn != null) { conn.close(); } } }
Example #4
Source File: ValueInMapTest.java From mybatis with Apache License 2.0 | 6 votes |
@BeforeClass public static void setUp() throws Exception { // create a SqlSessionFactory Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/valueinmap/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/valueinmap/CreateDB.sql"); ScriptRunner runner = new ScriptRunner(conn); runner.setLogWriter(null); runner.runScript(reader); reader.close(); session.close(); }
Example #5
Source File: SerializeCircularTest.java From mybaties with Apache License 2.0 | 6 votes |
private static void initDb(Connection conn) throws IOException, SQLException { try { Reader scriptReader = Resources .getResourceAsReader("org/apache/ibatis/submitted/serializecircular/CreateDB.sql"); ScriptRunner runner = new ScriptRunner(conn); runner.setLogWriter(null); runner.setErrorLogWriter(null); runner.runScript(scriptReader); conn.commit(); scriptReader.close(); } finally { if (conn != null) { conn.close(); } } }
Example #6
Source File: MybatisUtil.java From code with Apache License 2.0 | 6 votes |
public static void template(Callback callback) { SqlSession session = null; //1.读取配置文件 try (InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");) { //2.创建构建者对象 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); //3.创建 SqlSession 工厂对象 factory = builder.build(in); //4.创建 SqlSession 对象 session = factory.openSession(); //5.执行操作 callback.execute(session); //6.提交或回滚事务 session.commit(); } catch (IOException e) { if (session != null) { session.rollback(); } e.printStackTrace(); } finally { if (session != null) { session.close(); } } }
Example #7
Source File: BaseTest.java From mybaties with Apache License 2.0 | 6 votes |
@BeforeClass public static void setUp() throws Exception { // create an SqlSessionFactory Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/basetest/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/basetest/CreateDB.sql"); ScriptRunner runner = new ScriptRunner(conn); runner.setLogWriter(null); runner.runScript(reader); reader.close(); session.close(); }
Example #8
Source File: InheritanceTest.java From mybaties with Apache License 2.0 | 6 votes |
@BeforeClass public static void setUp() throws Exception { // create a SqlSessionFactory Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/inheritance/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/inheritance/CreateDB.sql"); ScriptRunner runner = new ScriptRunner(conn); runner.setLogWriter(null); runner.runScript(reader); reader.close(); session.close(); }
Example #9
Source File: ParametrizedListTest.java From mybatis with Apache License 2.0 | 6 votes |
@Before public void setUp() throws Exception { Connection conn = null; try { Class.forName("org.hsqldb.jdbcDriver"); conn = DriverManager.getConnection("jdbc:hsqldb:mem:parametrizedlist", "sa", ""); Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/parametrizedlist/CreateDB.sql"); ScriptRunner runner = new ScriptRunner(conn); runner.setLogWriter(null); runner.setErrorLogWriter(new PrintWriter(System.err)); runner.runScript(reader); conn.commit(); reader.close(); reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/parametrizedlist/Config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); reader.close(); } finally { if (conn != null) { conn.close(); } } }
Example #10
Source File: JavassistTest.java From mybatis with Apache License 2.0 | 6 votes |
@BeforeClass public static void setUp() throws Exception { // create a SqlSessionFactory Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/javassist/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/javassist/CreateDB.sql"); ScriptRunner runner = new ScriptRunner(conn); runner.setLogWriter(null); runner.runScript(reader); reader.close(); session.close(); }
Example #11
Source File: XMLConfigBuilder.java From QuickProject with Apache License 2.0 | 6 votes |
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); } } } } }
Example #12
Source File: PropertiesInMappersTest.java From mybatis with Apache License 2.0 | 6 votes |
@BeforeClass public static void setUp() throws Exception { // this property value should be replaced on all mapper files Properties p = new Properties(); p.put("property", "id"); // create a SqlSessionFactory Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/propertiesinmapperfiles/mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader, p); reader.close(); // populate in-memory database SqlSession session = sqlSessionFactory.openSession(); Connection conn = session.getConnection(); reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/propertiesinmapperfiles/CreateDB.sql"); ScriptRunner runner = new ScriptRunner(conn); runner.setLogWriter(null); runner.runScript(reader); reader.close(); session.close(); }
Example #13
Source File: MapperAnnotationBuilder.java From mybaties with Apache License 2.0 | 6 votes |
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(); } } }
Example #14
Source File: DynSqlTest.java From mybaties with Apache License 2.0 | 6 votes |
@BeforeClass public static void setUp() throws Exception { Connection conn = null; try { Reader configReader = Resources.getResourceAsReader("org/apache/ibatis/submitted/dynsql/MapperConfig.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader); configReader.close(); conn = sqlSessionFactory.getConfiguration().getEnvironment().getDataSource().getConnection(); Reader scriptReader = Resources.getResourceAsReader("org/apache/ibatis/submitted/dynsql/CreateDB.sql"); ScriptRunner runner = new ScriptRunner(conn); runner.setLogWriter(null); runner.setErrorLogWriter(null); runner.runScript(scriptReader); conn.commit(); scriptReader.close(); } finally { if (conn != null) { conn.close(); } } }
Example #15
Source File: BaseTest.java From mybatis with Apache License 2.0 | 6 votes |
@BeforeClass public static void setUp() throws Exception { // create an SqlSessionFactory Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables/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/global_variables/CreateDB.sql"); ScriptRunner runner = new ScriptRunner(conn); runner.setLogWriter(null); runner.runScript(reader); reader.close(); session.close(); }
Example #16
Source File: ImmutablePOJOTest.java From mybaties with Apache License 2.0 | 6 votes |
@BeforeClass public static void setupClass() throws Exception { Connection conn = null; try { Class.forName("org.hsqldb.jdbcDriver"); conn = DriverManager.getConnection("jdbc:hsqldb:mem:immutable_constructor", "sa", ""); Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/immutable_constructor/CreateDB.sql"); ScriptRunner runner = new ScriptRunner(conn); runner.setLogWriter(null); runner.setErrorLogWriter(new PrintWriter(System.err)); runner.runScript(reader); conn.commit(); reader.close(); reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/immutable_constructor/ibatisConfig.xml"); factory = new SqlSessionFactoryBuilder().build(reader); reader.close(); } finally { if (conn != null) { conn.close(); } } }
Example #17
Source File: InsertTest.java From mybaties with Apache License 2.0 | 6 votes |
@Ignore // Not supported yet in PostgreSQL @Test public void testInsertMappedBatch() throws Exception { Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/keycolumn/MapperConfig.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); try { InsertMapper mapper = sqlSession.getMapper(InsertMapper.class); Name name = new Name(); name.setFirstName("Fred"); name.setLastName("Flintstone"); mapper.insertNameMapped(name); Name name2 = new Name(); name2.setFirstName("Wilma"); name2.setLastName("Flintstone"); mapper.insertNameMapped(name2); List<BatchResult> batchResults = sqlSession.flushStatements(); assertNotNull(name.getId()); assertNotNull(name2.getId()); assertEquals(1, batchResults.size()); } finally { sqlSession.close(); } }
Example #18
Source File: XMLMapperBuilder.java From Shop-for-JavaWeb with MIT License | 6 votes |
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); } } } }
Example #19
Source File: HeavyInitialLoadTest.java From mybatis with Apache License 2.0 | 6 votes |
@BeforeClass public static void initSqlSessionFactory() throws Exception { Connection conn = null; try { Class.forName("org.hsqldb.jdbcDriver"); conn = DriverManager.getConnection("jdbc:hsqldb:mem:heavy_initial_load", "sa", ""); Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/heavy_initial_load/ibatisConfig.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); reader.close(); } finally { if (conn != null) { conn.close(); } } }
Example #20
Source File: NonFullyQualifiedNamespaceTest.java From mybatis with Apache License 2.0 | 6 votes |
private static void initDb(Connection conn) throws IOException, SQLException { try { Reader scriptReader = Resources .getResourceAsReader("org/apache/ibatis/submitted/xml_external_ref/CreateDB.sql"); ScriptRunner runner = new ScriptRunner(conn); runner.setLogWriter(null); runner.setErrorLogWriter(null); runner.runScript(scriptReader); conn.commit(); scriptReader.close(); } finally { if (conn != null) { conn.close(); } } }
Example #21
Source File: AssociationTest.java From mybaties with Apache License 2.0 | 6 votes |
@BeforeClass public static void setUp() throws Exception { // create a SqlSessionFactory Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/associationtest/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/associationtest/CreateDB.sql"); ScriptRunner runner = new ScriptRunner(conn); runner.setLogWriter(null); runner.runScript(reader); reader.close(); session.close(); }
Example #22
Source File: ColumnForwardingTest.java From mybatis with Apache License 2.0 | 6 votes |
@BeforeClass public static void setUp() throws Exception { // create a SqlSessionFactory Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/column_forwarding/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/column_forwarding/CreateDB.sql"); ScriptRunner runner = new ScriptRunner(conn); runner.setLogWriter(null); runner.runScript(reader); reader.close(); session.close(); }
Example #23
Source File: RawSqlSourceTest.java From mybatis with Apache License 2.0 | 6 votes |
@BeforeClass public static void setUp() throws Exception { // create an SqlSessionFactory Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/raw_sql_source/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/raw_sql_source/CreateDB.sql"); ScriptRunner runner = new ScriptRunner(conn); runner.setLogWriter(null); runner.runScript(reader); reader.close(); session.close(); }
Example #24
Source File: MybatisSqlExecUtil.java From metadata with Apache License 2.0 | 6 votes |
/** * 执行 * * @param jdbcVo 数据库连接信息 * @param inputStream 待执行的脚本信息 */ public static void execute(JdbcVo jdbcVo, InputStream inputStream) throws Exception { Class.forName(jdbcVo.getDriverClassName()); final String url = jdbcVo.getUrl(); final String username = jdbcVo.getUsername(); final String password = jdbcVo.getPassword(); try (Connection conn = DriverManager.getConnection(url, username, password)) { //TODO: 加在驱动时 前端会直接结束!为什么? Reader reader = new InputStreamReader(inputStream); ScriptRunner runner = new ScriptRunner(conn); //设置字符集,不然中文乱码插入错误 Resources.setCharset(StandardCharsets.UTF_8); //设置是否输出日志 runner.setLogWriter(new PrintWriter(System.out)); runner.runScript(reader); runner.closeConnection(); } }
Example #25
Source File: BlockingCacheTest.java From mybatis with Apache License 2.0 | 6 votes |
@Before public void setUp() throws Exception { // create a SqlSessionFactory Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/blocking_cache/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/blocking_cache/CreateDB.sql"); ScriptRunner runner = new ScriptRunner(conn); runner.setLogWriter(null); runner.runScript(reader); reader.close(); session.close(); }
Example #26
Source File: ColumnForwardingTest.java From mybaties with Apache License 2.0 | 6 votes |
@BeforeClass public static void setUp() throws Exception { // create a SqlSessionFactory Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/column_forwarding/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/column_forwarding/CreateDB.sql"); ScriptRunner runner = new ScriptRunner(conn); runner.setLogWriter(null); runner.runScript(reader); reader.close(); session.close(); }
Example #27
Source File: XMLConfigBuilder.java From QuickProject with Apache License 2.0 | 6 votes |
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); } }
Example #28
Source File: HierarchicalXMLConfigBuilder.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
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); } } } } }
Example #29
Source File: QuotedColumnNamesTest.java From mybatis with Apache License 2.0 | 6 votes |
@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(); } } }
Example #30
Source File: AwfulTableTest.java From mybaties with Apache License 2.0 | 6 votes |
@Before public void setUp() throws Exception { Class.forName("org.hsqldb.jdbcDriver"); Connection conn = DriverManager.getConnection("jdbc:hsqldb:mem:attest", "sa", ""); Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/awful_table/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/awful_table/MapperConfig.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); reader.close(); }