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

    在第二篇中,介绍了用 codesmith 生成数据库中的一些字段,可生成的属性不够简洁,这次对上一次的版本进行重构,生成一些简洁的属性访问器。代码如下:

    Camel 规则:

    <%-- 
    Name:  Copyright © Sun 2013-2014 All rights reserved  
    Contact me:  Sunnydayhu@163.com
    Author:  SpringFileld
    Description: 遍历数据库中的表,并映射成类的属性 
    DateTime: 2014-07-31
    --%>
    
    <%@ 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.ToCamelCase(column.Name) %> { get; set; }
    
    <% } %>


    生成的效果如下:

    public string sheetNo { get; set; }
    
    
    public decimal sheetAmt { get; set; }
    
    
    public System.DateTime operDate { get; set; }
    
    
    public string settleFlag { get; set; }
    
    
    public string transNo { get; set; }

    这次的属性简洁了很多,代码也很整齐,程序的可读性得到了进一步的提高

    Pascal 规则:

    <%-- 
    Name:  Copyright © Sun 2013-2014 All rights reserved  
    Contact me:  <a target=_blank href="mailto:Sunnydayhu@163.com">Sunnydayhu@163.com</a>
    Author:  SprngFileld
    Description: 遍历数据库中的表,并映射成类的属性 
    DateTime: 2014-07-31
    --%>
    
    <%@ 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; }
    
    <% } %>
    

    生成的效果如下:

    public string SheetNo { get; set; }
    
    
    public decimal SheetAmt { get; set; }
    
    
    public System.DateTime OperDate { get; set; }
    
    
    public string SettleFlag { get; set; }
    
    
    public string TransNo { get; set; }

    源生,就是生成的属性的大小写与数据库中的字段完全一样

    <%-- 
    Name:  Copyright © Sun 2013-2014 All rights reserved  
    Contact me:  <a target=_blank href="mailto:Sunnydayhu@163.com">Sunnydayhu@163.com</a>
    Author:  SprngFileld
    Description: 遍历数据库中的表,并映射成类的属性 
    DateTime: 2014-07-31
    --%>
    
    <%@ 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] %> <%=column.Name%> { get; set; }
    
    <% } %>
    

    生成的效果如下:

    public string sheet_no { get; set; }
    
    
    public decimal sheet_amt { get; set; }
    
    
    public System.DateTime oper_date { get; set; }
    
    
    public string settle_flag { get; set; }
    
    
    public string trans_no { get; set; }
    


     



     



     

  • 相关阅读:
    ionic3 学习记录
    关于华为 IOT平台的框架理解
    遥测浮点数的计算 十进制浮点型转为十六进制浮点型
    关于104规约的认识补充
    Php 十六进制短浮点数转十进制,带符号位
    yii2 项目初始化
    java Socket启动服务
    ionic3 关于屏幕方向问题
    ionic3 cordova ionic-native插件
    java javac 的区别
  • 原文地址:https://www.cnblogs.com/wisdo/p/4205347.html
Copyright © 2011-2022 走看看