Dapper 链式查询 扩展
Dapper 链式查询扩展 DapperSqlMaker
Github地址:https://github.com/mumumutou/DapperSqlMaker
Demo:
- 查询 TestsDapperSqlMaker\DapperSqlMaker.Test\ SelectDapperSqlMakerTest.cs
- 添加 TestsDapperSqlMaker\DapperSqlMaker.Test\ InsertDapperSqlMakerTest.cs
- 更新 TestsDapperSqlMaker\DapperSqlMaker.Test\ UpdateDapperSqlMakerTest.cs
- 删除 TestsDapperSqlMaker\DapperSqlMaker.Test\ DeleteDapperSqlMakerTest.cs
- 上下文类 TestsDapperSqlMaker\DbDapperSqlMaker\ LockDapperUtilsqlite.cs
简单栗子:
1.查询-联表查询,分页
1 [Test] 2 public void 三表联表分页测试() 3 { 4 LockPers lpmodel = new LockPers() { Name = "%蛋蛋%", IsDel = false}; 5 Users umodel = new Users() { UserName = "jiaojiao" }; 6 SynNote snmodel = new SynNote() { Name = "%木头%" }; 7 Expression<Func<LockPers, Users, SynNote, bool>> where = PredicateBuilder.WhereStart<LockPers, Users, SynNote>(); 8 where = where.And((lpw, uw, sn) => lpw.Name.Contains(lpmodel.Name)); 9 where = where.And((lpw, uw, sn) => lpw.IsDel == lpmodel.IsDel); 10 where = where.And((lpw, uw, sn) => uw.UserName == umodel.UserName); 11 where = where.And((lpw, uw, sn) => sn.Name.Contains(snmodel.Name)); 12 13 DapperSqlMaker<LockPers, Users, SynNote> query = LockDapperUtilsqlite<LockPers, Users, SynNote> 14 .Selec() 15 .Column((lp, u, s) => // null) //查询所有字段 16 new { lp.Id, lp.InsertTime, lp.EditCount, lp.IsDel, u.UserName, s.Content, s.Name }) 17 .FromJoin(JoinType.Left, (lpp, uu, snn) => uu.Id == lpp.UserId 18 , JoinType.Inner, (lpp, uu, snn) => uu.Id == snn.UserId) 19 .Where(where) 20 .Order((lp, w, sn) => new { lp.EditCount, lp.Name, sn.Content }); 21 22 var result = query.ExcuteSelect(); //1. 执行查询 23 WriteJson(result); // 打印查询结果 24 25 Tuple<StringBuilder, DynamicParameters> resultsqlparams = query.RawSqlParams(); 26 WriteSqlParams(resultsqlparams); // 打印生成sql和参数 27 28 int page = 2, rows = 3, records; 29 var result2 = query.LoadPagelt(page, rows, out records); //2. 分页查询 30 WriteJson(result2); // 查询结果 31 }
2.更新-更新部分字段
1 [Test] 2 public void 更新部分字段测试lt() 3 { 4 var issucs = LockDapperUtilsqlite<LockPers>.Cud.Update( 5 s => 6 { 7 s.Name = "测试bool修改1"; 8 s.Content = "update方法内赋值修改字段"; 9 s.IsDel = true; 10 }, 11 w => w.Name == "测试bool修改1" && w.IsDel == true 12 ); 13 Console.WriteLine(issucs); 14 }
3.七联表以上待扩展 copy六联表修改3个文件
- DapperSqlMaker
- Template_DapperSqlMaker 上下文类
- PredicateBuilder 条件拼接类
4.实体生成T4模板使用方法 https://www.cnblogs.com/cl-blogs/p/7205954.html