前两天调查了下自动生成工具MyGeneration和codesmith前一个版本已经不更新了后面一个太高级生成的代码包含了太多东西,没整明白。不过生成的xmlmapping很强大。所以干脆整合一下codesmith和动软的pojo。现版本的动态默认带了nhibernate的模版
表设计的时候最好不要带下划线之类的
一、配置下要用的namespace和表名大小写
二、因为我用的是mysql所以表名默认在windows环境下是小写,不符合编码的规范,所以小改了下模版把首字母大写其它小写参照
http://www.cnblogs.com/freshman0216/archive/2010/10/02/1840750.html
<#@ 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就可以了
<#@ 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
还有一个问题就是生成的文件名还是小写的。。。可能要改动软的生成文件的地方