いろいろ備忘録日記

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

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


今回は、Updateです。


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

public int Update(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>

        <insert id="InsertCategory" parameterClass="Category">
            <![CDATA[
                insert into Categories
                    (CategoryName)
                    values
                    (#CategoryName#)
            ]]>

            <selectKey property="CategoryId" resultClass="int" type="post">
                <![CDATA[
                    select @@IDENTITY as value
                ]]>
            </selectKey>
        </insert>

        <update id="UpdateCategory" parameterClass="Category">
            <![CDATA[
                update Categories
                    set
                        CategoryName = #CategoryName#
                        Updated      = getdate()
                    where
                        CategoryId   = #CategoryId#
            ]]>
        </update>
    </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 IBatisNetSample004 {

        const string INSERT_CATEGORY    = "Categories.InsertCategory";
        const string UPDATE_CATEGORY    = "Categories.UpdateCategory";
        const string FIND_BY_CATEGORYID = "Categories.FindByCategoryId";

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

            using(IDalSession session = Mapper.Instance().BeginTransaction()){
                //
                // 更新用のデータを登録する。
                //
                Category newCategory     = new Category();
                newCategory.CategoryName = "テスト用のカテゴリデータ";

                int newCategoryId = (int) Mapper.Instance().Insert(INSERT_CATEGORY, newCategory);

                //
                // 上記で登録を行なったデータをデータベースより取得.
                //
                Category insertedCategory = Mapper.Instance().QueryForObject(FIND_BY_CATEGORYID, newCategoryId);
                Assert.IsNotNull(insertedCategory);

                //
                // データを更新.
                //
                insertedCategory.CategoryName = "更新されたデータ";
                Assert.AreEqual(1, Mapper.Instance().Update(UPDATE_CATEGORY, insertedCategory));

                //
                // 更新が行なわれたデータを再取得し、更新が正常に行なわれているかどうかを確認.
                //
                Category updatedCategory = Mapper.Instance().QueryForObject(FIND_BY_CATEGORYID, insertedCategory.CategoryId);
                Assert.AreEqual(insertedCategory.CategoryId,   updatedCategory.CategoryId);
                Assert.AreEqual("更新されたデータ",            updatedCategory.CategoryName);

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

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


次は、Deleteをやってみます。これもまたUpdateと同じです。