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) |
insert
update
delete |
|
<select> |
id
parameterclass
resultclass
parametermap
resultmap
cachemodel |
all dynamic elements
<generate> (.net only) |
all query methods |
|
<procedure> |
id
parameterclass
resultclass
parametermap
resultmap
xmlresultname (java only) |
all dynamic elements |
insert
update
delete
all query methods |
其中,statement最为通用,它可以代替其余的所有节点。除statement之外的节点对应于sql中的同名操作(procedure对应存储过程)。使用statement定义所有操作,缺乏直观性,建议在开发中根据操作目的,各自选用对应的节点名加以说明。一方面,使得配置文件更加直观,另一方面,也可以借助xsd对i节点声明进行更有针对性的检查,以避免配置上的失误。
|
<statement id=”statementname”
[parametermap=”nameofparametermap”]
[parameterclass=”some.class.name”]
[resultmap=”nameofresultmap”]
[resultclass=”some.class.name”]
[cachemodel=”nameofcache”]
>
select * from product where prd_id = [?|#propertyname#]
order by [$simpledynamic$]
</statement> |
其中“[ ]”包围的部分为可能出现的配置项,各参数说明见下表。具体的使用方法参见ibatisnet官方文档。
|
参数 |
描述 |
|
parametermap |
参数映射,需结合parametermap节点对映射关系加以定义,对于存储过程之外的statement而言,建议使用parameterclass作为参数配置方式,一方面避免了参数映射配置工作,另一方面其性能表现更加出色 |
|
parameterclass |
参数类。指定了参数类型的完整类名(包括命名空间),可以通过别名避免每次书写冗长的类名 |
|
resultmap |
结果映射,需结合resultmap节点对映射关系加以定义 |
|
resultclass |
结果类。指定了结果类型的完整类名(包括命名空间),可以通过别名避免每次书写冗长的类名 |
|
cachemodel |
statement对应的cache模块 |
一般而言,对于insert、update、delete、select语句,优先采用parameterclass和resultclass.。paremetermap使用较少,而resultmap则大多用于存储过程处理和查询。存储过程相对而言比较封闭(很多情况下需要调用现有的存储过程),其参数名和返回的数据字段命名往往不符合dotnet编程的命名规范)。使用resultmap建立字段名同dotnet对象的属性之间的映射关系就非常有效。另一方面,由于通过resultmap指定了字段名和字段类型,ibatisnet无需再通过ado.net来动态获取字段信息,在一定程度上也提升了性能。
下面特别说明一下ibatisnet对stored procedures的处理,ibatis数据映射把存储过程当成另外一种声明元素。示例演示了一个基于存储过程的简单数据映射。
<!-- microsot sql server --><procedure id="swapemailaddresses" parametermap="swap-params"> ps_swap_email_address</procedure>... <parametermap id="swap-params"> <parameter property="email1" column="first_email" /> <parameter property="email2" column="second_email" /></parametermap><!-- oracle with ms oracleclient provider --><procedure id="insertcategory" parametermap="insert-params"> prc_insertcategory</procedure>... <parametermap id="insert-params"> <parameter property="name" column="p_category_name"/> <parameter property="guidstring" column="p_category_guid" dbtype="varchar"/> <parameter property="id" column="p_category_id" dbtype="int32" type="int"/></parametermap><!-- oracle with odp.net 10g provider --><statement id="insertaccount" parametermap="insert-params"> prc_insertaccount</statement>... <parametermap id="insert-params"> <parameter property="id" dbtype="int32"/> <parameter property="firstname" dbtype="varchar2" size="32"/> <parameter property="lastname" dbtype="varchar2" size="32"/> <parameter property="emailaddress" dbtype="varchar2" size="128"/></parametermap> |
示例是调用存储过程swapemailaddress的时候将会在数据库表的列和两个email地址之间交换数据,参数对象亦同。参数对象仅在属性被设置成inout或者out的时候才会被修改。否则,他们将不会被修改。当然,不可变得参数对象是不会被修改的,比如string.
.net中,parametermap属性是必须的。dbtype,参数方向,大小由框架自动发现的。(使用commandbuilder实现的)
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




