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


     

  • 相关阅读:
    ThinkPHP5如何引用新建的配置文件?
    MySQL与SQLServer的update left join语法区别
    如何POST一个JSON格式的数据给java接口,获得返回数据
    如何使用ThinkPHP5 ,自动生成目录?
    LeetCode347_TopK
    数据结构算法Review1_堆
    OJ_3_字符串数字相互转换
    C++Review11_指针数组和数组指针
    LeetCode15_三数之和
    LeetCode1_两数之和
  • 原文地址:https://www.cnblogs.com/wisdo/p/4205350.html
Copyright © 2011-2022 走看看