dapper的增删查改的CodeSmith模板

Posted 巴别塔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 [email protected]", 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(‘\r‘,‘\n‘,‘,‘) %>)
      VALUES
        <%
        sbSql.Clear();
        foreach(ColumnSchema col in Table.Columns){ 
            if(!col.IsPrimaryKeyMember){
                 sbSql.AppendLine("@"+col.Name+",");
            }
        }
        %>
        (<%=sbSql.ToString().Trim(‘\r‘,‘\n‘,‘,‘) %>)",
        <%
        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+"[email protected]"+col.Name+",");
            }
        }
      %>
 <%=sbSql.ToString().Trim(‘\r‘,‘\n‘,‘,‘) %>  
 WHERE [email protected]",
        <%
        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 [email protected]", 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;
        }
       
        
    }
}

  

以上是关于dapper的增删查改的CodeSmith模板的主要内容,如果未能解决你的问题,请参考以下文章

增删查改的实现

SQL Server 表的管理_关于表的操作增删查改的操作的详解(案例代码)

数据结构:链表实现增删查改的基本功能内含详细代码,建议收藏

求C++连接mysql数据库 并同时进行增删查改的代码 十分感谢

vector基本内容与增删查改的模拟实现

vector基本内容与增删查改的模拟实现