zoukankan      html  css  js  c++  java
  • ibatis.net学习笔记(三) SqlMap.xml

    SqlMap.xml 配置文件比较重要 包含了对应关系 以及SQL语句

    首先我们有个mssql2008的表  IDCreater 编号生成器表(主要用在主键而非自增列 自己控制ID编号 具体的好处下次在介绍)

    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    SET ANSI_PADDING ON
    GO
    
    CREATE TABLE [dbo].[IDCreater](
        [IDName] [varchar](50) NOT NULL,
        [CurrentCount] [int] NOT NULL,
        [Description] [nvarchar](200) NOT NULL,
        [CreateTime] [datetime] NOT NULL,
     CONSTRAINT [PK_IDCREATER] PRIMARY KEY CLUSTERED 
    (
        [IDName] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO
    
    SET ANSI_PADDING OFF
    GO

    表有4个字段 主键名称 列长 描述 创建日期

    IDCreater实体类

    #region
    /*******************************************************************************
    ** File Name:            EIDCreater.cs
    ** Creator:                chao.jiang 
    ** EMail:               chao.jiang 
    ** Create date:            
    ** Latest Modifier:
    ** Latest Modify date:
    ** Description:            
    ** Modify History:
    **
    ** Code Review:
    **
    ** Version number:       1.0.0
    *********************************************************************************/
    #endregion
    using System;
    using System.Collections.Generic;
    using System.Text;
            
    namespace EWineShop.Entity
    {
        public class EIDCreater
        {
            /// <summary>
            /// IDName
            /// </summary>
            public  System.String IDName {get;set;}
            
             /// <summary>
            /// CurrentCount
            /// </summary>
            public  System.Int32 CurrentCount {get;set;}
            
             /// <summary>
            /// Description
            /// </summary>
            public  System.String Description {get;set;}
            
             /// <summary>
            /// CreateTime
            /// </summary>
            public  System.DateTime CreateTime {get;set;}
            
         
        }
    }
        

    下面我们来写配置文件,当然这个我是用自己的代码生成器生成的..

    <?xml version="1.0" encoding="utf-8" ?>
    <sqlMap namespace="IDCreaterMap" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <alias>
        <typeAlias alias="IDCreater"  type="EWineShop.Entity.EIDCreater" />
      </alias>
      <resultMaps>
        <resultMap id="FullResultMap" class="IDCreater">
            <result property="IDName" column="IDName" type="System.String" dbType="SqlDbType.VarChar"/> 
            <result property="CurrentCount" column="CurrentCount" type="System.Int32" dbType="SqlDbType.Int"/>
            <result property="Description" column="Description" type="System.String" dbType="SqlDbType.NVarChar"/>
            <result property="CreateTime" column="CreateTime" type="System.DateTime" dbType="SqlDbType.DateTime"/>
        </resultMap>
      </resultMaps>
    
      <parameterMaps>
        <parameterMap id="CreateNumberID" parameterClass="Hashtable">
          <parameter property="IDName"  column="IDName"  type="string" dbType="NVarChar" direction="Input"/>
          <parameter property="Description"  column="Description" type="string" dbType="NVarChar" direction="Input"/>
          <parameter property="Seed"  column="Seed" type="int" dbType="Int" direction="Input"/>
          <parameter property="ReturnID" column="ReturnID"   type="int" dbType="Int" direction="Output" />
        </parameterMap>
      </parameterMaps>
      
      <statements>
        <select id="SelectAll" resultMap="FullResultMap">
          SELECT * FROM IDCreater
        </select>
        
        <select id="SelectByIDName" parameterClass="String" resultMap="FullResultMap" extends="SelectAll">
          WHERE
          (IDCreater.IDName = #IDName#)
        </select>
    
        <update id="UpdateByIDName" parameterClass="IDCreater">
          UPDATE IDCreater SET   IDName=#IDName# ,    CurrentCount=#CurrentCount# ,    Description=#Description# ,    CreateTime=#CreateTime#         WHERE IDName = #IDName#
        </update>
    
        <delete id="DeleteByIDName" parameterClass="string">
          DELETE FROM IDCreater  WHERE IDName = #IDName#
        </delete>
        
        <insert id="InsertIDCreater" parameterClass="IDCreater">
          INSERT INTO IDCreater(IDName, CurrentCount, Description, CreateTime )
          VALUES(#IDName# , #CurrentCount# , #Description# , #CreateTime#  )
      </insert>
    
        <procedure id="GetNewID" resultClass="int" parameterMap="CreateNumberID">
          CreateID
        </procedure>
        
        
      </statements>
    </sqlMap>
     

    "sqlMap namespace=" 配置文件的namespace

    每个ResultMap都有一个自己的ID,如果你在sqlmap.config中没有配置使用命名空间的话,那么这个ResulteMap ID是全局(这点在所有的iBATIS配置元素都是一样的),ResultMap一个重要的属性的是class,它将决定这个ResultMap对应的实 例的类,换句话讲,它的作用是指出结果集要映射的数据类型。在extends属性中可以设置它将要继承的ResultMap,如果给他指定的了值,那么它 将会从super Resultmap继承所的映射配置字段。定义如下:

    <resultMaps>
        <resultMap id="FullResultMap" class="IDCreater">
        </resultMap>
      </resultMaps>

    如果你有正确配置了iBATIS的XSD架构文件的话,那么这时候就会提示resultMap的定义是不完全的。没错,接下来就是要定义Result元 素。每一个result元素都是定义一个字段与数据类属性对应的映射。在每一个result元素有比较多的属性参数,其中property和column 是必须的,其它的参数属性都是可选的。所以我们在每一个resultMap中必须定义超过一个以上的result定义。通常以下的配置就可以完成基本的配 置了。

      <resultMaps>
        <resultMap id="FullResultMap" class="IDCreater">
            <result property="IDName" column="IDName" type="System.String" dbType="SqlDbType.VarChar"/> 
            <result property="CurrentCount" column="CurrentCount" type="System.Int32" dbType="SqlDbType.Int"/>
            <result property="Description" column="Description" type="System.String" dbType="SqlDbType.NVarChar"/>
            <result property="CreateTime" column="CreateTime" type="System.DateTime" dbType="SqlDbType.DateTime"/>
        </resultMap>
      </resultMaps>

    property对应 IDCreater实体类的属性 column="IDName" 对应数据库的 "IDName"字段 type="" 声明.net字段类型 dbtype声明DB类型

    <statements></statements>节点就是sql语法了 下篇详细介绍
     <parameterMaps> 就是声明存储过程

     
  • 相关阅读:
    hdu 2554 最短路 (dijkstra)
    hdu 1217 Arbitrage (spfa)
    盘点:2018年双11背后的蚂蚁核心技术
    跨境物流链路怎么做?菜鸟工程师打造了全球通关“神器”
    用简单代码看卷积组块发展
    分析core,是从案发现场,推导案发经过
    全图化引擎(AI·OS)中的编译技术
    开源 serverless 产品原理剖析
    手把手教您将 libreoffice 移植到函数计算平台
    在数据采集器中用TensorFlow进行实时机器学习
  • 原文地址:https://www.cnblogs.com/jcgh/p/2628356.html
Copyright © 2011-2022 走看看