首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 网络编程-> Delphi教程
IBatisNet基础组件-ASP教程,组件开发
作者:网友供稿 点击:101
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
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)
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/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·ListBox控件基本功能-.NET教程,组件控件开发
·用window.location.href实现刷新另个框架页面-.NET教程,Asp.Net开发
·JSP+STRUTS+EJB+DAO+HIBERNATE实例-JSP教程,Jsp/Servlet
·.NET中的设计模式四:命令模式-.NET教程,Asp.Net开发
·数字转英文(货币)大写-.NET教程,数据库应用
·用户控件用户登录判断-ASP教程,客户端相关
·计数器的另一用法:自动切换首页图片-ASP教程,ASP应用
·漫谈Java数据库存取技术-JSP教程,Java技巧及代码
·正则表达式-.NET教程,Asp.Net开发
·即时通讯靠免费短信能赚10亿?
最新文章
·让flash动画适应任何分辨率的网页_flash教程
·新手必看之网站的定位篇_站长心得
·1000ip的效益也能大于一万ip_网赚技巧
·google adsense课堂:西联快汇知识_网赚技巧
·googleadsense的无效点击_google推广
·google adsense高价关键字[排行榜]_google推广
·google搜索引擎的十大应用_google推广
·windows vista下如何关闭远程控制_windows vista
·修改配置 让windows vista系统实现自动登录_windows vista
·整齐划一 将整个网页保存在一个文件中_站长心得
相关主题
  • ibatisnet系列-执行存储过程_asp.net技巧
  • IBatisNet配置-.NET教程,评论及其它
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