• dapper的增、删、查改的CodeSmith模板


    <%@ Template Language="C#" TargetLanguage="Text" %>
    <%@ Property Name="BaseNamespace" Type="String" %>
    <%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" Category="Context" Description="Database that the documentation should be based on." %>
    <%-- 要打印的表 --%>
    <%@ Property Name="Table" Type="TableSchema" DeepLoad="True" Optional="False" Category="Context" Description="" %>
     
    <%@ Assembly Name="SchemaExplorer" %>
    <%@ Assembly Name="System.Design" %>
    <%@ Import Namespace="SchemaExplorer" %>
     
    using System;
    using System.Collections.Generic;
    using System.linq;
    using System.Text;
    using <%=BaseNamespace%>.Model;
    using Dapper;
    using MayHuan.Data;
    using System.Collections;
     
    namespace <%=BaseNamespace%>.Services
    {
        public class <%= Table.Name%>Service:I<%= Table.Name%>
        {
            public bool Delete(M<%= Table.Name%> model)
            {
                using (var conn = DBCommon.GetConn())
                {
                    conn.Open();
                    var a = conn.Execute(@"Delete from <%= Table.Name%> where OID=@OID", new { OID = model.OID });
                    conn.Close();
                    return a > 0;
                }
            }
     
            public bool Insert(M<%= Table.Name%> model)
            {
                using (var conn = DBCommon.GetConn())
                {
                    conn.Open();
                    var a = conn.Execute(@"INSERT INTO <%= Table.Name%>
            <%
            System.Text.StringBuilder sbSql=new System.Text.StringBuilder();
            %>
          <%
            
            foreach(ColumnSchema col in Table.Columns){ 
                if(!col.IsPrimaryKeyMember){ 
                    sbSql.AppendLine(col.Name+",");
                }
            }
          %>
          (<%=sbSql.ToString().Trim('
    ','
    ',',') %>)
          VALUES
            <%
            sbSql.Clear();
            foreach(ColumnSchema col in Table.Columns){ 
                if(!col.IsPrimaryKeyMember){
                     sbSql.AppendLine("@"+col.Name+",");
                }
            }
            %>
            (<%=sbSql.ToString().Trim('
    ','
    ',',') %>)",
            <%
            sbSql.Clear();
            foreach(ColumnSchema col in Table.Columns){ 
            	if(!col.IsPrimaryKeyMember){ 
            		sbSql.Append(col.Name+" = model."+col.Name+",");
            	}
            }
            %>
            new { <%=sbSql.ToString().Trim(',') %> });
                    conn.Close();
                    return a > 0;
                }
            }
     
            public bool Update(M<%= Table.Name%> model)
            {
                using (var conn = DBCommon.GetConn())
                {
                    conn.Open();
                    var a = conn.Execute(@"UPDATE <%= Table.Name%> SET 
            <%
            sbSql.Clear();
            foreach(ColumnSchema col in Table.Columns){ 
                if(!col.IsPrimaryKeyMember){ 
                    sbSql.AppendLine(col.Name+"=@"+col.Name+",");
                }
            }
          %>
     <%=sbSql.ToString().Trim('
    ','
    ',',') %>  
     WHERE OID=@OID",
            <%
            sbSql.Clear();
            foreach(ColumnSchema col in Table.Columns){ 
            	if(!col.IsPrimaryKeyMember){ 
            		sbSql.Append(col.Name+" = model."+col.Name+",");
            	}
            }
            %>
            new { <%=sbSql.ToString().Trim(',') %>,OID = model.OID });
                    conn.Close();
                    return a > 0;
                }
            }
     
            public M<%= Table.Name%> GetModelByID(int id)
            {
                using (var conn = DBCommon.GetConn())
                {
                    conn.Open();
                    var a = conn.Query<M<%= Table.Name%>>(@"select * from <%= Table.Name%> where OID=@OID", new { OID =id }).FirstOrDefault();
                    conn.Close();
                    return a;
                }
            }
     
            public Model.PageDataView<M<%= Table.Name%>> GetList(string companyName, int pageNum, int pageSize = 10)
            {
                var criteria = new PageCriteria();
                criteria.CurrentPage = pageNum;
                criteria.Fields = "a.*";
                criteria.PageSize = pageSize;
                criteria.PrimaryKey = "a.OID";
                criteria.TableName = @"<%= Table.Name%> a";
                
                string sqlFilter = "1=1";
                if (!string.IsNullOrEmpty(companyName))
                    sqlFilter += string.Format(" and b.CompanyName like '%{0}%'", companyName);
                criteria.Condition += sqlFilter;
                
                var r = DBCommon.GetPageData<M<%= Table.Name%>>(criteria);
     
                return r;
            }
           
            
        }
    }
    

      

  • 相关阅读:
    mysql where中如何判断不为空
    python- with和上下文管理器
    python-面向对象-多态
    python
    python魔术方法- __init__,_new_,_call_,_str_,_repr_,_add_,_getitem_,_getattr_,_bases_,_dict_,_slots_
    jmeter 改变字体大小
    如何提高onenote同步速度?
    win10 中添加快捷方式到开始菜单
    解决win10电脑连上wifi但是无internet访问故障
    安装office2019版本后, vb无法引用Microsoft excel object library和Microsoft word object library 解决办法
  • 原文地址:https://www.cnblogs.com/babietongtianta/p/5614060.html
Copyright © 2020-2023  润新知