(原创)代码生成器让你抓狂,结束你选择,就是如此简单!

代码生成器是提高生产力的工具,目前的代码生成器太多了,CodeSmith,MyGeneration,动软,应该是目前用的比较多的几个代码生成器,但是东西往往不尽人意,复杂的东西太麻烦,简单的又不灵活,我现在像大家介绍一个简单而有强大的代码生成方法,简单的你无法想象,我前段时间也在写代码生成器,发现有几种方法可以实现代码生成器,一种是用字符串直接连接起来,这个可太难看,而且应该要修改也不方便,还有就是模板生成,用模板来生成,这个好.简单有灵活,我现在介绍另一种方法就是xml+xslt来生成代码,这个相当强大,只需要2个文件就可以搞定你的代码生成器,一个xml文件,还有就是一个xslt文件,xml文件大家都知道是什么,我就不用介绍了,xslt就是xml的样式文件,大家应该看过很多.xml结尾的网站,这些网站需要输出网页的风格就需要xslt来格式化,否则只能看到一个xml文档,下面我们开始吧.

第一点我们需要一个xml文件,这个xml你可以用c#来生成大致的结构是如下

 

<? xml version="1.0" encoding="UTF-8" ?>
< Root >
    
< Table >
        
<!--    名称空间    -->
        
< NameSpace > NameSpace </ NameSpace >
        
<!--    类名    -->
        
< ClassName > ClassName </ ClassName >
        
<!--    表名    -->
        
< TableName > TableName </ TableName >
        
<!--    时间    -->
        
< DateTime > DateTime </ DateTime >
        
<!--    主键    -->
        
< PrimaryKey >
            
< FieldName >
                
< Field > Field </ Field >
                
< Key > true </ Key >
                
< Identity > true </ Identity >
                
< DataType > int </ DataType >
                
< DbType > Int </ DbType >
                
< Length > 4 </ Length >
                
< isNull > false </ isNull >
                
< DefualtValue ></ DefualtValue >
                
< ColumnDescription > 员工ID </ ColumnDescription >
            
</ FieldName >
            
< FieldName >
                
< Field > Field1 </ Field >
                
< Key > true </ Key >
                
< Identity > true </ Identity >
                
< DataType > int </ DataType >
                
< DbType > Int </ DbType >
                
< Length > 4 </ Length >
                
< isNull > false </ isNull >
                
< DefualtValue ></ DefualtValue >
                
< ColumnDescription > 员工ID </ ColumnDescription >
            
</ FieldName >
        
</ PrimaryKey >
        
<!--    自动增长    -->
        
< Identity >
            
< FieldName >
                
< Field > Field </ Field >
                
< Key > false </ Key >
                
< Identity > true </ Identity >
                
< DataType > int </ DataType >
                
< DbType > Int </ DbType >
                
< Length > 4 </ Length >
                
< isNull > false </ isNull >
                
< DefualtValue ></ DefualtValue >
                
< ColumnDescription > 员工ID </ ColumnDescription >
            
</ FieldName >
        
</ Identity >
        
<!--    字段详细内容    -->
        
< DataField >
            
< FieldName >
                
< Field > EmployeeID </ Field >
                
< Key > true </ Key >
                
< Identity > true </ Identity >
                
< DataType > int </ DataType >
                
< DbType > Int </ DbType >
                
< Length > 4 </ Length >
                
< isNull > false </ isNull >
                
< DefualtValue ></ DefualtValue >
                
< ColumnDescription > 员工ID </ ColumnDescription >
            
</ FieldName >
            
< FieldName >
                
< Field > LastName </ Field >
                
< Key > false </ Key >
                
< Identity > false </ Identity >
                
< DataType > string </ DataType >
                
< DbType > NvarChar </ DbType >
                
< Length > 20 </ Length >
                
< isNull > false </ isNull >
                
< DefualtValue > "" </ DefualtValue >
                
< ColumnDescription > 名称ID </ ColumnDescription >
            
</ FieldName >
            
< FieldName >
                
< Field > FirstName </ Field >
                
< Key > false </ Key >
                
< Identity > false </ Identity >
                
< DataType > string </ DataType >
                
< DbType > NvarChar </ DbType >
                
< Length > 10 </ Length >
                
< isNull > false </ isNull >
                
< DefualtValue > "" </ DefualtValue >
                
< ColumnDescription > </ ColumnDescription >
            
</ FieldName >
            
< FieldName >
                
< Field > BirthDate </ Field >
                
< Key > false </ Key >
                
< Identity > false </ Identity >
                
< DataType > DateTime </ DataType >
                
< DbType > DateTime </ DbType >
                
< Length ></ Length >
                
< isNull > false </ isNull >
                
< DefualtValue > getdate() </ DefualtValue >
                
< ColumnDescription > 生日 </ ColumnDescription >
            
</ FieldName >
            
< FieldName >
                
< Field > Sex </ Field >
                
< Key > false </ Key >
                
< Identity > false </ Identity >
                
< DataType > string </ DataType >
                
< DbType > NvarChar </ DbType >
                
< Length > 1 </ Length >
                
< isNull > true </ isNull >
                
< DefualtValue > "" </ DefualtValue >
                
< ColumnDescription > 性别 </ ColumnDescription >
            
</ FieldName >
            
< FieldName >
                
< Field > Address </ Field >
                
< Key > false </ Key >
                
< Identity > false </ Identity >
                
< DataType > string </ DataType >
                
< DbType > NvarChar </ DbType >
                
< Length > 200 </ Length >
                
< isNull > false </ isNull >
                
< DefualtValue > "" </ DefualtValue >
                
< ColumnDescription > 地址 </ ColumnDescription >
            
</ FieldName >
            
< FieldName >
                
< Field > Company </ Field >
                
< Key > false </ Key >
                
< Identity > false </ Identity >
                
< DataType > string </ DataType >
                
< DbType > NvarChar </ DbType >
                
< Length > 100 </ Length >
                
< isNull > false </ isNull >
                
< DefualtValue > "" </ DefualtValue >
                
