3.4 ParameterMap and Inline Parameter

 语法

<parameterMap id="parameterMapIdentifier" 
  [class="fullyQualifiedClassName, assembly|typeAlias"]
  [extends="[sqlMapNamespace.]parameterMapId"]>
  <parameter 
    property ="propertyName" 
    [column="columnName"]
    [direction="Input|Output|InputOutput"]
    [dbType="databaseType"] 
    [type="propertyCLRType"]
    [nullValue="nullValueReplacement"] 
    [size="columnSize"] 
    [precision="columnPrecision"] 
    [scale="columnScale"]  
    [typeHandler="fullyQualifiedClassName, assembly|typeAlias"]  
  <parameter ... ... />
  <parameter ... ... /> 
</parameterMap>

class属性可以帮助解析参数提升性能,建议声明

例子

<parameterMap id="insert-product-param" class="Product">
  <parameter property="description" />
  <parameter property="id"/>
</parameterMap>

<statement id="insertProduct" parameterMap="insert-product-param">
  insert into PRODUCT (PRD_DESCRIPTION, PRD_ID) values (?,?);
</statement>

除了以上使用ParameterMap标签声明外,还可以使用内联(inline)的方式

可以引入其他Map文件的ParameterMap,方式:Map_Name.ParameterMapName

3.4.1 <parameterMap>attributes

3.4.1.1 id

3.4.1.2 class 

The parameter class must be a property object or IDictionary(System.Collections.IDictionary) instance

3.4.1.3 extends

注意SQL语句中参数顺序

<parameterMaps>
    <parameterMap id="parameterMap1" extends="parameterMap2">
      <parameter property="Id" />
      <parameter property="Remark"/>
    </parameterMap>

    <parameterMap id="parameterMap2">
      <parameter property="Name" />
      <parameter property="Phone"/>
    </parameterMap>
  </parameterMaps>

<select id="GetUserList" parameterMap="parameterMap1" resultMap="TestResult">
      SELECT TOP 1000 [Id]
      ,[Name]
      FROM [CYI].[dbo].[ActiveUser] WHERE Name=? AND Phone=? AND Id=? AND Remark=?
    </select>
View Code

3.4.2 <parameter> Elements

3.4.2.1 property

3.4.2.2 column

定义存储过程参数

3.4.2.3 direction 

定义存储过程参数类型

  • Input
  • Output
  • InputOutput

3.4.2.4 dbType

声明参数在数据库中的类型

通常在数据库列为可空列时需要指定。还有就是显示指定类型,如.NEt 中只有System.DateTime一种时间类型 ,而很多数据库会提供至少三种时间类型(DATE, DATETIME,DATESTAMP)。为了正确映射类型需要显示指定。

The dbType attribute can be set to any string value that matches a constant in the specific data type enum of the used provider such as System.Data.SqlDbType for Microsoft Sql Server. Section 3.6 describes the types that are supported by the framework.(在.NET中可以设为枚举System.Data.SqlDbType中的任意一项

3.4.2.5 type

指定属性的CLR类型

3.4.2.6 nullValue

The nullValue attribute can be set to any valid value (based on property type). The nullValue attribute is used to specify an outgoing null value replacement. What this means is that when the value is detected in the object property, a NULL will be written to the database (the opposite behavior of an inbound null value replacement). This allows you to use a magic null number in your application for types that do not support null values (such as intdoublefloat). When these types of properties contain a matching null value (–9999), a NULL will be written to the database instead of the value.

3.4.2.7 size

The size attribute sets the maximum size of the data within the column

3.4.2.8 precision

设置最大位数

3.4.2.9 scale

设置小数位数

3.4.2.10 typeHandler

3.4.3  Inline Parameter Map

ep1

<statement id="insertProduct" parameterClass="Product">
  insert into PRODUCT (PRD_ID, PRD_DESCRIPTION)
  values (#id#, #description#)
</statement>

ep2 设置属性类型

<statement id="insertProduct" parameterClass="Product">
  insert into PRODUCT (PRD_ID, PRD_DESCRIPTION)
  values (#id:int#, #description:VarChar#)
</statement>

ep3 设置 null value (specify the null value replacement)

<statement id="insertProduct" parameterClass="Product">
  insert into PRODUCT (PRD_ID, PRD_DESCRIPTION)
  values (#id:int:-999999#, #description:VarChar#)
</statement>

ep4 另一种使用方式

<update id="UpdateAccountViaInlineParameters" parameterClass="Account">
 update Accounts set
 Account_FirstName = #FirstName#,
 Account_LastName = #LastName#,
 Account_Email = #EmailAddress,type=string,dbType=Varchar,nullValue=no_email@provided.com#
 where
 Account_ID = #Id#
</update>

3.4.4. Standard Type Parameters

当只有一个参数时

<statement id="getProduct" parameterClass="System.Int32">
  select * from PRODUCT where PRD_ID = #value#
</statement>

#value#中value只是站位符可以替换成任意字符

3.4.5. Map or IDictionary Type Parameters

<statement id="getProduct" parameterClass="System.Collections.IDictionary">
  select * from PRODUCT
  where PRD_CAT_ID = #catId#
  and PRD_CODE = #code#
</statement>

这里可以使用3.4.3中内联参数语法设置参数属性

parameterClass="System.Collections.IDictionary" 可使用 map、HashTable、System.Collections.Hashtable

 

posted @ 2020-03-23 16:29  vvf  阅读(452)  评论(0编辑  收藏  举报