代码生成器 CodeSmith 的使用(四)

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

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

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

 

<%--

Name:  Copyright © Sun 2013-2014 All rights reserved

Contact me:  [email protected]

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:  [email protected]

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



      }


 

 

你可能感兴趣的:(code)