简单体验一个高性能,简单,轻量的ORM库- Dapper (无依赖其它库,非常方便高效)

步骤1)引入该ORM库。

        使用Nuget搜索"Dapper"安装或者直接从github上下载源码  (https://github.com/StackExchange/Dapper) 集成到项目中。

步骤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
{
    /// 
    /// 功能简介:基于Dapper这个orm框架的数据库访问对象
    /// 创建时间:2020-8-11
    /// 创建人:ww
    /// 
    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
{
    /// 
    /// 功能简介:本控制器内的代码仅用于快速测试.
    /// 创建时间:2020-8-11
    /// 创建人:ww
    /// 
    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 listCardType = dbConnection.Query("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 listCardType = dbConnection.Query("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"; } } /// /// 游戏卡类型 /// public class E_CardType { public string CardTypeId { get; set; } public string CardTypeName { get; set; } public DateTime CreateTime { get; set; } public int EnableStatus { get; set; } } }

 

你可能感兴趣的:(简单体验一个高性能,简单,轻量的ORM库- Dapper (无依赖其它库,非常方便高效))