1、java bean:
public class Account
{
private int id;
private String firstName;
private String lastName;
private String emailAddress;
}get set 略。
2、与java bean相匹配的xml配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
//dtd
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Account">
<typeAlias alias="Account" type="com.data.Account" />//别名
<resultMap id="AccountResult" class="Account">//javabean与数据库字段的映射
<result property="id" column="ACC_ID" />
<result property="firstName" column="ACC_FIRST_NAME" />
<result property="lastName" column="ACC_LAST_NAME" />
<result property="emailAddress" column="ACC_EMAIL" />
</resultMap>
<select id="selectAllAccounts" resultMap="AccountResult">//选择
select * from ACCOUNT
</select>
<select id="selectAccountById" parameterClass="int"
resultClass="Account">//选择
select ACC_ID as id, ACC_FIRST_NAME as firstName, ACC_LAST_NAME
as lastName, ACC_EMAIL as emailAddress from ACCOUNT where ACC_ID
= #id#
</select>
<insert id="insertAccount" parameterClass="Account">//更新
insert into ACCOUNT ( ACC_ID, ACC_FIRST_NAME, ACC_LAST_NAME,
ACC_EMAIL) values ( #id#, #firstName#, #lastName#, #emailAddress#)
</insert>
<update id="updateAccount" parameterClass="Account">
update ACCOUNT set ACC_FIRST_NAME = #firstName#, ACC_LAST_NAME =
#lastName#, ACC_EMAIL = #emailAddress# where ACC_ID = #id#
</update>
<delete id="deleteAccountById" parameterClass="int">//删除
delete from ACCOUNT where ACC_ID = #id#
</delete>
</sqlMap>
3、配置文件,主要是配置数据源,以及映射文件,
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
通过传统JDBC Connection.commit/rollback实现事务支持。
<transactionManager type="JDBC" commitRequired="false">
SIMPLE是ibatis内置的dataSource实现, 其中实现了一个简单的数 据库 连 接 池 机制 ,对 应 ibatis 实 现 类 为com.ibatis.sqlmap.engine.datasource.SimpleDataSourceFactory
DBCP: 基于 Apache DBCP 连接池组件实现的 DataSource 封装,当无容器提供 DataSource 服务时,建议使用该选项,对应 ibatis 实现类为com.ibatis.sqlmap.engine.datasource.DbcpDataSourceFactory。JNDI:
使用 J2EE 容器提供的 DataSource 实现,DataSource 将通过指定的 JNDI Name 从 容器 中 获取 。对 应 ibatis 实 现 类 为com.ibatis.sqlmap.engine.datasource.JndiDataSourceFactory。
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.ddtek.jdbc.sqlserver.SQLServerDriver" />
<property name="JDBC.ConnectionURL" value="jdbc:datadirect:sqlserver://localhost:1433;databaseName =a" />//该处由于使用了其他的jdbc连接,所以需要多了一个databaseName=a来连接一个数据库
<property name="JDBC.Username" value="sa" />
<property name="JDBC.Password" value="ss" />
<property name="Pool.MaximumActiveConnections" value="10"/>
<property name="Pool.MaximumIdleConnections" value="5"/>
<property name="Pool.MaximumCheckoutTime" value="120000"/>
<property name="Pool.TimeToWait" value="500"/>
<property name="Pool.PingQuery" value="select 1 from ACCOUNT"/>
<property name="Pool.PingEnabled" value="false"/>
<property name="Pool.PingConnectionsOlderThan" value="1"/>
<property name="Pool.PingConnectionsNotUsedFor" value="1"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/data/Account.xml" />//加载映射文件
</sqlMapConfig>
4、编写测试用例
读取配置文件
Reader reader = Resources.getResourceAsReader("com/data/SqlMapConfig.xml");
sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
XmlSqlMapClientBuilder是ibatis 2.0之后版本新引入的组件,用以替代1.x
版本中的XmlSqlMapBuilder。其作用是根据配置文件创建SqlMapClient实例
sqlMapper.queryForList("selectAllAccounts");
sqlMapper.queryForObject("selectAccountById", id);
sqlMapper.insert("insertAccount", account);
sqlMapper.update("updateAccount", account);
sqlMapper.delete("deleteAccount", id);
配置文件:
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
errorTracingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false"
/>
<![CDATA[……]]>
XmlSqlMapClientBuilder xmlBuilder =
new XmlSqlMapClientBuilder();
SqlMapClient sqlMap = xmlBuilder.buildSqlMap(reader);
sqlMap.startTransaction();