簡單體驗一個高性能,簡單,輕量的ORM庫- Dapper (無依賴其它庫,很是方便高效)

步驟1)引入該ORM庫。git

        使用Nuget搜索"Dapper"安裝或者直接從github上下載源碼  (https://github.com/StackExchange/Dapper) 集成到項目中。github

步驟2)初始化數據庫鏈接配置.數據庫

        

using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace CoreLab.DAL.Base
{
    /// <summary>
    /// 功能簡介:基於Dapper這個orm框架的數據庫訪問對象
    /// 建立時間:2020-8-11
    /// 建立人:ww
    /// </summary>
    public class DBaseForDapper
    {
        public static IDbConnection dbConnect_GmUser
        {
            get
            {
                return new MySqlConnection(SqlConnectManager.Instance.SqlConnectStr_Gateway);//SqlConnection默認是Sqlserver的,他們都實現了IDbConnnection
            }
        }
    }
}

步驟3)從簡單的CRUD來體驗Dapper( 因爲僅用於體驗用法,所以直接寫在controller中了).數組

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using CoreLab.Model.ListPageModel;
using CoreLab.Model.Configs;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Http;
using System.Net.Http.Headers;
using System.IO;
using Microsoft.AspNetCore.Hosting;using Newtonsoft.Json;using System.Text;
using CoreLab.DAL;
using CoreLab.DAL.Base;
using Dapper;
using System.Data;namespace CoreLab.Web.Controllers
{
    /// <summary>
    /// 功能簡介:本控制器內的代碼僅用於快速測試.
    /// 建立時間:2020-8-11
    /// 建立人:ww
    /// </summary>
    public class DemoController : BaseController
    {
public string DapperTest_Add(string CardTypeId,string CardTypeName) { try { E_CardType eCardType = new E_CardType() { CardTypeId = CardTypeId, CardTypeName = CardTypeName, CreateTime = DateTime.Now, EnableStatus = 1 }; using (IDbConnection dbConnection = DBaseForDapper.dbConnect_GmUser) { int iReturn = dbConnection.Execute("insert into tb_cardtype(CardTypeId,CardTypeName,EnableStatus) values(@CardTypeId,@CardTypeName,@EnableStatus)", eCardType); return iReturn.ToString(); } } catch(Exception ex) { } return "-1"; } public IActionResult DapperTest_QueryList() { try { using (IDbConnection dbConnection = DBaseForDapper.dbConnect_GmUser) { IList<E_CardType> listCardType = dbConnection.Query<E_CardType>("select * from tb_cardtype",null).ToList(); return new JsonResult(listCardType); } } catch (Exception ex) { } return new JsonResult(null); } public IActionResult DapperTest_QueryEntity(string CardTypeId) { try { using (IDbConnection dbConnection = DBaseForDapper.dbConnect_GmUser) { //若是是in @ids 則對應一個數組而不是對象。 IEnumerable<E_CardType> listCardType = dbConnection.Query<E_CardType>("select * from tb_cardtype where CardTypeId=@CardTypeId", new E_CardType() { CardTypeId= CardTypeId }); if (listCardType != null && listCardType.Count() > 0) { return new JsonResult(listCardType.First()); } return new JsonResult(null); } } catch (Exception ex) { } return new JsonResult(null); } public string DapperTest_Update(string CardTypeId, int EnableStatus) { try { using (IDbConnection dbConnection = DBaseForDapper.dbConnect_GmUser) { int iReturn = dbConnection.Execute($"update tb_cardtype set EnableStatus={EnableStatus} where CardTypeId=@CardTypeId", new E_CardType() { CardTypeId=CardTypeId }); return iReturn.ToString(); } } catch (Exception ex) { } return "-1"; } public string DapperTest_Delete(string CardTypeId) { try { using (IDbConnection dbConnection = DBaseForDapper.dbConnect_GmUser) { int iReturn = dbConnection.Execute($"delete from tb_cardtype where CardTypeId=@CardTypeId", new E_CardType() { CardTypeId = CardTypeId }); return iReturn.ToString(); } } catch (Exception ex) { } return "-1"; } } /// <summary> /// 遊戲卡類型 /// </summary> public class E_CardType { public string CardTypeId { get; set; } public string CardTypeName { get; set; } public DateTime CreateTime { get; set; } public int EnableStatus { get; set; } } }
相關文章
相關標籤/搜索