CodeSmith 简单使用和常用模板

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/

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

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GoodTimeGGB

鼓励一下!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值