Dapper擴展Dapper.Common框架 Linq To Sql 底層源碼.net ORM框架
阿新 • • 發佈:2019-01-29
rem 常用api 提交 session使用 框架 == actor null back
源代碼:https://github.com/1448376744/Dapper.Common
NUGET: Dapper.Common
QQ群:642555086
一、基本結構,此處可用委托,或動態代理完成 class Program { //類加載時配置一次 static Program() { //配置數據源為mysql SessionFactory.DataSource = ()=>new MySqlConnection("server=127.0.0.1;user id=root;password=1024;database=test;"); //下劃線不敏感,默認不區分大小寫 SessionFactory.MatchNamesWithUnderscores = true; //Session使用靜態代理,記錄會話日誌,生產模式設置false SessionFactory.SessionProxy = true; } static void Main(string[] args) { //變量聲明 ISession session = null; try { //開啟一次數據會話 session = SessionFactory.GetSession(); //開啟事物,取消自動提交 session.Open(false); //事物操作1 //事物操作2 //事物操作3 //....... //提交事物 session.Commit(); } catch (Exception e) { //異常回滾 if (session!=null) { session.Rollback(); } throw e; } finally { //資源釋放 if (session!=null) { session.Close(); } } } } 二、映射,此處可用自動模板完成 /// <summary> /// 對應表student,同名可以省略,字段名不分大小寫,不分下劃線 /// </summary> [Table("student")] public class Student { /// <summary> /// 對應字段ID,主鍵標識列identity=true /// </summary> [Column("ID", true)] public int? Id { get; set; } /// <summary> /// 對應字段ME_NAME,通過Column校正為Name, /// </summary> [Column("ME_NAME", false)] public string MeName { get; set; } /// <summary> /// 對應數據字段AGE無需校正 /// </summary> public int? Age { get; set; } /// <summary> /// 對應字段CREATE_TIME,不分大小寫,下劃線不敏感 /// </summary> [Column("CREATE_TIME", false)] public DateTime? CreateTime { get; set; } } 三、常用API var sesion = SessionFactory.GetSession(); /*****************INSERT*******************/ //Dapper var row1 = sesion.Execute("insert into student(Age,ME_NAME)values(@Age,@MeName)", new { Age = 20, MeName = "Dapper" }); //擴展 var row2 = sesion.From<Student>().Insert(new Student() { Name = "Dapper.Common", Age = 50, CreateTime = DateTime.Now }); var identity = sesion.From<Student>().InsertById(new Student() { Age = 20, Name = "Identity" }); //list var list = new List<Student>(); list.Add(new Student() { Name = "Dapper.Common", Age = 50, CreateTime = DateTime.Now }); var row3 = sesion.From<Student>().Insert(list); /*****************UPDATE*******************/ //根據主鍵修改全部列 var row4 = sesion.From<Student>().Update(new Student() { Id = 27, Name = "Update" }); //list var list2 = new List<Student>(); list2.Add(new Student() { Name = "Update List", Id = 27 }); list2.Add(new Student() { Name = "Update List", Id = 28 }); var row5 = sesion.From<Student>().Update(list2); //修改部分列+條件更新 var entity = new { Age = 20, Name = "admin" }; var row6 = sesion.From<Student>() //如果第一個條件為true,則更新MeName為entity.Name .Set(!string.IsNullOrEmpty(entity.Name), a => a.Name, entity.Name) //Age在原來的基礎上加20 .Set(a => a.Age.Eq(a.Age + entity.Age)) //條件ID=30 .Where(a => a.Id == 30) //要執行的操作 .Update(); /*****************DELETE*******************/ //更據實體ID刪除 var row7 = sesion.From<Student>().Delete(new Student() { Id = 30 }); //條件刪除 var row8 = sesion.From<Student>() .Where(a => a.Age > 20) .Delete(); /*****************Select*******************/ //查詢單個 var student = sesion.From<Student>().Single(); var list1 = sesion.From<Student>().Select(); //復雜查詢 list = sesion.From<Student>() //查詢條件 .Where(a => a.Age > 20 && a.Id.In(new int[] { 1, 2, 3 }.ToList())) //排序 .Desc(a => a.Id) //悲觀鎖 .XLock() //分頁 .Limit(1, 10) //部分列 .Select(s => new { s.Name }); //分頁查詢,返回總記錄數 var total = 10; list = sesion.From<Student>() .Skip(1, 5, out total) .Select(); /*****************動態查詢*******************/ var query = new WhereQuery<Student>(); query .And(a => a.Name.Like("%aa%")) .Or(a => a.Id > 0) .Or(a => a.Id < 10) .Or(a => a.Id.In(new[] { 1, 2, 3 })) .And(a => 1 > 2 ? a.Name.Like("cc%") : a.Id > 100); var res = sesion.From<Student>().Where(query).Exists(); /*****************會話日誌*******************/ var aa = sesion.Logger();
Dapper擴展Dapper.Common框架 Linq To Sql 底層源碼.net ORM框架