1,引用必须jar包:ibatis2-common-2.1.7.597.jar ibatis2-sqlmap-2.1.7.597.jar commons-logging-1.1.1.jar
jdbc驱动jar包:ojdbc14.jar 或者classes12.jar (jar包位于:%ORACLE_HOME%jdbc\lib)
2,配置sqlMapConfig.xml 和 sqlMap.xml
sqlMapConfig.xml配置如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!-- 全局配置选项 -->
<settings
cacheModelsEnabled="true" //是否使用高速缓存,默认true
enhancementEnabled="true" //是否使用cglib优化,默认true,cglib是运行时代码生成库,与延迟加载配合使用。
lazyLoadingEnabled="true" //是否延迟加载,默认true
maxRequests="32" //最大请求数,已废弃
maxSessions="10" //最大会话线程数,已废弃
maxTransactions="5" //最大事务数,已废弃
useStatementNamespaces="false" /> //在引用已映射语句时是否需要限定名,默认false
<!-- 事务管理器 -->
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@127.0.0.1:1521:ORCL" />
<property name="JDBC.Username" value="***" />
<property name="JDBC.Password" value="***" />
</dataSource>
</transactionManager>
<sqlMap resource="ibatis/sqlMap.xml" /> <!-- 对各SqlMap 文件的引用 -->
<properties resource="db.properties"/> <!-- 配置属性文件 使用方法如: ${driver}-->
</sqlMapConfig>
sqlMap.xml配置如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap>
<resultMap id="RM-LINEPAY-TXN-TYPE" class="com.fan.ibatis.TxnTypeDO">
<result property="id" column="ID" javaType="long" jdbcType="NUMBER" nullValue="0"/>
<result property="txnType" column="TXN_TYPE" javaType="java.lang.String" jdbcType="VARCHAR2"/>
<result property="enable" column="ENABLE" javaType="java.lang.String" jdbcType="CHAR"/>
<result property="gmtCreate" column="GMT_CREATE" javaType="java.util.Date" jdbcType="TIMESTAMP(6)"/>
<result property="gmtModified" column="GMT_MODIFIED" javaType="java.util.Date" jdbcType="TIMESTAMP(6)"/>
</resultMap>
<select id="MS-TXN-TYPE-GET-ALL" resultMap="RM-TXN-TYPE" parameterClass="string">
<![CDATA[
select * from txn_type where scencs_code=#scencsCode#
]]>
</select>
</sqlMap>
3,编写测试类SampleSqlMapClinet.java
public static void main(String[] args) throws IOException, SQLException {
String resource = "ibatis/sqlMapConfig.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
List list = sqlMap.queryForList("MS-TXN-TYPE-GET-ALL", "1001");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}