1、简介
CodeSmith 是一种基于模板的代码生成工具,它使用类似于 ASP.NET的语法来生成任意类型的代码或文本。
2、软件布局
整体布局和visual studio系列相似,用过VS开发对此软件布局会很熟悉,加上模板语句类似ASP.NET对.NET开发人员相对友好。大致布局如下:
注:本文截图和运行环境是CodeSmith Generator 8.0.1
3、连接数据库
添加数据源,具体如下图
SQL连接字符串示例
server=localhost;database=sales;user=root;CharSet=utf8;password=root;port=3306;Allow User Variables=True;
4、新建模板
打开模板浏览器,右击模板文件夹新建模板
5、模板执行
6、执行结果示例
7、常用模板
1)生成实体.cst
<%--
Name: 数据库表实体代码生成器
Author: GoodTime
Description: 实体
DateTime: 2022-1-13
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %><% foreach (ColumnSchema column in this.SourceTable.Columns) { %>public <%= CSharpAlias[column.SystemType.FullName] %> <%= StringUtil.ToPascalCase(column.Name) %>{ get;set; }<% } %>
运行效果
2) 生成构造实体
<%--
Name: 数据库表实体代码生成器
Author: GoodTime
Description: 实体属性
DateTime: 2022-1-13
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %> <% foreach (ColumnSchema column in this.SourceTable.Columns) { %>
private <%= CSharpAlias[column.SystemType.FullName] %> _<%= StringUtil.ToCamelCase(column.Name) %>; public <%= CSharpAlias[column.SystemType.FullName] %> <%= StringUtil.ToPascalCase(column.Name) %>
{ get { return _<%= StringUtil.ToCamelCase(column.Name) %>; } set { _<%= StringUtil.ToCamelCase(column.Name) %> = value; }
} <% } %>
运行效果
3)生成实体类
<%--
Name: 数据库表实体代码生成器
Author: GoodTime
Description: 实体类
DateTime: 2022-1-13
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Debug="False" Description="Create a list of properties from database table." %>
<%@ Property Name="SourceData" Type="SchemaExplorer.DatabaseSchema" Category="Context" Description="Table that the object is based on." %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %> <%foreach( var tb in SourceData.Tables){ %> [TableName("<%=tb.Name %>")] <%foreach (var pk in tb.PrimaryKey.MemberColumns){ %> [PrimaryKey("<%= pk.Name%>")] <%} %> [ExplicitColumns] public partial class <%=StringUtil.ToPascalCase(tb.Name) %> () { <%foreach( var cl in tb.Columns) {%> [Column] public <%=CSharpAlias[cl.SystemType.FullName]%> <%=StringUtil.ToPascalCase(cl.Name) %> { get; set; } <%} %> } <%} %>
运行效果
8、附录
CodeSmith下载地址:https://www.codesmithtools.com/downloads
CodeSmith官方文档:https://codesmith.atlassian.net/wiki/spaces/Generator/pages/529119/User+s+Guide
CodeSmith教程文档:https://www.w3cschool.cn/codesmith/