/*
* Copyright 2004 Clinton Begin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibatis.sqlmap.client;
import com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser;
import java.io.InputStream;
import java.io.Reader;
import java.util.Properties;
/**
* Builds SqlMapClient instances from a supplied resource (e.g. XML configuration file)
* <p/>
* The SqlMapClientBuilder class is responsible for parsing configuration documents
* and building the SqlMapClient instance. Its current implementation works with
* XML configuration files (e.g. sql-map-config.xml).
* <p/>
* Example:
* <pre>
* Reader reader = Resources.getResourceAsReader("properties/sql-map-config.xml");
* SqlMapClient client = SqlMapClientBuilder.buildSqlMapClient (reader);
* </pre>
* <p/>
* Examples of the XML document structure used by SqlMapClientBuilder can
* be found at the links below.
* <p/>
* Note: They might look big, but they're mostly comments!
* <ul>
* <li> <a href="sql-map-config.txt">The SQL Map Config File</a>
* <li> <a href="sql-map.txt">An SQL Map File</a>
* <ul>
*/
public class SqlMapClientBuilder {
/**
* No instantiation allowed.
*/
protected SqlMapClientBuilder() {
}
/**
* Builds an SqlMapClient using the specified reader.
*
* @param reader A Reader instance that reads an sql-map-config.xml file.
* The reader should read an well formed sql-map-config.xml file.
* @return An SqlMapClient instance.
*/
public static SqlMapClient buildSqlMapClient(Reader reader) {
// return new XmlSqlMapClientBuilder().buildSqlMap(reader);
return new SqlMapConfigParser().parse(reader);
}
/**
* Builds an SqlMapClient using the specified reader and properties file.
* <p/>
*
* @param reader A Reader instance that reads an sql-map-config.xml file.
* The reader should read an well formed sql-map-config.xml file.
* @param props Properties to be used to provide values to dynamic property tokens
* in the sql-map-config.xml configuration file. This provides an easy way to
* achieve some level of programmatic configuration.
* @return An SqlMapClient instance.
*/
public static SqlMapClient buildSqlMapClient(Reader reader, Properties props) {
// return new XmlSqlMapClientBuilder().buildSqlMap(reader, props);
return new SqlMapConfigParser().parse(reader, props);
}
/**
* Builds an SqlMapClient using the specified input stream.
*
* @param inputStream An InputStream instance that reads an sql-map-config.xml file.
* The stream should read a well formed sql-map-config.xml file.
* @return An SqlMapClient instance.
*/
public static SqlMapClient buildSqlMapClient(InputStream inputStream) {
return new SqlMapConfigParser().parse(inputStream);
}
/**
* Builds an SqlMapClient using the specified input stream and properties file.
* <p/>
*
* @param inputStream An InputStream instance that reads an sql-map-config.xml file.
* The stream should read an well formed sql-map-config.xml file.
* @param props Properties to be used to provide values to dynamic property tokens
* in the sql-map-config.xml configuration file. This provides an easy way to
* achieve some level of programmatic configuration.
* @return An SqlMapClient instance.
*/
public static SqlMapClient buildSqlMapClient(InputStream inputStream, Properties props) {
return new SqlMapConfigParser().parse(inputStream, props);
}
}
分享到:
相关推荐
sqlMapClient = com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); } catch (IOException e) { e.printStackTrace(); } } public void addStudent(Student ...
SqlMapClient sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); return sqlMapper; } public void executeSQLCommand(String sql) throws Exception { st.executeUpdate(sql...
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); } catch (IOException e) { throw new RuntimeException("Exception!" + e, e); } } public List<Employee> ...
- **iBatis** 使用 SqlMapClientBuilder 从配置文件(如 SqlMapConfig.xml)构建 SqlMapClient 对象。 - **示例代码**: ```java String resource = "SqlMapConfig.xml"; Reader reader = Resources....
SqlMapClient sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(Resources.getResourceAsStream("SqlMapConfig.xml")); List<User> users = sqlMapClient.queryForList("selectAllUsers", 1); for (User user ...
- 使用`SqlMapClientBuilder`构建`SqlMapClient`对象,这一步是初始化iBATIS的核心,通过读取`SqlMapConfig.xml`来建立与数据库的连接。代码示例: ```java Reader reader = Resources.getResourceAsReader(...
SqlMapClient可以通过SqlMapClientBuilder类来构建,这个类提供了加载XML配置文件并初始化SqlMapClient的方法。 Ibatis的配置文件中,你可以定义SQL语句的映射,这些映射位于独立的XML文件中,通常命名为xxxSqlMap....
orgproxool.SqlMapClientBuilder builder = new SqlMapClientBuilder(); SqlMapClient sqlMap = builder.buildSqlMapClient(ppp); ``` 最后,在应用程序中使用Proxool就像使用普通的数据库连接一样,但实际上是通过...
这个类中import了多个包,包括com.ibatis.common.xml.NodeletException、com.ibatis.sqlmap.client.SqlMapClient、com.ibatis.sqlmap.client.SqlMapClientBuilder等。需要注意的是,IBatis的包声明是org.apache....
在Java代码中,通常通过`SqlMapClientBuilder`构建实例。 2. 类型别名(Type Alias):`typeAlias`元素用于简化Java类型引用,例如`<typeAlias alias="Student" type="entity.Student"/>`,之后可以直接用"Student...
在使用iBATIS(现已被MyBatis取代)进行数据库操作时,我们常常需要传递参数到SQL语句中。在iBATIS中,有两种主要的方式来处理这些参数:使用`$`和`#`。这两种方式在不同的场景下有不同的效果。...
在Ibatis2中,我们通过SqlMapClientBuilder构建SqlMapClient对象,然后通过它可以生成DAO的动态代理对象,实现业务逻辑。 ```java SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(Resources....
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient( Resources.getResourceAsReader("SqlMapConfig.xml")); } catch (IOException e1) { e1.printStackTrace(); } return sqlMapClient; } } ``` 4. *...
SqlMapClient sqlMap = new SqlMapClientBuilder().build(new FileInputStream("SqlMapConfig.xml")); // 执行查询操作 List users = (List)sqlMap.queryForList("selectAllUsers"); // 执行更新操作 int result =...
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(Resources.getResourceAsStream("SqlMapConfig.xml")); List<Contact> contacts = sqlMap.queryForList("Contact.selectAllContacts"); for (Contact ...
利用`Resources.getResourceAsReader()`方法读取配置文件,然后通过`SqlMapClientBuilder.buildSqlMapClient()`方法创建SqlMapClient实例。这个过程通常放在一个静态代码块中,以确保在类加载时就完成初始化。 2. *...
- **SqlMapClientBuilder**:用于构建`SqlMapClient`实例。 - **异常处理**:捕获并处理可能发生的`IOException`。 #### 四、总结 通过对ibatis配置文件的解析,我们可以看到ibatis的强大之处在于它能够灵活地配置...
在源码方面,iBATIS的实现中,`<iterate>`标签的解析和执行是通过SqlMapConfig.xml配置文件加载后,由SqlMapClientBuilder解析,并由Executor执行器来处理。在处理动态SQL时,iBATIS会将`<iterate>`标签转换成对应的...
通过 `SqlMapClientBuilder` 创建 `SqlMapClient` 实例,并传入配置文件路径。这样就可以通过 `SqlMapClient` 来执行 SQL 操作了。 #### 四、ibatis 与 Hibernate 的对比 - **SQL 控制**:ibatis 相比 Hibernate ...