CodeSmith 简单使用和常用模板

简介: CodeSmith 简单使用和常用模板

1、简介

CodeSmith 是一种基于模板的代码生成工具,它使用类似于 ASP.NET的语法来生成任意类型的代码或文本。

2、软件布局

整体布局和visual studio系列相似,用过VS开发对此软件布局会很熟悉,加上模板语句类似ASP.NET对.NET开发人员相对友好。大致布局如下:

注:本文截图和运行环境是CodeSmith Generator 8.0.1

image.gif

3、连接数据库

添加数据源,具体如下图

image.gif

SQL连接字符串示例

server=localhost;database=sales;user=root;CharSet=utf8;password=root;port=3306;Allow User Variables=True;

4、新建模板

打开模板浏览器,右击模板文件夹新建模板

image.gif

5、模板执行

image.gif

6、执行结果示例

image.gif

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; }
<% } %>

image.gif

运行效果

image.gif

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; }  
}  
<% } %>

image.gif

运行效果

image.gif

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; }   
        <%} %>  
      } <%} %>

image.gif

运行效果

image.gif

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/

以上就是CodeSmith 简单使用和常用模板的介绍,做此记录,如有帮助,欢迎点赞关注收藏!

目录
相关文章
|
12月前
QWebEngineView简单使用
QWebEngineView是提供一个访问web页面的widget,这里是一个简单的使用代码
133 0
ApplicationEventPublisher的简单使用
ApplicationEventPublisher的简单使用
331 0
MPPlayer的简单使用
MPPlayer的简单使用
152 0
|
测试技术 程序员 数据库连接
|
测试技术 数据安全/隐私保护