Model
Model
Model
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Data.DataSetExtensions.dll" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\EF.Utility.CS.ttinclude" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\OutputHelper.ttinclude" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\DbHelper.ttinclude" #>
<#
var manager = Manager.Create(Host, GenerationEnvironment);
foreach(DbTable dbTable in DbHelper.GetDbTables(config.ConnectionString, config.DbDatabase))
{
string item=dbTable.TableName;
manager.StartNewFile(item+".cs");#>
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由T4模板自动生成
// 生成时间 <#=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")#>
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace OADemo.Model
{
public partial class <#=item#>
{
<# foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, item)) {#>
/// <summary>
/// <#=column.Remark#>
/// </summary>
public <#= column.CSharpType#><# if(column.CommonType.IsValueType && column.IsNullable){#>?<#}#> <#=column.ColumnName#> { get; set; }
<#}#>
}
}
<#
manager.EndBlock();
}
manager.Process(true);
#>
DAL
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Data.DataSetExtensions.dll" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\EF.Utility.CS.ttinclude" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\OutputHelper.ttinclude" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\DbHelper.ttinclude" #>
<#
var manager = Manager.Create(Host, GenerationEnvironment);
foreach(DbTable dbTable in DbHelper.GetDbTables(config.ConnectionString, config.DbDatabase))
{
string tableName=dbTable.TableName;
manager.StartNewFile(tableName+"Dal.cs");#>
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由T4模板自动生成
// 生成时间 <#=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")#> by ding
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using OADemo.Model;
using OADemo.Tool;
namespace OADemo.DAL
{
public partial class <#=tableName+"Dal"#>
{
/// <summary>
/// 添加一条数据
/// </summary>
public bool Insert<#=tableName#>(<#=tableName#> entity)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into <#=tableName#>(");
strSql.Append("<#= DbHelper.GetFileStr(tableName) #>");
strSql.Append(" values (");
strSql.Append("<#= DbHelper.GetParStr(tableName) #>");
strSql.Append(" );");
var newClass =
new {<# bool b=false;foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) {if(!column.IsIdentity) {if (b){ #>, <# } #><#= column.ColumnName #> = entity.<#= column.ColumnName #><# b=true;} #>
<# }#>};
return DapperHelper.Excute(strSql.ToString(), newClass) > 0;
}
/// <summary>
/// 更新一条数据
/// </summary>
public bool Update<#=tableName#>(<#=tableName#> entity)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("update <#=tableName#> set ");
<#bool isFirst=true; foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) { if(!column.IsIdentity) { #>
<#if (!isFirst){ #>strSql.Append(","); <# } #>
strSql.Append("<#= column.ColumnName #>[email protected]<#= column.ColumnName #>");
<# isFirst=false; }} #>
strSql.Append(" where ");
<# foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) { if(column.IsIdentity) {#>
strSql.Append("<#= column.ColumnName #>[email protected]<#= column.ColumnName #>");
<# }} #>
var newClass =
new {<# bool u=false;foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) {if(!column.IsIdentity) {if (u){ #>, <# } #><#= column.ColumnName #> = entity.<#= column.ColumnName #><# u=true;} #>
<# }#>};
return DapperHelper.Excute(strSql.ToString(), newClass) > 0;
}
/// <summary>
/// 删除一条数据
/// </summary>
public bool Delete<#=tableName#>(int id)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("delete from <#=tableName#> ");
strSql.Append(" where ");
<# foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) { if(column.IsIdentity) {#>
strSql.Append("<#= column.ColumnName #>[email protected]<#= column.ColumnName #>");
<# }} #>
var newClass =
new {
<#foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) { if(column.IsIdentity) {#> <#= column.ColumnName #>= id<# break;}} #>
};
return DapperHelper.Excute(strSql.ToString(), newClass) > 0;
}
/// <summary>
/// 批量删除数据
/// </summary>
public bool Delete<#=tableName#>(string idList)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("delete from <#=tableName#> ");
strSql.Append(" where ");
strSql.Append(" <#foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) { if(column.IsIdentity) {#><#= column.ColumnName #> <# break;}} #>");
strSql.Append(" in (" + idList + ") ");
return DapperHelper.Excute(strSql.ToString()) > 0;
}
/// <summary>
///根据 主键查询
/// </summary>
public <#=tableName#> SelectOne<#=tableName#>(int id)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select top 1 * from <#=tableName#> ");
strSql.Append(" where ");
<# foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) { if(column.IsIdentity) { #>
strSql.Append("<#= column.ColumnName #>[email protected]<#= column.ColumnName #>");
<# }} #>
var newClass = new {
<#foreach(DbColumn column in DbHelper.GetDbColumns(config.ConnectionString, config.DbDatabase, tableName) ) { if(column.IsIdentity) {#><#= column.ColumnName #>= id<# break;}} #>
};
return DapperHelper.FirstOrDefault<<#=tableName#>>(strSql.ToString(), newClass);
}
/// <summary>
/// 根据条件查询所有实体
/// </summary>
public IEnumerable<<#=tableName#>> Select<#=tableName#>List(string strWhere)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select * ");
strSql.Append(" FROM <#=tableName#> ");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
return DapperHelper.Query<<#=tableName#>>(strSql.ToString());
}
/// <summary>
/// 分页获取数据列表
/// </summary>
public IEnumerable<<#=tableName#>> Select<#=tableName#>ListByPage(string strWhere, string orderby, int startIndex, int endIndex)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("SELECT * FROM ( ");
strSql.Append(" SELECT ROW_NUMBER() OVER (");
if (!string.IsNullOrEmpty(orderby.Trim()))
{
strSql.Append("order by T." + orderby);
}
else
{
strSql.Append("order by T.Id desc");
}
strSql.Append(")AS Row, T.* from <#=tableName#> T ");
if (!string.IsNullOrEmpty(strWhere.Trim()))
{
strSql.Append(" WHERE " + strWhere);
}
strSql.Append(" ) TT");
strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
return DapperHelper.Query<<#=tableName#>>(strSql.ToString());
}
}
}
<#
manager.EndBlock();
}
manager.Process(true);
#>
BLL
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Data.DataSetExtensions.dll" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Data.SqlClient" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\EF.Utility.CS.ttinclude" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\OutputHelper.ttinclude" #>
<#@ include file="D:\代码\OADemo\OADemo.Model\include\DbHelper.ttinclude" #>
<#
var manager = Manager.Create(Host, GenerationEnvironment);
foreach(DbTable dbTable in DbHelper.GetDbTables(config.ConnectionString, config.DbDatabase))
{
string tableName=dbTable.TableName;
manager.StartNewFile(tableName+"Bll.cs");#>
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由T4模板自动生成
// 生成时间 <#=DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")#> by ding
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OADemo.DAL;
using OADemo.Model;
namespace OADemo.BLL
{
public partial class <#=tableName#>Business
{
<#=tableName#>Dal dal = new <#=tableName#>Dal();
/// <summary>
/// 添加一条数据
/// </summary>
public bool Add<#=tableName#>(<#=tableName#> entity)
{
return dal.Insert<#=tableName#>(entity);;
}
/// <summary>
/// 更新一条数据
/// </summary>
public bool Modify<#=tableName#>(<#=tableName#> entity)
{
return dal.Update<#=tableName#>(entity);
}
/// <summary>
/// 删除一条数据
/// </summary>
public bool Remove<#=tableName#>(int id)
{
return dal.Delete<#=tableName#>(id);
}
/// <summary>
/// 批量删除数据
/// </summary>
public bool Remove<#=tableName#>(string idList)
{
return dal.Delete<#=tableName#>(idList);
}
/// <summary>
/// 查询一个实体
/// </summary>
public <#=tableName#> GetOne<#=tableName#>(int id)
{
return dal.SelectOne<#=tableName#>(id);
}
/// <summary>
/// 根据条件查询所有实体
/// </summary>
public List<<#=tableName#>> Get<#=tableName#>List(string strWhere)
{
return dal.Select<#=tableName#>List(strWhere).ToList();
}
/// <summary>
/// 分页获取数据列表
/// </summary>
public List<<#=tableName#>> Get<#=tableName#>ListByPage(string strWhere, string orderby, int startIndex, int endIndex)
{
return dal.Select<#=tableName#>ListByPage(strWhere,orderby,startIndex,endIndex).ToList();
}
}
}
<#
manager.EndBlock();
}
manager.Process(true);
#>