< ColumnDescription > 公司 </ ColumnDescription >
            
</ FieldName >
            
< FieldName >
                
< Field > Age </ Field >
                
< Key > false </ Key >
                
< Identity > false </ Identity >
                
< DataType > int </ DataType >
                
< DbType > int </ DbType >
                
< Length > 4 </ Length >
                
< isNull > false </ isNull >
                
< DefualtValue > "" </ DefualtValue >
                
< ColumnDescription > 年龄 </ ColumnDescription >
            
</ FieldName >
            
< FieldName >
                
< Field > Phone </ Field >
                
< Key > false </ Key >
                
< Identity > false </ Identity >
                
< DataType > string </ DataType >
                
< DbType > NvarChar </ DbType >
                
< Length > 30 </ Length >
                
< isNull > true </ isNull >
                
< DefualtValue > "" </ DefualtValue >
                
< ColumnDescription > 电话 </ ColumnDescription >
            
</ FieldName >
            
< FieldName >
                
< Field > Mobile </ Field >
                
< Key > false </ Key >
                
< Identity > false </ Identity >
                
< DataType > string </ DataType >
                
< DbType > NvarChar </ DbType >
                
< Length > 20 </ Length >
                
< isNull > true </ isNull >
                
< DefualtValue > "" </ DefualtValue >
                
< ColumnDescription > 手机 </ ColumnDescription >
            
</ FieldName >
            
< FieldName >
                
< Field > UserId </ Field >
                
< Key > false </ Key >
                
< Identity > false </ Identity >
                
< DataType > int </ DataType >
                
< DbType > Int </ DbType >
                
< Length > 10 </ Length >
                
< isNull > false </ isNull >
                
< DefualtValue > 用户ID </ DefualtValue >
                
< ColumnDescription ></ ColumnDescription >
            
</ FieldName >
            
< FieldName >
                
< Field > FirstName </ Field >
                
< Key > false </ Key >
                
< Identity > false </ Identity >
                
< DataType > string </ DataType >
                
< DbType > NvarChar </ DbType >
                
< Length > 10 </ Length >
                
< isNull > false </ isNull >
                
< DefualtValue > "" </ DefualtValue >
                
< ColumnDescription > </ ColumnDescription >
            
</ FieldName >
            
< FieldName >
                
< Field > FirstName </ Field >
                
< Key > false </ Key >
                
< Identity > false </ Identity >
                
< DataType > string </ DataType >
                
< DbType > NvarChar </ DbType >
                
< Length > 10 </ Length >
                
< isNull > false </ isNull >
                
< DefualtValue > "" </ DefualtValue >
                
< ColumnDescription > </ ColumnDescription >
            
</ FieldName >
            
< FieldName >
                
< Field > FirstName </ Field >
                
< Key > false </ Key >
                
< Identity > false </ Identity >
                
< DataType > string </ DataType >
                
< DbType > NvarChar </ DbType >
                
< Length > 10 </ Length >
                
< isNull > false </ isNull >
                
< DefualtValue > "" </ DefualtValue >
                
< ColumnDescription > </ ColumnDescription >
            
</ FieldName >
            
        
</ DataField >
    
</ Table >
</ Root >

 

这个是我的格式的xml文件,当你可以生成一个自己想要个别的xml文件,比如通过一个sql语句来查询数据库把,表结构,和字段信息,查出来,然后生成一个这样子的xml文件就可以

最重要的是xlst文件.这个才是最重要的东西.这个xml就是数据文件.

下面我们要的就是模板文件了.其实这个模板文件就是一个xslt的文件.用他来格式化xml,让以一定风格来输出这个xml

我们先用上面的xml文件来生成对表的添加,删除,修改,获取实体的sql语句操作

 

<? xml version="1.0" encoding="utf-8" ?>
< xsl:stylesheet  version ="1.0"  xmlns:xsl ="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl
="urn:schemas-microsoft-com:xslt"  exclude-result-prefixes ="msxsl"
>
    
< xsl:output  method ="xml"  indent ="yes" />

    
< xsl:template  match ="/" >
create proc 
< xsl:value-of  select ="Root/Table/TableName" /> _Add
--
< xsl:value-of  select ="Root/Table/DateTime" /> --
        
< xsl:call-template  name ="DefinitionAddParameters" />
as

    insert into 
