いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

iBatis.NET奮闘記-005 (基本的な操作(Delete)) (Mapper, ISqlMapper, Delete, IDalSession, BeginTransaction)


今回は、Deleteです。


Deleteは、Updateと同じです。
ISqlMapperオブジェクトの以下のメソッドを利用します。

public int Delete(string statementName, object data)

戻り値として返却されるのは、削除件数です。



てことで、いつものようにモデルクラスから作成します。
今回は、Categoriesテーブルの対象となるデータを削除します。


[Category.cs]

using System;
using System.Collections.Generic;
using System.Text;

namespace Gsf.Samples.IBatisNet.Models {

    [Serializable]
    public class Category {

        int      _categoryId = -1;
        string   _categoryName;
        DateTime _created;
        DateTime _updated;

        public int CategoryId{
            get{
                return _categoryId;
            }
            protected set{
                _categoryId = value;
            }
        }

        public string CategoryName{
            get{
                return _categoryName;
            }
            set{
                _categoryName = value;
            }
        }

        public DateTime Created{
            get{
                return _created;
            }
            protected set{
                _created = value;
            }
        }

        public DateTime Updated{
            get{
                return _updated;
            }
            protected set{
                _updated = value;
            }
        }
    }
}


次に、SQL定義ファイルです。


[Category.ibatis.xml]

<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="Categories" 
        xmlns="http://ibatis.apache.org/mapping" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <alias>
        <typeAlias type="Gsf.Samples.IBatisNet.Models.Category" alias="Category"/>
    </alias>

    <statements>
        <select id="FindByCategoryId" parameterClass="int" resultClass="Category">
            <![CDATA[
                select
                     CategoryId
                    ,CategoryName
                    ,Created
                    ,Updated
                from
                    Categories
                where
                    CategoryId = #value#
            ]]>
        </select>

        <delete id="DeleteCategory" parameterClass="int">
            <![CDATA[
                delete Categories
                    where
                        CategoryId = #value#
            ]]>
        </delete>
    </statements>
</sqlMap>


最後に、確認用のサンプルクラスです。

using System;
using System.Collections.Generic;

using NUnit.Framework;

using Gsf.Samples.IBatisNet.Models;

using IBatisNet.DataMapper;
using IBatisNet.Common;

namespace Gsf.Samples.IBatisNet {

    [TestFixture]
    public class IBatisNetSample005 {

        const string DELETE_CATEGORY    = "Categories.DeleteCategory";
        const string FIND_BY_CATEGORYID = "Categories.FindByCategoryId";

        [Test]
        public void データの削除を行なってみる(){

            using(IDalSession session = Mapper.Instance().BeginTransaction()){
                //
                // 削除対象のデータが存在することを確認.
                //
                Assert.IsNotNull(Mapper.Instance().QueryForObject(FIND_BY_CATEGORYID, 1));

                //
                // 以下のデータを削除する。
                //    ・CategoryIdが1のもの(CategoryNameの値は"C#")
                //
                Assert.AreEqual(1, Mapper.Instance().Delete(DELETE_CATEGORY, 1));

                //
                // 削除が正常に完了しているかどうかを確認.
                //
                Assert.IsNull(Mapper.Instance().QueryForObject(FIND_BY_CATEGORYID, 1));

                // わざとトランザクションロールバックする。
                //
                //session.Complete();
            }
        }
    }

    class DummyEntryPoint004{
        static void Main(){
            //
            // noop;
            //
        }
    }
}

次からは、JOIN関連を行なってみます。
1対Nや、N対Nなどですね。