DomSqlMapBuilder,其作用是根据配置文件创建SqlMap实例。可以通过这个组件从Stream, Uri, FileInfo, or XmlDocument instance 来读取sqlMap.config文件。
SqlMap是IBatisnet的核心组件,提供数据库操作的基础平台。SqlMap可通过DomSqlMapBuilder创建。
Assembly assembly = Assembly.Load("IBatisNetDemo");
Stream stream = assembly.GetManifestResourceStream("IBatisNetDemo.sqlmap.config");
DomSqlMapBuilder builder = new DomSqlMapBuilder();
sqlMap = builder.Configure( stream );
SqlMap是线程安全的,也就是说,在一个应用中,可以共享一个SqlMap实例。
SqlMap提供了众多数据操作方法,下面是一些常用方法的示例,具体说明文档参见 ibatis net doc,或者ibatisnet的官方开发手册。
SqlMap基本操作示例
例1:数据写入操作(insert、update、delete)
SqlMap.BeginTransaction();
Person person = new Person();
Person.FirstName = “Zhang”;
Person.LastName = “shanyou”;
int Id = (int) SqlMap.Insert("InsertPerson", person);
SqlMap.CommitTransaction();.
|
例2:数据查询:
Int Id = 1;
Person person = SqlMap.QueryForObject<Person>("", Id);
return person;
|
例3:在指定对象中存放查询结果:
Int Id = 1;
Person person = new Person();
person = SqlMap.QueryForObject<Person>("GetBirthday", Id, person);
return person;
|
例4:执行批量查询(Select)
IList<Person> list = null;
list = SqlMap.QueryForList<Person>("SelectAllPerson", null);
return list;
|
例5:查询指定范围内的数据(Select)
IList<Person> list = null;
list = SqlMap.QueryForList<Person>("SelectAllPerson", null, 0, 40);
return list;
|
例6:结合RowDelegate进行查询:
public void RowHandler(object obj, IList list)
{
Product product = (Product) object;
product.Quantity = 10000;
}
SqlMapper.RowDelegate handler = new SqlMapper.RowDelegate(this.RowHandler);
IList list = sqlMap.QueryWithRowDelegate("getProductList", null, handler);
|
例7:分页查询(Select)
PaginatedList list = sqlMap.QueryForPaginatedList (“getProductList”, null, 10);
list.NextPage();
list.PreviousPage();
|
例8:基于Map的批量查询(select)
IDictionary map = sqlMap.QueryForMap (“getProductList”, null, “productCode”);
Product p = (Product) map[“EST-93”];
|
OR映射
相对于Nhibernate等ORM实现来说,IBatisnet的映射配置更为直接,下面是一个典型的配置文件:
<?xmlversion="1.0"encoding="utf-8" ?>
<sqlMapnamespace="Person"xmlns="http://ibatis.apache.org/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<!—模块配置à
<alias>
<typeAliasalias="Person"type="IBatisNetDemo.Domain.Person,IBatisNetDemo" />
</alias>
<cacheModels>
<cacheModelid="person-cache"implementation="MEMORY" >
<flushIntervalhours="24"/>
<flushOnExecute statement="UpdateAccountViaInlineParameters"/>
<flushOnExecute statement="UpdateAccountViaParameterMap"/>
<propertyname="Type"value="Weak"/>
</cacheModel>
</cacheModels>
<resultMaps>
<resultMapid="SelectAllResult"class="Person">
<resultproperty="Id"column="PER_ID" />
<resultproperty="FirstName"column="PER_FIRST_NAME" />
<resultproperty="LastName"column="PER_LAST_NAME" />
<resultproperty="BirthDate"column="PER_BIRTH_DATE" />
<resultproperty="WeightInKilograms"column="PER_WEIGHT_KG" />
<resultproperty="HeightInMeters"column="PER_HEIGHT_M" />
</resultMap>
</resultMaps>
<!—statement配置 à
<statements>
<selectid="SelectAllPerson"resultMap="SelectAllResult" cacheModel="account-cache">
select
PER_ID,
PER_FIRST_NAME,
PER_LAST_NAME,
PER_BIRTH_DATE,
PER_WEIGHT_KG,
PER_HEIGHT_M
from PERSON
</select>
<selectid="SelectByPersonId"resultClass="Person"parameterClass="int">
select
PER_ID,
PER_FIRST_NAME,
PER_LAST_NAME,
PER_BIRTH_DATE,
PER_WEIGHT_KG,
PER_HEIGHT_M
from PERSON
where PER_ID = #value#
</select>
<insertid="InsertPerson" parameterclass="Person" >
<selectKeyproperty="Id"type="post"resultClass="int">
${selectKey}
</selectKey>
insert into Person
( PER_FIRST_NAME,
PER_LAST_NAME,
PER_BIRTH_DATE,
PER_WEIGHT_KG,
PER_HEIGHT_M)
values
(#FirstName#,#LastName#,#BirthDate#, #WeightInKilograms#, #HeightInMeters#)
</insert>
<updateid="UpdatePerson"
parameterclass="Person">
<![CDATA[ update Person set
PER_FIRST_NAME =#FirstName#,
PER_LAST_NAME =#LastName#,
PER_BIRTH_DATE =#BirthDate#,
PER_WEIGHT_KG=#WeightInKilograms#,
PER_HEIGHT_M=#HeightInMeters#
where
PER_ID = #Id# ]]>
</update>
<deleteid="DeletePerson"parameterclass="Person">
delete from Person
where
PER_ID = #Id#
</delete>
</statements>
</sqlMap>
可以看到,映射文件主要分为两个部分:模块配置和Statement配置。
模块配置包括:
1、typeAlias节点
定义了本映射文件中的别名,以避免过长变量值的反复书写,此例中通过typeAlias节点为类“IBatisNetDemo.Domain.Person”定义了一个别名“Person”,这样在本配置文件中的其他部分,需要引用“IBatisNetDemo.Domain.Person”类时,只需以其别名替代即可。
2、cacheModel节点
定义了本映射文件中使用的Cache机制:
<cacheModelid="person-cache"implementation="MEMORY" >
<flushIntervalhours="24"/>
<flushOnExecute statement="UpdateAccountViaInlineParameters"/>
<flushOnExecute statement="UpdateAccountViaParameterMap"/>
<propertyname="Type"value="Weak"/>
</cacheModel>
|
这里声明了一个名为“person-cache”的cacheModel,之后可以在Statement声明中对其进行引用:
<selectid="SelectAllPerson"resultMap="SelectAllResult" cacheModel=" person-cache">
select
PER_ID,
PER_FIRST_NAME,
PER_LAST_NAME,
PER_BIRTH_DATE,
PER_WEIGHT_KG,
PER_HEIGHT_M
from PERSON
</select>
|
这表明对通过id为SelAllPerson的“Select Statement”获取的数据,使用CacheModel “person-cache”进行缓存。之后如果程序再次用此Satement进行数据查询。即直接从缓存中读取数据,而不需再去数据库查询。
CacheModel主要有几个配置点:
参数
|
描述
|
flushInterval
|
设定缓存有效期,如果超过此设定值,则将此CacheModel缓存清空
|
CacheSize
|
本Cachemodel中最大的数据对象数量
|
flushOnExecute
|
指定执行特定的Statement时,将缓存清空。如UpdatePerson操作将更新数据库中用户信息,这将导致缓存中的数据对象与数据库中的实际数据发生偏差,因此必须将缓存清空以避免脏数据的出现。
|
3、resultMaps节点
resultMaps实现dotnet实体到数据库字段的映射配置:
<resultMapid="SelectAllResult"class="Person">
<resultproperty="Id"column="PER_ID" />
<resultproperty="FirstName"column="PER_FIRST_NAME" />
<resultproperty="LastName"column="PER_LAST_NAME" />
<resultproperty="BirthDate"column="PER_BIRTH_DATE" />
<resultproperty="WeightInKilograms"column="PER_WEIGHT_KG" />
<resultproperty="HeightInMeters"column="PER_HEIGHT_M" />
</resultMap>
|
Statement配置:
Statement配置包含了数个与Sql Statement相关的节点,<statement>元素是一个通用的能够包容任意类型sql的元素。我们可以用更多细节的元素。
这些细节元素提供更好的错误检查以及一些更多的功能。(例如,一个插入函数能够返回数据库自动生成的key)。以下表格总结了声明类型元素以及他们的特性和属性。
Statement Element
|
Attributes
|
Child Elements
|
Methods
|
||
<statement>
|
id
parameterClass
resultClass
parameterMap
resultMap
cacheModel
xmlResultName (Java only)
|
All dynamic elements
|
insert
update
delete
All query methods
|
||
<insert>
|
id
parameterClass
parameterMap
|
All dynamic elements
<selectKey>
<generate> (.NET only)
|
insert
update
delete
|
||
<update>
|
id
parameterClass
parameterMap
|
All dynamic elements
<generate> (.NET only)
|
insert
update
delete
|
||
<delete>
|
id
parameterClass
parameterMap
|
All dynamic elements
<generate> (.NET only)
|
|