zoukankan      html  css  js  c++  java
  • 代码生成器 CodeSmith 的使用(四)

    在上一篇的版本中,我们生成了数据库中的字段,使生成的属性更加简洁,可读性也提高了很多,但都是钍对一个数据库的单个表,如果要将数据库中的所有 的表都生成相应的类,表中的字段也都生成属性,运行一次就可以将所有的表中的字段都生成属性。这样不仅提高了代码的生产效率,同时,为我们省去了很多枯燥 乏味的工作,把主要的精力集中在业务的处理上。

    接下来就是要生成一个数据库中所有表中的所有的字段属性。同样的,生成后的规则也有三种, Camel规则, Pascal规则,和原生的

    首先来看完整的 Camel 规则模板:

    <%--
    Name:  Copyright © Sun 2013-2014 All rights reserved
    Contact me:  Sunnydayhu@163.com
    Author:  SpringFileld
    Description: 遍历数据库中所有的表,并映射成 类的属性 Camel 规则
    DateTime: 2014-07-31
    --%>
    
    <%@ 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){ %>
    
        public partial class  <%=StringUtil.ToCamelCase(tb.Name) %>
          {
            <%foreach( var cl in tb.Columns) {%>
    
             public <%=CSharpAlias[cl.SystemType.FullName]%> <%=StringUtil.ToCamelCase(cl.Name)%> { get; set; }
    
            <%} %>
          }
    
    <%} %>

    生成的效果如下:

     public partial class  accCardAccount
          {
    
             public int id { get; set; }
    
    
             public System.DateTime dateCreate { get; set; }
    
    
             public string card { get; set; }
    
    
             public decimal balance { get; set; }
    
    
             public decimal interest { get; set; }
    
    
             public decimal interestRates { get; set; }
    
    
             public string remark { get; set; }
    
          }
    
    
    
        public partial class  accCardComhistory
          {
    
             public int id { get; set; }
    
    
             public System.DateTime dateCreate { get; set; }
    
    
             public string cardNo { get; set; }
    
    
             public decimal bonus { get; set; }
    
    
             public decimal welfare { get; set; }
    
    
             public string rechargeCardNo { get; set; }
    
    
             public decimal rechargeMoney { get; set; }
    
          }


    生成的效果如下:

    public partial class  AccCardAccount
          {
    
             public int Id { get; set; }
    
    
             public System.DateTime DateCreate { get; set; }
    
    
             public string Card { get; set; }
    
    
             public decimal Balance { get; set; }
    
    
             public decimal Interest { get; set; }
    
    
             public decimal InterestRates { get; set; }
    
    
             public string Remark { get; set; }
    
          }
    
    
    
        public partial class  AccCardComhistory
          {
    
             public int Id { get; set; }
    
    
             public System.DateTime DateCreate { get; set; }
    
    
             public string CardNo { get; set; }
    
    
             public decimal Bonus { get; set; }
    
    
             public decimal Welfare { get; set; }
    
    
             public string RechargeCardNo { get; set; }
    
    
             public decimal RechargeMoney { get; set; }
    
          }


     原生的:

    <%--
    Name:  Copyright © Sun 2013-2014 All rights reserved
    Contact me:  Sunnydayhu@163.com
    Author:  SpringFileld
    Description: 遍历数据库中所有的表,并映射成类的属性 Primitive
    DateTime: 2014-07-31
    --%>
    
    <%@ 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){ %>
    
        public partial class  <%=tb.Name %>
          {
            <%foreach( var cl in tb.Columns) {%>
    
             public <%=CSharpAlias[cl.SystemType.FullName]%> <%=cl.Name%> { get; set; }
    
            <%} %>
          }
    
    
    <%} %>


    生成的效果:

     public partial class  acc_card_account
          {
    
             public int Id { get; set; }
    
    
             public System.DateTime DateCreate { get; set; }
    
    
             public string Card { get; set; }
    
    
             public decimal Balance { get; set; }
    
    
             public decimal Interest { get; set; }
    
    
             public decimal InterestRates { get; set; }
    
    
             public string Remark { get; set; }
    
          }
    
    
    
        public partial class  acc_card_comhistory
          {
    
             public int Id { get; set; }
    
    
             public System.DateTime DateCreate { get; set; }
    
    
             public string CardNo { get; set; }
    
    
             public decimal Bonus { get; set; }
    
    
             public decimal Welfare { get; set; }
    
    
             public string RechargeCardNo { get; set; }
    
    
             public decimal RechargeMoney { get; set; }
    
          }
    


     

  • 相关阅读:
    mac 个人账户环境变量位置
    版本控制工具Git
    xmind导图用例转换为表格用例
    Mysql清空表(truncate)与删除表中数据(delete)的区别
    log4j日志级别及使用规范
    洛谷 P3807 【模板】卢卡斯定理
    jmeter环境配置与java环境配置
    NOI2020乱搞记
    Re:ゼロから始める文化課生活
    阅读书单
  • 原文地址:https://www.cnblogs.com/wisdo/p/4205350.html
Copyright © 2011-2022 走看看