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('\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+"=@"+col.Name+",");
            }
        }
      %>
 <%=sbSql.ToString().Trim('\r','\n',',') %>  
 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;
        }
       
        
    }
}

  

posted @ 2016-06-24 14:16  巴别塔  阅读(674)  评论(0编辑  收藏  举报