< xsl:value-of  select ="Root/Table/TableName" />  
                ( 
< xsl:call-template  name ="GetAddField" /> )
                values
                ( 
< xsl:call-template  name ="GetAddParameters" />
                
                
< xsl:call-template  name ="IdentityOutput" />
                
go             
                
create proc 
< xsl:value-of  select ="Root/Table/TableName" /> _Update
--
< xsl:value-of  select ="Root/Table/DateTime" /> --
        
< xsl:call-template  name ="DefinitionUpdateParameters" />
as

    update 
< xsl:value-of  select ="Root/Table/TableName" />  set 
                
< xsl:call-template  name ="GetUpdateField" />
                
< xsl:call-template  name ="GetUpdateWhere" />
                
                
go

create proc 
< xsl:value-of  select ="Root/Table/TableName" /> _Delete
--
< xsl:value-of  select ="Root/Table/DateTime" /> --
< xsl:call-template  name ="DefinitionDeleteParameters" />
as
    
    delete from 
< xsl:value-of  select ="Root/Table/TableName" />
                
< xsl:call-template  name ="GetDeleteWhere" />
                
                
go

create proc 
< xsl:value-of  select ="Root/Table/TableName" /> _GetModel
--
< xsl:value-of  select ="Root/Table/DateTime" /> --
< xsl:call-template  name ="DefinitionDeleteParameters" />
as

    select  
< xsl:call-template  name ="GetAddField" />
                from 
< xsl:value-of  select ="Root/Table/TableName" />< xsl:text  >   </ xsl:text >< xsl:call-template  name ="GetModelWhere" />
    
</ xsl:template >
    
< xsl:template  name ="IdentityOutput" >
     
< xsl:for-each  select ="Root/Table/PrimaryKey/FieldName" >
    
< xsl:if  test ="position()!=last()" >
                set @
< xsl:value-of  select ="Field" />  = scope_identity()
    
</ xsl:if >
    
< xsl:if  test ="position()=last()" >
                set @
< xsl:value-of  select ="Field" />  = scope_identity()
    
</ xsl:if >
    
</ xsl:for-each >
</ xsl:template >
<!-- 定义参数 -->
< xsl:template  name ="DefinitionAddParameters" >
    
< xsl:for-each  select ="Root/Table/DataField/FieldName" >
        
< xsl:if  test ="Key='true'" >
        
< xsl:if  test ="position()!=last()" >
@
< xsl:value-of  select ="Field" />< xsl:text  >   </ xsl:text >< xsl:value-of  select ="DbType" />< xsl:if  test ="Length!=''" > ( < xsl:value-of  select ="Length"   /> ) </ xsl:if >  output,  < xsl:if  test ="ColumnDescription!=''" > -- < xsl:value-of  select ="ColumnDescription" /></ xsl:if >
        
</ xsl:if >
                
< xsl:if  test ="position()=last()" >
@
< xsl:value-of  select ="Field" />< xsl:text  >   </ xsl:text >< xsl:value-of  select ="DbType" />< xsl:if  test ="Length!=''" > ( < xsl:value-of  select ="Length"   /> ) </ xsl:if >  output  < xsl:if  test ="ColumnDescription!=''" > -- < xsl:value-of  select ="ColumnDescription" /></ xsl:if >
        
</ xsl:if >
        
</ xsl:if >
        
< xsl:if  test ="Key='false'" >
        
< xsl:if  test ="position()!=last()" >
@
< xsl:value-of  select ="Field" />< xsl:text  >   </ xsl:text >< xsl:value-of  select ="DbType" />   < xsl:if  test ="Length!=''" > ( < xsl:value-of  select ="Length"   /> ) </ xsl:if > , < xsl:if  test ="ColumnDescription!=''" >< xsl:text  >   </ xsl:text > -- < xsl:value-of  select ="ColumnDescription" /></ xsl:if >
        
</ xsl:if >
                
< xsl:if  test ="position()=last()" >
@
< xsl:value-of  select ="Field" />< xsl:text  >   </ xsl:text >< xsl:value-of  select ="DbType" />   < xsl:if  test ="Length!=''" > ( < xsl:value-of  select ="Length"   /> ) </ xsl:if >   < xsl:if  test ="ColumnDescription!=''" >< xsl:text  >   </ xsl:text > -- < xsl:value-of  select ="ColumnDescription" /></ xsl:if >
        
</ xsl:if >
        
</ xsl:if >
    
</ xsl:for-each >
</ xsl:template >
<!-- 生成AddSQL -->
< xsl:template  name ="GetAddField" >
    
< xsl:for-each  select ="Root/Table/DataField/FieldName" >
            
< xsl:if  test ="position() mod 8 != 0" >
                
< xsl:if  test ="position()!=last()" >
                
< xsl:value-of  select ="Field" />< xsl:text  >   </ xsl:text > </ xsl:if >
                
< xsl:if  test ="position()=last()" >
                
< xsl:value-of  select ="Field" />
                
< xsl:text  >   </ xsl:text >
                
</ xsl:if >
            
</ xsl:if >
            
< xsl:if  test ="position() mod 8 = 0" >
                
< xsl:if  test ="position()!=last()" >
                    
< xsl:value-of  select ="Field" />
                    
< xsl:text  >   </ xsl:text >  ,
                
</ xsl:if >
                
< xsl:if  test ="position()=last()" >
                    
< xsl:value-of  select ="Field" />
                    
< xsl:text  >   </ xsl:text >
                
</ xsl:if >
            
</ xsl:if >
    
</ xsl:for-each >
</ xsl:template >
<!-- 生成AddSQL -->
< xsl:template  name ="GetAddParameters" >
    
< xsl:for-each  select ="Root/Table/DataField/FieldName" >
        
< xsl:if  test ="Identity!='true'" >
            
< xsl:if  test ="position() mod 8 != 0" >
                
< xsl:if  test ="position()!=last()" > @ < xsl:value-of  select ="Field" />< xsl:text  >   </ xsl:text > </ xsl:if >
                
< xsl:if  test ="position()=last()" > @ < xsl:value-of  select ="Field" />
                
< xsl:text  >   </ xsl:text > )
                
</ xsl:if >
            
</ xsl:if >
            
< xsl:if  test ="position() mod 8 = 0" >
                
< xsl:if  test ="position()!=last()" > @ < xsl:value-of  select ="Field" />
                    
< xsl:text  >   </ xsl:text > ,
                
</ xsl:if >
                
< xsl:if  test ="position()=last()" > @ < xsl:value-of  select ="Field" />
                    
< xsl:text  >   </ xsl:text > )
                
</ xsl:if >
            
</ xsl:if >
        
</ xsl:if >
    
</ xsl:for-each >
</ xsl:template >
<!-- 定义Update参数 -->
< xsl:template  name ="DefinitionUpdateParameters" >
    
< xsl:for-each  select ="Root/Table/DataField/FieldName" >
        
< xsl:if  test ="Key='true'" >
            
< xsl:if  test ="position()!=last()" >
@
< xsl:value-of  select ="Field" />< xsl:text  >   </ xsl:text >< xsl:value-of  select ="DbType" />< xsl:if  test ="Length!=''" > ( < xsl:value-of  select ="Length"   /> ) </ xsl:if >  output,  < xsl:if  test ="ColumnDescription!=''" > -- < xsl:value-of  select ="ColumnDescription" /></ xsl:if >
            
</ xsl:if >
            
< xsl:if  test ="position()=last()" >
@
< xsl:value-of  select ="Field" />< xsl:text  >   </ xsl:text >< xsl:value-of  select ="DbType" />< xsl:if  test ="Length!=''" > ( < xsl:value-of  select ="Length"   /> ) </ xsl:if >  output  < xsl:if  test ="ColumnDescription!=''" > -- < xsl:value-of  select ="ColumnDescription" /></ xsl:if >
            
