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 public void 三表联表分页测试()
 2 {
 3     var arruser = new int[2] { 1,2 };  // 
 4     string uall = "b.*", pn1 = "%蛋蛋%", pn2 = "%m%";
 5     LockPers lpmodel = new LockPers() { IsDel = false};
 6     Users umodel = new Users() { UserName = "jiaojiao" };
 7     SynNote snmodel = new SynNote() { Name = "木头" };
 8     Expression<Func<LockPers, Users, SynNote, bool>> where = PredicateBuilder.WhereStart<LockPers, Users, SynNote>();
 9     where = where.And((l, u, s) => ( l.Name.Contains(pn1) || l.Name.Contains(pn2) ));
10     where = where.And((lpw, uw, sn) => lpw.IsDel == lpmodel.IsDel);
11     where = where.And((l, u, s) => u.UserName == umodel.UserName);
12     where = where.And((l, u, s) => s.Name == snmodel.Name );
13     where = where.And((l, u, s) => SM.In(u.Id, arruser));
14 
15     DapperSqlMaker<LockPers, Users, SynNote> query = LockDapperUtilsqlite<LockPers, Users, SynNote>
16         .Selec()
17         .Column((lp, u, s) => //null)  //查询所有字段
18             new { lp.Name, lpid = lp.Id, x = "LENGTH(a.Prompt) as len", b = SM.Sql(uall)
                              , scontent = s.Content, sname = s.Name }) 19 .FromJoin(JoinType.Left, (lpp, uu, snn) => uu.Id == lpp.UserId 20 , JoinType.Inner, (lpp, uu, snn) => uu.Id == snn.UserId) 21 .Where(where) 22 .Order((lp, w, sn) => new { lp.EditCount, x = SM.OrderDesc(lp.Name), sn.Content }); 23 24 var result = query.ExcuteSelect(); 25 WriteJson(result); // 查询结果 26 27 Tuple<StringBuilder, DynamicParameters> resultsqlparams = query.RawSqlParams(); 28 WriteSqlParams(resultsqlparams); // 打印sql和参数 29 30 int page = 2, rows = 3, records; 31 var result2 = query.LoadPagelt(page, rows, out records); 32 WriteJson(result2); // 查询结果 33 }

生成的sql :

select  a.Name as Name, a.Id as lpid
    , LENGTH(a.Prompt) as len, b.*
    , c.Content as scontent, c.Name as sname  
from LockPers a  
    left join  Users b on  b.Id = a.UserId   
    inner join  SynNote c on  b.Id = c.UserId  
where  (  a.Name like @Name0  or  a.Name like @Name1  )  
    and  a.IsDel = @IsDel2  and  b.UserName = @UserName3  
    and  c.Name = @Name4  and  b.Id in @Id 
order by  a.EditCount, a.Name desc , c.Content 

 

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