zoukankan      html  css  js  c++  java
  • CodeMatic动软自动生成Nhibernate

    前两天调查了下自动生成工具MyGenerationcodesmith前一个版本已经不更新了后面一个太高级生成

    的代码包含了太多东西,没整明白。不过生成的xmlmapping很强大。所以干脆整合一下codesmith和动

    软的pojo。现版本的动态默认带了nhibernate的模版表设计的时候最好不要带下划线之类的

    一、配置下要用的namespace和表名大小写

    二、因为我用的是mysql所以表名默认在windows环境下是小写,不符合编码的规范,所以小改了下模版把首字母大写其它小写参照

    View plaincopyprint?
    
    <#@ template language="c#" HostSpecific="True" #> 
    <#@ output extension= ".cs" #> 
    <# 
    TableHost host = (TableHost)(Host); 
    host.Fieldlist.Sort(CodeCommon.CompareByintOrder); 
    #>
    using System; 
    
    //Nhibernate Code Generation Template 1.0
    //author:MythXin
    //blog:www.cnblogs.com/MythXin
    //Entity Code Generation Template
    namespace <#= host.NameSpace #>.Entity<# if( host.Folder.Length > 0) {#>.<#= host.Folder #><# } #> 
    { 
    <# if( host.TableDescription.Length > 0) {#> 
    //<#= host.TableDescription #>
    <# } #> 
    [Serializable] 
    public class <#= host.GetModelClass(host.TableName.Substring(0, 1).ToUpper() + host.TableName.Substring(1).ToLower()) #> : EntityBase 
    { 
    
    <# foreach (ColumnInfo c in host.Fieldlist) 
    { #>/// <summary>
    /// <#= string.IsNullOrEmpty(c.Description) ? c.ColumnName : c.Description #>
    /// </summary>
    public virtual <#= CodeCommon.DbTypeToCS(c.TypeName) #><# if(c.Nullable) { #><#if(CodeCommon.isValueType(CodeCommon.DbTypeToCS(c.TypeName 
    ))){ #>?<# }#><# }#> <#= c.ColumnName.Substring(0, 1).ToUpper() + c.ColumnName.Substring(1).ToLower() #> 
    { 
    get; 
    set; 
    } 
    <# } #> 
    } }
    <#@ template language="c#" HostSpecific="True" #>
    <#@ output extension= ".cs" #>
    <#
        TableHost host = (TableHost)(Host);
        host.Fieldlist.Sort(CodeCommon.CompareByintOrder);
    #>
    using System;
    
    //Nhibernate Code Generation Template 1.0
    //author:MythXin
    //blog:www.cnblogs.com/MythXin
    //Entity Code Generation Template
    namespace <#= host.NameSpace #>.Entity<# if( host.Folder.Length > 0) {#>.<#= host.Folder #><# } #>
    {
        <# if( host.TableDescription.Length > 0) {#>
         //<#= host.TableDescription #>
        <# } #>
        [Serializable]
        public class <#= host.GetModelClass(host.TableName.Substring(0, 1).ToUpper() + host.TableName.Substring(1).ToLower()) #> : EntityBase
        {
        
              <# foreach (ColumnInfo c in host.Fieldlist)
            { #>/// <summary>
            /// <#= string.IsNullOrEmpty(c.Description)  ? c.ColumnName : c.Description #>
            /// </summary>
            public virtual <#= CodeCommon.DbTypeToCS(c.TypeName) #><# if(c.Nullable) {  #><#if(CodeCommon.isValueType(CodeCommon.DbTypeToCS(c.TypeName
    ))){ #>?<# }#><# }#> <#= c.ColumnName.Substring(0, 1).ToUpper() + c.ColumnName.Substring(1).ToLower() #>
            {
                get; 
                set; 
            }        
            <# } #>
       
        }
    }
    c#的substring带2个参数的方法和java不太一样,
    java和c#中的substring
    如果只有一个参数,意思是一样,取该索引之后的全部字符

    如果有2个参数。Java 的substring第二个参数表示索引号,实际取值是索引号的前一位; C# 的Substring方法第二个参数表示取子字符串的长度

    host.GetModelClass()方法是动软命名规则里面的设置。比如命名规则设置首字母大写那生成的时候就会首字母大写,但是不满足驼峰形类名所以又修改了下:如果前面没有配置命名规则则把下面的host.GetModelClass()去掉自己ToUpper就可以了

    [csharp] view plaincopyprint?

    <#@ template language="c#" HostSpecific="True" #> 
    <#@ output extension= ".cs" #> 
    <# 
    TableHost host = (TableHost)(Host); 
    host.Fieldlist.Sort(CodeCommon.CompareByintOrder); 
    #>
    using System; 
    
    //Nhibernate Code Generation Template 1.0
    
    namespace <#= host.NameSpace #>.Entity<# if( host.Folder.Length > 0) {#>.<#= host.Folder #><# } #> 
    { 
    [Serializable] 
    public class <#= host.GetModelClass(host.TableName.Substring(0, 1).ToUpper()) 
    #><# int index=host.TableName.IndexOf("_"); 
    #><# if(index!=-1){ 
    if(host.TableName.Length>(index+1)){ 
    #><#=host.TableName.Substring(1,index-1)+host.TableName.Substring(index+1,1).ToUpper()+host.TableName.Substring(index+2) 
    #><# }else { 
    } 
    }else { 
    #><#=host.TableName.Substring(1).ToLower() #><# } #> : EntityBase 
    { 
    
    <# foreach (ColumnInfo c in host.Fieldlist) 
    { #>/// <summary>
    /// <#= string.IsNullOrEmpty(c.Description) ? c.ColumnName : c.Description #>
    /// </summary>
    public virtual <#= CodeCommon.DbTypeToCS(c.TypeName) #><# if(c.Nullable) { #><#if(CodeCommon.isValueType(CodeCommon.DbTypeToCS(c.TypeName 
    ))){ #>?<# }#><# }#> <#= c.ColumnName.Substring(0, 1).ToUpper() + c.ColumnName.Substring(1) #> 
    { 
    get; 
    set; 
    } 
    <# } #> 
    
    } 
    } 
    <#@ template language="c#" HostSpecific="True" #>
    <#@ output extension= ".cs" #>
    <#
        TableHost host = (TableHost)(Host);
        host.Fieldlist.Sort(CodeCommon.CompareByintOrder);
    #>
    using System;
    
    //Nhibernate Code Generation Template 1.0
    
    namespace <#= host.NameSpace #>.Entity<# if( host.Folder.Length > 0) {#>.<#= host.Folder #><# } #>
    {
        [Serializable]
        public class <#= host.GetModelClass(host.TableName.Substring(0, 1).ToUpper())
        #><# int index=host.TableName.IndexOf("_");
        #><# if(index!=-1){ 
                 if(host.TableName.Length>(index+1)){ 
                 #><#=host.TableName.Substring(1,index-1)+host.TableName.Substring(index+1,1).ToUpper()+host.TableName.Substring(index+2)
        #><#     }else {
                 }
           }else { 
        #><#=host.TableName.Substring(1).ToLower() #><# }  #>  : EntityBase
        {
        
              <# foreach (ColumnInfo c in host.Fieldlist)
            { #>/// <summary>
            /// <#= string.IsNullOrEmpty(c.Description)  ? c.ColumnName : c.Description #>
            /// </summary>
            public virtual <#= CodeCommon.DbTypeToCS(c.TypeName) #><# if(c.Nullable) {  #><#if(CodeCommon.isValueType(CodeCommon.DbTypeToCS(c.TypeName
    ))){ #>?<# }#><# }#> <#= c.ColumnName.Substring(0, 1).ToUpper() + c.ColumnName.Substring(1) #>
            {
                get; 
                set; 
            }        
            <# } #>
       
        }
    }
    三、用codesmith生成的xml配置文件要注意,默认是会忽略表中字段名的前缀的类似 表名product 字段名为productid 生成的xml文件中的属性就会变成 id

    还有一个问题就是生成的文件名还是小写的。。。可能要改动软的生成文件的地方

    参考:http://www.cnblogs.com/freshman0216/archive/2010/10/02/1840750.html

  • 相关阅读:
    Cat- Linux必学的60个命令
    Cmp- Linux必学的60个命令
    Diff- Linux必学的60个命令
    ls- Linux必学的60个命令
    mv- Linux必学的60个命令
    Find- Linux必学的60个命令
    libvirt
    PHP 设计模式 笔记与总结(2)开发 PSR-0 的基础框架
    Java实现 LeetCode 147 对链表进行插入排序
    Java实现 LeetCode 146 LRU缓存机制
  • 原文地址:https://www.cnblogs.com/hornet/p/4076051.html
Copyright © 2011-2022 走看看