學習MyBatis.Net之路 (二)

7.我們在項目根目錄下建Maps文件夾,並在文件夾中創建PersonMap.xml映射文件,如下

<sqlMap namespace="PratiseMyBatis" xmlns="http://ibatis.apache.org/mapping"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <alias>
    <!--類的別名-->
    <typeAlias alias="Person" type="PratiseMyBatis.Models.Person,PratiseMyBatis"/>
  </alias>

  <resultMaps>
    <!--Person類與db表的映射-->
    <resultMap id="PersonResult" class="Person">
      <result property="ID" column="ID"/>
      <result property="Name" column="Name"/>
      <result property="Age" column="Age" />
      <result property="Sex" column="Sex"  />
    </resultMap>
  </resultMaps>


  <statements>
    <!--插入Sql語句-->
    <insert id="Add" parameterClass="Person" resultClass="Int32">
      insert into Person(Name,Age,Sex)values(#Name#,#Age#,#Sex#)
      <selectKey property="ID" resultClass="int" type="post" >
        SELECT @@identity AS ID
      </selectKey>

    </insert>
    <!--修改Sql語句-->
    <update id="Update" parameterClass="Person">
      update Person set Name=#Name#,Age=#Age#,Sex=#Sex# where ID=#ID#
    </update>

    <!--根據主鍵刪除單條記錄-->
    <delete id="Delete" parameterClass="Int32">
      delete Person where ID=#ID#
    </delete>

    <!--查詢單個實體Sql語句-->
    <select id="Get" parameterClass="Int32" resultMap="PersonResult">
      select * from Person where ID=#ID#
    </select>

    <!--查詢所有記錄-->
    <select id="GetList" resultMap="PersonResult">
      <![CDATA[select * from Person where ID<4]]>
    </select>
  </statements>
</sqlMap>

我們可以看到這段代碼裏有一個命名空間,如下:
這裏寫圖片描述
這個命名空間與此文件中的 statements標籤中配置的SQL語句的id有關,在大型項目中,可能存在大量的 SQL 語句,這時候,爲每個SQL 語句起一個唯一的標識id 就變得並不容易了。爲了解決這個問題,在 mybatis 中,可以爲每個映射文件起一個唯一的命名空間,這樣,定義在這個映射文件中的每個 SQL 語句就成了定義在這個命名空間中的一個 id。只要我們能夠保證每個命名空間是唯一的,即使在不同映射文件中的語句的 id 相同,也就不會衝突了。(這段是網上覆制的),這個是在SqlMap.config配置文件中控制的,就是這個標籤 。

8.接下來我們就要在SqlMap.config文件中配置這個映射文件的路徑了,上面的代碼裏面我們已經配好了
這裏寫圖片描述

9.在XML映射文件中我們已經寫好的基礎的增、刪、改、查SQL語句,接下來我們在項目中建一個Common文件夾,裏面寫一個通用的BaseDA類,裏面進行ISqlMapper的實例化,以及對MyBatis.Net做些基本的封裝(這個是網上覆制的)


using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace PratiseMyBatis.Common
{
    public class BaseDA
    {
        //添加
        public static int Insert<T>(string statementName, T t)
        {
            ISqlMapper iSqlMapper = Mapper.Instance();
            if (iSqlMapper != null)
            {
                return (int)iSqlMapper.Insert(statementName, t);
            }
            return 0;
        }

        //刪除
        public static int Delete<T>(string statementName, T t)
        {
            ISqlMapper iSqlMapper = Mapper.Instance();
            if (iSqlMapper != null)
            {
                return (int)iSqlMapper.Delete(statementName,t);
            }
            return 0;
        }
        //修改
        public static int Update<T>(string statementName, T t) 
        {
            ISqlMapper iSqlMapper = Mapper.Instance();
            if (iSqlMapper != null)
            {
                return (int)iSqlMapper.Update(statementName,t);
            }
            return 0;
        }
        //查詢
        public static T Get<T>(string statementName, int PrimaryKeyId)
        {
            ISqlMapper iSqlMapper = Mapper.Instance();
            if (iSqlMapper != null) 
            {
                return iSqlMapper.QueryForObject<T>(statementName,PrimaryKeyId);
            }
            return default(T);
        }
        //查詢全部
        public static IList<T> QueryForList<T>(string statementName, object parameterObject = null)
        {
            ISqlMapper iSqlMapper = Mapper.Instance();
            if (iSqlMapper != null)
            {
                return  iSqlMapper.QueryForList<T>(statementName,parameterObject);
            }
            return null;
        }

    }
}

9 現在我們就可以控制器中進行操作了

         Person p1 = new Person();
            p1.Name = "亞瑟";
            p1.Age = 25;
            p1.Sex = "男";
            //插入一條信息
            int PID = BaseDA.Insert<Person>("Add", p1);
            //查詢剛插入的信息
            Person per = BaseDA.Get<Person>("Get", PID);
            //對ID爲2的信息進行修改
            Person p2 = new Person();
            p2.ID = 2;
            p2.Name = "葛二蛋";
            p2.Age = 20;
            p2.Sex = "男";
            int updateResult = BaseDA.Update<Person>("Update", p2);

  List<Person> list = (List<Person>)Common.BaseDA.QueryForList<Models.Person>("GetList", null);

view頁面

@model List<PratiseMyBatis.Models.Person>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <table border="1" style="color:green">
            <tr>
                <td>名字</td>
                <td>年齡</td>
                <td>性別</td>
            </tr>
        @foreach (var item in Model)
        {
           <tr>
               <td>@item.Name</td>
               <td>@item.Age</td>
               <td>@item.Sex</td>
           </tr>
        }
        </table>
    </div>
</body>
</html>

這裏寫圖片描述
因爲在PersonMap.xml裏查詢所有信息的SQL語句裏有一個 ID<4。
所以這就是查詢到的信息了,這裏方法中的第一個參數就是我們映射文件中SQL語句標籤中的id名稱,其他的增、刪、改都一樣,就不多說了

發佈了44 篇原創文章 · 獲贊 21 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章