</ xsl:if >
        
</ xsl:if >
        
< xsl:if  test ="Key='false'" >
            
< xsl:if  test ="position()!=last()" >
@
< xsl:value-of  select ="Field" />< xsl:text  >   </ xsl:text >< xsl:value-of  select ="DbType" />   < xsl:if  test ="Length!=''" > ( < xsl:value-of  select ="Length"   /> ) </ xsl:if > , < xsl:if  test ="ColumnDescription!=''" >< xsl:text  >   </ xsl:text > -- < xsl:value-of  select ="ColumnDescription" /></ xsl:if >
            
</ xsl:if >
            
< xsl:if  test ="position()=last()" >
@
< xsl:value-of  select ="Field" />< xsl:text  >   </ xsl:text >< xsl:value-of  select ="DbType" />   < xsl:if  test ="Length!=''" > ( < xsl:value-of  select ="Length"   /> ) </ xsl:if >   < xsl:if  test ="ColumnDescription!=''" >< xsl:text  >   </ xsl:text > -- < xsl:value-of  select ="ColumnDescription" /></ xsl:if >
            
</ xsl:if >
        
</ xsl:if >
    
</ xsl:for-each >
</ xsl:template >
<!-- 生成UpdateSQL -->
< xsl:template  name ="GetUpdateField" >
    
< xsl:for-each  select ="Root/Table/DataField/FieldName" >
        
< xsl:if  test ="Identity!='true'" >
            
< xsl:if  test ="position() mod 4 != 0" >
                
< xsl:if  test ="position()!=last()" >
                
< xsl:value-of  select ="Field" />  = @ < xsl:value-of  select ="Field" />  ,  </ xsl:if >
                
< xsl:if  test ="position()=last()" >
                
< xsl:value-of  select ="Field" />  = @ < xsl:value-of  select ="Field" />
                
< xsl:text  >   </ xsl:text >
                
</ xsl:if >
            
</ xsl:if >
            
< xsl:if  test ="position() mod 4 = 0" >
                
< xsl:if  test ="position()!=last()" >
                    
< xsl:value-of  select ="Field" />  = @ < xsl:value-of  select ="Field" />
                    
< xsl:text  >   </ xsl:text >  ,
                
</ xsl:if >
                
< xsl:if  test ="position()=last()" >
                    
< xsl:value-of  select ="Field" />  = @ < xsl:value-of  select ="Field" />
                    
< xsl:text  >   </ xsl:text >
                
</ xsl:if >
            
</ xsl:if >
        
</ xsl:if >
    
</ xsl:for-each >
</ xsl:template >
<!-- 生成Update条件 -->
< xsl:template  name ="GetUpdateWhere" >
                where 
< xsl:for-each  select ="Root/Table/PrimaryKey/FieldName" >
                
< xsl:if  test ="position()!=last()" >< xsl:value-of  select ="Field" />  = @ < xsl:value-of  select ="Field" />  ,  </ xsl:if >
                
< xsl:if  test ="position()=last()" >
                    
< xsl:value-of  select ="Field" />  = @ < xsl:value-of  select ="Field" />
                
</ xsl:if >
    
</ xsl:for-each >
</ xsl:template >
<!-- 生成Update条件 -->
< xsl:template  name ="DefinitionDeleteParameters" >
    
< xsl:for-each  select ="Root/Table/PrimaryKey/FieldName" >
            
< xsl:if  test ="position()!=last()" >
@
< xsl:value-of  select ="Field" />< xsl:text  >   </ xsl:text >< xsl:value-of  select ="DbType" />< xsl:if  test ="Length!=''" > ( < xsl:value-of  select ="Length"   /> ) </ xsl:if > < xsl:if  test ="ColumnDescription!=''" > -- < xsl:value-of  select ="ColumnDescription" /></ xsl:if >
            
</ xsl:if >
            
< xsl:if  test ="position()=last()" >
@
< xsl:value-of  select ="Field" />< xsl:text  >   </ xsl:text >< xsl:value-of  select ="DbType" />< xsl:if  test ="Length!=''" > ( < xsl:value-of  select ="Length"   /> ) </ xsl:if >   < xsl:if  test ="ColumnDescription!=''" >< xsl:text  >   </ xsl:text > -- < xsl:value-of  select ="ColumnDescription" /></ xsl:if >
        
</ xsl:if >
    
</ xsl:for-each >
</ xsl:template >
<!-- 生成Delete条件 -->
< xsl:template  name ="GetDeleteWhere" >
                where 
< xsl:for-each  select ="Root/Table/PrimaryKey/FieldName" >
                
< xsl:if  test ="position()!=last()" >< xsl:value-of  select ="Field" />  = @ < xsl:value-of  select ="Field" />  ,  </ xsl:if >
                
< xsl:if  test ="position()=last()" >
                    
< xsl:value-of  select ="Field" />  = @ < xsl:value-of  select ="Field" />
                
</ xsl:if >
    
</ xsl:for-each >
</ xsl:template >
<!-- 生成getmodel条件 -->
< xsl:template  name ="GetModelWhere" > where  < xsl:for-each  select ="Root/Table/PrimaryKey/FieldName" >
                
< xsl:if  test ="position()!=last()" >< xsl:value-of  select ="Field" />  = @ < xsl:value-of  select ="Field" />  ,  </ xsl:if >
                
< xsl:if  test ="position()=last()" >
                    
< xsl:value-of  select ="Field" />  = @ < xsl:value-of  select ="Field" />
                
</ xsl:if >
    
</ xsl:for-each >
</ xsl:template >
</ xsl:stylesheet >

 

这个就是一个xslt文件,注意他也有自己的语法,不过超级简单,大家可以学习下,真的太简单了.

http://www.w3school.com.cn/xml/xml_xsl.asp 大家可以去这里学习一下xslt语法.这个真是个好东西,如果你看了.我这片文章,你还不知道有xslt这个东西,那真的我只能说你真是太让人失望了,呵呵...

xslt的语法不是我要重要介绍的.我要介绍的是代码生成器,当然如果你需要生成不同的代码,只需要需该我上面生成的xslt文件就可以做到.

