DomSqlMapBuilder

DomSqlMapBuilder,其作用是根据配置文件创建SqlMap实例。可以通过这个组件从StreamUriFileInfo, or XmlDocument instance 来读取sqlMap.config文件。

SqlMap

  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的映射配置更为直接,下面是一个典型的配置文件:

<?xml version="1.0" encoding="utf-8" ?>

<sqlMap namespace="Person" xmlns="http://ibatis.apache.org/mapping"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >

<!—模块配置à

<alias>

<typeAlias alias="Person" type="IBatisNetDemo.Domain.Person,IBatisNetDemo" />

</alias>

<cacheModels>

<cacheModel id="person-cache" implementation="MEMORY" >

<flushInterval hours="24"/>

<flushOnExecute  statement="UpdateAccountViaInlineParameters"/>

<flushOnExecute  statement="UpdateAccountViaParameterMap"/>

<property name="Type" value="Weak"/>

</cacheModel>

</cacheModels>

<resultMaps>

<resultMap id="SelectAllResult" class="Person">

<result property="Id" column="PER_ID" />

<result property="FirstName" column="PER_FIRST_NAME" />

<result property="LastName" column="PER_LAST_NAME" />

<result property="BirthDate" column="PER_BIRTH_DATE" />

<result property="WeightInKilograms" column="PER_WEIGHT_KG" />

<result property="HeightInMeters" column="PER_HEIGHT_M" />

</resultMap>

</resultMaps>

<!—statement配置 à

<statements>

<select id="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>

<select id="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>

<insert id="InsertPerson"  parameterclass="Person" >

<selectKey property="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>

<update id="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>

<delete id="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机制:

<cacheModel id="person-cache" implementation="MEMORY" >

<flushInterval hours="24"/>

<flushOnExecute  statement="UpdateAccountViaInlineParameters"/>

<flushOnExecute  statement="UpdateAccountViaParameterMap"/>

<property name="Type" value="Weak"/>

</cacheModel>

这里声明了一个名为“person-cache”的cacheModel,之后可以在Statement声明中对其进行引用:

<select id="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实体到数据库字段的映射配置:

<resultMap id="SelectAllResult" class="Person">

<result property="Id" column="PER_ID" />

<result property="FirstName" column="PER_FIRST_NAME" />

<result property="LastName" column="PER_LAST_NAME" />

<result property="BirthDate" column="PER_BIRTH_DATE" />

<result property="WeightInKilograms" column="PER_WEIGHT_KG" />

<result property="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实现的)

IBatisNet基础组件的更多相关文章

  1. winform快速开发平台 -> 基础组件之分页控件

    一个项目控件主要由及部分的常用组件,当然本次介绍的是通用分页控件. 处理思想:我们在处理分页过程中主要是针对数据库操作. 一般情况主要是传递一些开始位置,当前页数,和数据总页数以及相关关联的业务逻辑. ...

  2. CentOS安装LNMP环境的基础组件

    注:以下所有操作均在CentOS 6.5 x86_64位系统下完成. 在安装LNMP环境之前,请确保已经使用yum安装了以下各类基础组件(如果系统已自带,还可以考虑yum update下基础组件): ...

  3. Ext学习-基础组件介绍

    1.目标    学习对象获取,组件基础,事件模型以及学习ExtJS中的基础组件的应用. 2.内容   1.对象获取   2.组件原理以及基础   3.事件模型   4.常用组件的介绍 3.学习步骤 1 ...

  4. 如何从零开始实现一个soa远程调用服务基础组件

    说起soa远程调用基础组件,最著名的莫过于淘宝的dubbo了,目前很多的大型互联网公司都有一套自己的远程服务调用分布式框架,或者是使用开源的(例如dubbo),或者是自己基于某种协议(例如hessia ...

  5. android学习——必学基础组件

    android基础组件是一个Android的开发人员必须要了解,且深刻理解的东西: 1.应用程序基础 2.应用程序组件 2.1.活动(Activities) 2.2.服务(Services) 2.3. ...

  6. Android 基础组件

    基础组件 所有的控件都可以在java代码中创建出来,并且大部分的属性都对应set和get方法,比如 View view = new View(Context context)  context是上下文 ...

  7. Akka(17): Stream:数据流基础组件-Source,Flow,Sink简介

    在大数据程序流行的今天,许多程序都面临着共同的难题:程序输入数据趋于无限大,抵达时间又不确定.一般的解决方法是采用回调函数(callback-function)来实现的,但这样的解决方案很容易造成“回 ...

  8. App架构师实践指南三之基础组件

    App架构师实践指南三之基础组件 1.基础组件库随着时间的增长,代码量的逐渐积累,新旧项目之间有太多可以服用的代码.下面是整理的公共代码库. 2.关于加密密钥的保护以及网络传输安全是移动应用安全最关键 ...

  9. JMeter各个基础组件简介

    刚从LoadRunner转到JMeter,对JMeter的各种概念比较懵.在这里记录下.欢迎大家关注我的个人微信号:测试杂货铺. JMeter的各个功能都是它的组件来完成或实现的,下面来对JMeter ...

随机推荐

  1. iOS设计模式之中介者模式

    中介者模式 基本理解 中介者模式又叫做调停者模式,其实就是中间人或者调停者的意思. 尽管将一个系统分割成许多对象通常可以增加可复用性,但是对象之间的连接又降低了可复用性. 如果两个类不必彼此直接通信, ...

  2. [转]Designing a User Interface

    UI design can be divided into three essential elements : functionality, aesthetics, and performance. ...

  3. Effetive Java 22 Favor static member classes over nonstatic

    Nested class types Usage and remark Advantage Disadvantage static member classes Use for public help ...

  4. npm报错Error: ENOENT, stat 'D:\NodeLearn\node-global'

    最近想试下当前的当红炸子鸡 Nodejs,在安装配置时,发生了下面的错误: C:\nodejs\npmjs\bin>cd .. C:\nodejs\npmjs>cd .. C:\nodej ...

  5. 第七篇 :微信公众平台开发实战Java版之如何获取微信用户基本信息

    在关注者与公众号产生消息交互后,公众号可获得关注者的OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的.对于不同公众号,同一用户的openid不同). 公众号可通过本接口来根据O ...

  6. hdu Flow Problem (最大流 裸题)

    最大流裸题,贴下模版 view code#include <iostream> #include <cstdio> #include <cstring> #incl ...

  7. hiveserver2 with kerberos authentication

    Kerberos协议: Kerberos协议主要用于计算机网络的身份鉴别(Authentication), 其特点是用户只需输入一次身份验证信息就可以凭借此验证获得的票据(ticket-grantin ...

  8. DimDate populate data

    日期维度 任何一个数据仓库都应该有一个日期维度. 因为很少有不需要通过日期维度看数据的情况存在. 日期维度的好处是,你可以通过他连接各个事实表,然后在报表端传送报表参数的时候, 直接自动过滤日期维度的 ...

  9. 使用LVS实现负载平衡之Windows Server 2008配置

    LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一.承载于 II ...

  10. hyperstart 容器创建流程分析

    hyperstart中运行的pod的核心数据结构如下所示: struct hyper_pod { struct hyper_interface *iface; struct hyper_route * ...