標籤:des style blog http io color ar os java
前兩天調查了下自動產生工具MyGeneration和codesmith前一個版本已經不更新了後面一個太進階產生
的程式碼封裝含了太多東西,沒整明白。不過產生的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 Templatenamespace <#= 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 Templatenamespace <#= 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.0namespace <#= 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.0namespace <#= 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
CodeMatic動軟自動產生Nhibernate