xslt格式化xml有很多种方法,比如js,或者.net,都可以很简单的来实现.下面我用vs2008来生成这个代码来看看

用vs2008打开这2个文件,或者直接拖到vs2008 里面编辑

(原创)代码生成器让你抓狂,结束你选择,就是如此简单!_第1张图片

 

选择vs的工具条中的 显示xlst输出 ,然后选择这个xml文件,就会输出你要的存储过程了.

 

< ?xml version = " 1.0 " encoding = "utf - 8 "? >
create   proc  TableName_Add
-- DateTime--
        
@EmployeeID   Int ( 4 ) output,  -- 员工ID
@LastName   NvarChar ( 20 ),  -- 名称ID
@FirstName   NvarChar ( 10 ),  --
@BirthDate   DateTime -- 生日
@Sex   NvarChar ( 1 ),  -- 性别
@Address   NvarChar ( 200 ),  -- 地址
@Company   NvarChar ( 100 ),  -- 公司
@Age   int ( 4 ),  -- 年龄
@Phone   NvarChar ( 30 ),  -- 电话
@Mobile   NvarChar ( 20 ),  -- 手机
@UserId   Int ( 10 ),
@FirstName   NvarChar ( 10 ),  --
@FirstName   NvarChar ( 10 ),  --
@FirstName   NvarChar ( 10 --
as

    
insert   into  TableName 
                ( EmployeeID , LastName , FirstName , BirthDate , Sex , Address , Company , Age  ,
                Phone , Mobile , UserId , FirstName , FirstName , FirstName )
                
values
                ( 
@LastName  ,  @FirstName  ,  @BirthDate  ,  @Sex  ,  @Address  ,  @Company  ,  @Age  ,
                
@Phone  ,  @Mobile  ,  @UserId  ,  @FirstName  ,  @FirstName  ,  @FirstName  )
                
                
set   @Field   =   scope_identity ()
    
                
set   @Field1   =   scope_identity ()
    
                
go              
                
create   proc  TableName_Update
-- DateTime--
        
@EmployeeID   Int ( 4 ) output,  -- 员工ID
@LastName   NvarChar ( 20 ),  -- 名称ID
@FirstName   NvarChar ( 10 ),  --
@BirthDate   DateTime -- 生日
@Sex   NvarChar ( 1 ),  -- 性别
@Address   NvarChar ( 200 ),  -- 地址
@Company   NvarChar ( 100 ),  -- 公司
@Age   int ( 4 ),  -- 年龄
@Phone   NvarChar ( 30 ),  -- 电话
@Mobile   NvarChar ( 20 ),  -- 手机
@UserId   Int ( 10 ),
@FirstName   NvarChar ( 10 ),  --
@FirstName   NvarChar ( 10 ),  --
@FirstName   NvarChar ( 10 --
as

    
update  TableName  set  
                LastName 
=   @LastName  , FirstName  =   @FirstName  , BirthDate  =   @BirthDate   ,
                Sex 
=   @Sex  , Address  =   @Address  , Company  =   @Company  , Age  =   @Age   ,
                Phone 
=   @Phone  , Mobile  =   @Mobile  , UserId  =   @UserId  , FirstName  =   @FirstName   ,
                FirstName 
=   @FirstName  , FirstName  =   @FirstName  
                
where  Field  =   @Field  , Field1  =   @Field1
                
                
go

create   proc  TableName_Delete
-- DateTime--

@Field   Int ( 4 ),  -- 员工ID
@Field1   Int ( 4 -- 员工ID
as
    
    
delete   from  TableName
                
where  Field  =   @Field  , Field1  =   @Field1
                
                
go

create   proc  TableName_GetModel
-- DateTime--

@Field   Int ( 4 ),  -- 员工ID
@Field1   Int ( 4 -- 员工ID
as

    
select   EmployeeID , LastName , FirstName , BirthDate , Sex , Address , Company , Age  ,
                Phone , Mobile , UserId , FirstName , FirstName , FirstName 
                
from  TableName  where  Field  =   @Field  , Field1  =   @Field1

 

xslt相当灵活,可以说你要什么效果都可以实现,只要你写一个xslt文件这么简单,也就是我们说的模板文件

我们再生成一下dal的代码

当然要一个dal.xslt的文件

 

<? xml version="1.0" encoding="utf-8" ?>
< xsl:stylesheet  version ="1.0"  xmlns:xsl ="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl
="urn:schemas-microsoft-com:xslt"  exclude-result-prefixes ="msxsl"
 xmlns:user
="UserCode" >
    
< xsl:output  method ="xml"  indent ="yes" />
    
< xsl:template  match ="/" >
///
< xsl:value-of  select ="Root/Table/DateTime" />
using System;
using System.Data;
using System.Data.SqlClient;
< xsl:choose >< xsl:when  test ="Root/Table/NameSpace!=''" > using  < xsl:value-of  select ="Root/Table/NameSpace" /> .Model; </ xsl:when >< xsl:otherwise > using Model;
            
</ xsl:otherwise >
        
</ xsl:choose >
        
< xsl:call-template  name ="GetNameSpace" />
    public class 
< xsl:value-of  select ="Root/Table/ClassName" />
    {
        ///
< xsl:text  disable-output-escaping ="yes"   > &lt; summary &gt; </ xsl:text >
        /// 添加
        ///
< xsl:text   disable-output-escaping ="yes" > &lt; /summary &gt; </ xsl:text >
        public bool Add(
< xsl:call-template  name ="GetName" />  model)
        {
            SqlParameter[] parameters ={
< xsl:call-template  name ="GetField" />
            
< xsl:text  > </ xsl:text >

        
< xsl:text  >      </ xsl:text >< xsl:call-template  name ="Add-SetField" />
            if(SQLHelper.RunQuery("
< xsl:value-of  select ="Root/Table/ClassName" /> _Add", parameters))
            {
< xsl:call-template  name ="Add-SetReturn" />
                return true;
            }
            else
            {
                return false;
            }
                
        }

        ///
< xsl:text  disable-output-escaping ="yes"   > &lt; summary &gt; </ xsl:text >
        /// 更新记录
        ///
< xsl:text   disable-output-escaping ="yes" > &lt; /summary &gt; </ xsl:text >
        public bool Update(
< xsl:call-template  name ="GetName" />  model)
        {
            SqlParameter[] parameters ={
< xsl:call-template  name ="GetField" />
            
< xsl:text  > </ xsl:text >
        
< xsl:text  >          </ xsl:text >< xsl:call-template  name ="Update-SetField" />
            if(SQLHelper.RunQuery("
< xsl:value-of  select ="Root/Table/ClassName" /> _Update", parameters))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        ///
< xsl:text  disable-output-escaping ="yes"   > &lt; summary &gt; </ xsl:text >
        /// 删除
        ///
< xsl:text   disable-output-escaping ="yes" > &lt; /summary &gt; </ xsl:text >
        public bool Delete(
< xsl:call-template  name ="GetKeyField" /> )
        {
            SqlParameter[] parameters ={
< xsl:call-template  name ="Delete-GetField" />

            
< xsl:call-template  name ="Delete-SetField" />
            
            if(SQLHelper.RunQuery("
< xsl:value-of  select ="Root/Table/ClassName" /> _Delete", parameters))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        ///
< xsl:text  disable-output-escaping ="yes"   > &lt; summary &gt; </ xsl:text >
        /// 获取实体
        ///
< xsl:text   disable-output-escaping ="yes" > &lt; /summary &gt; </ xsl:text >
        public 
< xsl:call-template  name ="GetName" />  GetModel( < xsl:call-template  name ="GetKeyField" /> )
        {
            SqlParameter[] parameters ={
< xsl:call-template  name ="Delete-GetField" />

            
< xsl:call-template  name ="Delete-SetField" />
            
< xsl:text  > </ xsl:text >   < xsl:text  > </ xsl:text >
        
< xsl:text  >              </ xsl:text >< xsl:call-template  name ="GetName" />   model= new  < xsl:call-template  name ="GetName" /> ();
            DataTable dt = SQLHelper.QueryTable("
< xsl:value-of  select ="Root/Table/ClassName" /> _GetModel", parameters);
            if (dt.Rows.Count 
< xsl:text  disable-output-escaping ="yes"   > &gt; </ xsl:text >  0)
            {
< xsl:call-template  name ="GetModel" />
            }
            else
            {
                model = null;
            }
                
            dt.Clear();
            dt.Dispose();
            return model;
        }

    }
}
    
</ xsl:template >

    
<!-- 获取类 -->
< xsl:template  name ="GetName" >
    
< xsl:choose >
        
< xsl:when  test ="Root/Table/NameSpace!=''" >< xsl:value-of  select ="Root/Table/NameSpace" /> .Model. < xsl:value-of  select ="Root/Table/ClassName" /></ xsl:when >
        
< xsl:otherwise > Model. < xsl:value-of  select ="Root/Table/ClassName" />
        
</ xsl:otherwise >
    
</ xsl:choose >
</ xsl:template >
    
<!-- 获取名称空间 -->
< xsl:template  name ="GetNameSpace" >
        
< xsl:choose >
            
< xsl:when  test ="Root/Table/NameSpace!=''" >
namespace 
< xsl:value-of  select ="Root/Table/NameSpace" /> .DAL
{
            
</ xsl:when >
            
< xsl:otherwise >
namespace DAL
{
            
</ xsl:otherwise >
        
</ xsl:choose >
</ xsl:template >
    
<!-- 获取主键 -->
< xsl:template  name ="GetKeyField" >
    
< xsl:for-each  select ="Root/Table/PrimaryKey/FieldName" >
        
< xsl:value-of  select ="DataType" />
        
< xsl:text  >   </ xsl:text >
        
< xsl:if  test ="position()!=last()" >
            
< xsl:value-of  select ="Field" /> , < xsl:text  >   </ xsl:text >
    
</ xsl:if >
        
< xsl:if  test ="position()=last()" >
            
< xsl:value-of  select ="Field" />
        
</ xsl:if >
    
</ xsl:for-each >
</ xsl:template >
    
<!-- 获取字段声明 -->
< xsl:template  name ="GetField" >
        
< xsl:for-each  select ="Root/Table/DataField/FieldName" >
< xsl:if  test ="position()!=last()" >
                new SqlParameter("@
< xsl:value-of  select ="Field" /> ",SqlDbType. < xsl:value-of  select ="DbType" />< xsl:if  test ="Length!=''" > , < xsl:value-of  select ="Length" /></ xsl:if > ), </ xsl:if >
< xsl:if  test ="position()=last()" >
                new SqlParameter("@
< xsl:value-of  select ="Field" /> ",SqlDbType. < xsl:value-of  select ="DbType" />< xsl:if  test ="Length!=''" > , < xsl:value-of  select ="Length" />
                
</ xsl:if > )}; </ xsl:if >
</ xsl:for-each >
    
</ xsl:template >
    
<!-- (添加)字段赋值 -->
< xsl:template  name ="Add-SetField" >
< xsl:for-each  select ="Root/Table/DataField/FieldName" >
    
< xsl:if  test ="Key='true'" >
        
< xsl:text  >          </ xsl:text > parameters[ < xsl:value-of    select ="position()-1" /> ].Value = ParameterDirection.Output;
    
</ xsl:if >
    
< xsl:if  test ="Key='false'" >
    
< xsl:text  >          </ xsl:text > parameters[ < xsl:value-of    select ="position()-1" /> ].Value = model. < xsl:value-of  select ="Field" /> ;
    
</ xsl:if >
    
</ xsl:for-each >
</ xsl:template >
    
<!-- (更新)字段赋值 -->
    
< xsl:template  name ="Update-SetField" >
        
< xsl:for-each  select ="Root/Table/DataField/FieldName" >
            
< xsl:text  >      </ xsl:text > parameters[ < xsl:value-of    select ="position()-1" /> ].Value = model. < xsl:value-of  select ="Field" /> ;
        
</ xsl:for-each >
    
</ xsl:template >
    
<!-- (添加)设置返回值 -->
< xsl:template  name ="Add-SetReturn" >
    
< xsl:for-each  select ="Root/Table/PrimaryKey/FieldName" >
                model.
< xsl:value-of  select ="Field" />  = ( < xsl:value-of  select ="DataType" /> )parameters[ < xsl:value-of    select ="position()-1" /> ].Value; </ xsl:for-each >
</ xsl:template >
<!-- (删除)获取字段声明 -->
< xsl:template  name ="Delete-GetField" >
    
< xsl:for-each  select ="Root/Table/PrimaryKey/FieldName" >
        
< xsl:if  test ="position()!=last()" >
                new SqlParameter("@
< xsl:value-of  select ="Field" /> ",SqlDbType. < xsl:value-of  select ="DbType" />< xsl:if  test ="Length!=''" > , < xsl:value-of  select ="Length" />
                
</ xsl:if > ), </ xsl:if >
        
< xsl:if  test ="position()=last()" >
                new SqlParameter("@
< xsl:value-of  select ="Field" /> ",SqlDbType. < xsl:value-of  select ="DbType" />< xsl:if  test ="Length!=''" > , < xsl:value-of  select ="Length" />
                
</ xsl:if > )};
        
</ xsl:if >
    
</ xsl:for-each >
</ xsl:template >
    
<!-- (删除)字段赋值 -->
< xsl:template  name ="Delete-SetField" >
    
< xsl:for-each  select ="Root/Table/PrimaryKey/FieldName" >
            parameters[
< xsl:value-of    select ="position()-1" /> ].Value =  < xsl:value-of  select ="Field" /> ; </ xsl:for-each >
</ xsl:template >
<!-- 返回实体 -->
< xsl:template  name ="GetModel" >
    
< xsl:for-each  select ="Root/Table/DataField/FieldName" >
        
< xsl:if  test ="DataType='string'" >
                model.
< xsl:value-of  select ="Field" />  = dt.Rows[0][" < xsl:value-of  select ="Field" /> "].ToString(); </ xsl:if >
        
< xsl:if  test ="DataType!='string'" >
                if (dt.Rows[0]["
< xsl:value-of  select ="Field" /> "].ToString() != "")
                {
                    model.
< xsl:value-of  select ="Field" />  = int.Parse(dt.Rows[0][" < xsl:value-of  select ="Field" /> "].ToString());
                }
</ xsl:if >
    
</ xsl:for-each >
</ xsl:template >
</ xsl:stylesheet >

 

重复上面同样的工作就可以生成出dal的代码了..

选择一个xml文件,我们还是选择上面的xml.然后输出的结果如下

 

<? xml version = " 1.0 "  encoding = " utf-8 " ?>
/// DateTime
using  System;
using  System.Data;
using  System.Data.SqlClient;
using  NameSpace.Model;
namespace  NameSpace.DAL
{
            
    
public   class  ClassName
    {
        
/// <summary>
        
///  添加
        
/// </summary>
         public   bool  Add(NameSpace.Model.ClassName model)
        {
            SqlParameter[] parameters 
= {
                
new  SqlParameter( " @EmployeeID " ,SqlDbType.Int, 4 ),
                
new  SqlParameter( " @LastName " ,SqlDbType.NvarChar, 20 ),
                
new  SqlParameter( " @FirstName " ,SqlDbType.NvarChar, 10 ),
                
new  SqlParameter( " @BirthDate " ,SqlDbType.DateTime),
                
new  SqlParameter( " @Sex " ,SqlDbType.NvarChar, 1 ),
                
new  SqlParameter( " @Address " ,SqlDbType.NvarChar, 200 ),
                
new  SqlParameter( " @Company " ,SqlDbType.NvarChar, 100 ),
                
new  SqlParameter( " @Age " ,SqlDbType. int , 4 ),
                
new  SqlParameter( " @Phone " ,SqlDbType.NvarChar, 30 ),
                
new  SqlParameter( " @Mobile " ,SqlDbType.NvarChar, 20 ),
                
new  SqlParameter( " @UserId " ,SqlDbType.Int, 10 ),
                
new  SqlParameter( " @FirstName " ,SqlDbType.NvarChar, 10 ),
                
new  SqlParameter( " @FirstName " ,SqlDbType.NvarChar, 10 ),
                
new  SqlParameter( " @FirstName " ,SqlDbType.NvarChar, 10 )};

            parameters[
0 ].Value  =  ParameterDirection.Output;
            parameters[
1 ].Value  =  model.LastName;
            parameters[
2 ].Value  =  model.FirstName;
            parameters[
3 ].Value  =  model.BirthDate;
            parameters[
4 ].Value  =  model.Sex;
            parameters[
5 ].Value  =  model.Address;
            parameters[
6 ].Value  =  model.Company;
            parameters[
7 ].Value  =  model.Age;
            parameters[
8 ].Value  =  model.Phone;
            parameters[
9 ].Value  =  model.Mobile;
            parameters[
10 ].Value  =  model.UserId;
            parameters[
11 ].Value  =  model.FirstName;
            parameters[
12 ].Value  =  model.FirstName;
            parameters[
13 ].Value  =  model.FirstName;
    
            
if (SQLHelper.RunQuery( " ClassName_Add " , parameters))
            {
                model.Field 
=  ( int )parameters[ 0 ].Value;
                model.Field1 
=  ( int )parameters[ 1 ].Value;
                
return   true ;
            }
            
else
            {
                
return   false ;
            }
                
        }

        
/// <summary>
        
///  更新记录
        
/// </summary>
         public   bool  Update(NameSpace.Model.ClassName model)
        {
            SqlParameter[] parameters 
= {
                
new  SqlParameter( " @EmployeeID " ,SqlDbType.Int, 4 ),
                
new  SqlParameter( " @LastName " ,SqlDbType.NvarChar, 20 ),
                
new  SqlParameter( " @FirstName " ,SqlDbType.NvarChar, 10 ),
                
new  SqlParameter( " @BirthDate " ,SqlDbType.DateTime),
                
new  SqlParameter( " @Sex " ,SqlDbType.NvarChar, 1 ),
                
new  SqlParameter( " @Address " ,SqlDbType.NvarChar, 200 ),
                
new  SqlParameter( " @Company " ,SqlDbType.NvarChar, 100 ),
                
new  SqlParameter( " @Age " ,SqlDbType. int , 4 ),
                
new  SqlParameter( " @Phone " ,SqlDbType.NvarChar, 30 ),
                
new  SqlParameter( " @Mobile " ,SqlDbType.NvarChar, 20 ),
                
new  SqlParameter( " @UserId " ,SqlDbType.Int, 10 ),
                
new  SqlParameter( " @FirstName " ,SqlDbType.NvarChar, 10 ),
                
new  SqlParameter( " @FirstName " ,SqlDbType.NvarChar, 10 ),
                
new  SqlParameter( " @FirstName " ,SqlDbType.NvarChar, 10 )};

            parameters[
0 ].Value  =  model.EmployeeID;
            parameters[
1 ].Value  =  model.LastName;
            parameters[
2 ].Value  =  model.FirstName;
            parameters[
3 ].Value  =  model.BirthDate;
            parameters[
4 ].Value  =  model.Sex;
            parameters[
5 ].Value  =  model.Address;
            parameters[
6 ].Value  =  model.Company;
            parameters[
7 ].Value  =  model.Age;
            parameters[
8 ].Value  =  model.Phone;
            parameters[
9 ].Value  =  model.Mobile;
            parameters[
10 ].Value  =  model.UserId;
            parameters[
11 ].Value  =  model.FirstName;
            parameters[
12 ].Value  =  model.FirstName;
            parameters[
13 ].Value  =  model.FirstName;
        
            
if (SQLHelper.RunQuery( " ClassName_Update " , parameters))
            {
                
return   true ;
            }
            
else
            {
                
return   false ;
            }
        }
        
        
/// <summary>
        
///  删除
        
/// </summary>
         public   bool  Delete( int  Field,  int  Field1)
        {
            SqlParameter[] parameters 
= {
                
new  SqlParameter( " @Field " ,SqlDbType.Int, 4 ),
                
new  SqlParameter( " @Field1 " ,SqlDbType.Int, 4 )};
        
            parameters[
0 ].Value  =  Field;
            parameters[
1 ].Value  =  Field1;
            
            
if (SQLHelper.RunQuery( " ClassName_Delete " , parameters))
            {
                
return   true ;
            }
            
else
            {
                
return   false ;
            }
        }

        
/// <summary>
        
///  获取实体
        
/// </summary>
         public  NameSpace.Model.ClassName GetModel( int  Field,  int  Field1)
        {
            SqlParameter[] parameters 
= {
                
new  SqlParameter( " @Field " ,SqlDbType.Int, 4 ),
                
new  SqlParameter( " @Field1 " ,SqlDbType.Int, 4 )};
        
            parameters[
0 ].Value  =  Field;
            parameters[
1 ].Value  =  Field1;

            NameSpace.Model.ClassName  model
=   new  NameSpace.Model.ClassName();
            DataTable dt 
=  SQLHelper.QueryTable( " ClassName_GetModel " , parameters);
            
if  (dt.Rows.Count  >   0 )
            {
                
if  (dt.Rows[ 0 ][ " EmployeeID " ].ToString()  !=   "" )
                {
                    model.EmployeeID 
=   int .Parse(dt.Rows[ 0 ][ " EmployeeID " ].ToString());
                }
                model.LastName 
=  dt.Rows[ 0 ][ " LastName " ].ToString();
                model.FirstName 
=  dt.Rows[ 0 ][ " FirstName " ].ToString();
                
if  (dt.Rows[ 0 ][ " BirthDate " ].ToString()  !=   "" )
                {
                    model.BirthDate 
=   int .Parse(dt.Rows[ 0 ][ " BirthDate " ].ToString());
                }
                model.Sex 
=  dt.Rows[ 0 ][ " Sex " ].ToString();
                model.Address 
=  dt.Rows[ 0 ][ " Address " ].ToString();
                model.Company 
=  dt.Rows[ 0 ][ " Company " ].ToString();
                
if  (dt.Rows[ 0 ][ " Age " ].ToString()  !=   "" )
                {
                    model.Age 
=   int .Parse(dt.Rows[ 0 ][ " Age " ].ToString());
                }
                model.Phone 
=  dt.Rows[ 0 ][ " Phone " ].ToString();
                model.Mobile 
=  dt.Rows[ 0 ][ " Mobile " ].ToString();
                
if  (dt.Rows[ 0 ][ " UserId " ].ToString()  !=   "" )
                {
                    model.UserId 
=   int .Parse(dt.Rows[ 0 ][ " UserId " ].ToString());
                }
                model.FirstName 
=  dt.Rows[ 0 ][ " FirstName " ].ToString();
                model.FirstName 
=  dt.Rows[ 0 ][ " FirstName " ].ToString();
                model.FirstName 
=  dt.Rows[ 0 ][ " FirstName " ].ToString();
            }
            
else
            {
                model 
=   null ;
            }
                
            dt.Clear();
            dt.Dispose();
            
return  model;
        }

    }
}
    

 

怎么样,绝对不会比你见过的代码生成器弱,而这个使用是如此的简单,当然还可以生成bll,webui层的代码.不过都需要自己写一个xslt文件而已,大家用了这个肯定会有想法

不想只是这样麻烦的使用,如何把他封装一下,变成有界面的,真正的代码生成器.其实也简单 ,我下面写了一小demo,代码很简单,大家可以下载看看..

(原创)代码生成器让你抓狂,结束你选择,就是如此简单!_第2张图片

轮子就这样被我造出来了,如此简单!

不敢怠慢,奉上代码奉上/Files/mextb1860/CodeFactory.rar

欢迎各位踊跃的仍鸡蛋,和指点错误!

 

 

你可能感兴趣的:(代码生成)