生成数据表的映射文件 for iBATIS.NET

Filename: MappingFile.cst

<%--  
  Code Generator 
for  iBATIS.NET  1.2 . 1
  功能描述: 生成数据表的映射文件
--%>
<% @ CodeTemplate Language = " C# "  TargetLanguage = " C# "  LinePragmas = " True "   %>
<% @ Assembly Name = " SchemaExplorer "   %>
<% @ Import Namespace = " SchemaExplorer "   %>
<% @ Property Name = " SourceTable "  Type = " SchemaExplorer.TableSchema "  Category = " DataSource "  
            Description
= " 选择一个数据表 "   %>
<% @ Property Name = " TableRename "  Type = " String "  Category = " DataSource "  
            Description
= " 如果不需要表名到类名的重命名, 请保留空 "   %>
<% @ Property Name = " Assemble "  Type = " String "  Category = " Main "  
            Description
= " 程序集名称 "   %>
<% @ Property Name = " Namespace "  Type = " String "  Category = " Main "  
            Description
= " 类所在的命名空间 "   %>
<% @ Property Name = " DeveloperName "  Type = " String "  Category = " Main "  
            Description
= " 作者 "   %>
<? xml version = " 1.0 "  encoding = " utf-8 "   ?>

< sqlMap 
    
namespace = " <%= ClassName %> "  
    xmlns:xsi
= " http://www.w3.org/2001/XMLSchema-instance "  
    xsi:noNamespaceSchemaLocation
= " SqlMap.xsd " >
    
    
<!--  
          Created: 
<%=  DateTime.Now.ToShortDateString()  %>     Author:   <%=  DeveloperName  %>
          File: 
<%=  ClassName  %> .xml
          Description: 数据表 
<%=  TableName  %>  的映射文件, 使用 CodeSmith 自动生成.
    
-->
    
    
< alias >
        
< typeAlias alias = " <%= ClassName %> "  type = " <%= Namespace %>.<%= ClassName %>, <%= Assemble %> "   />
    
</ alias >
    
    
<!--  返回结果的类属性与数据库字段映射  -->
    
< resultMaps >
        
< resultMap id = " SelectResult "   class = " <%= ClassName %> " >
<%=  GetResultProperty()  %>
        
</ resultMap >
    
</ resultMaps >
    
    
<!--  数据库操作定义块  -->
    
< statements >
    
        
< select id = " selectAll "  parameterClass = " string "  resultMap = " SelectResult " >
            select 
*
            from [
<%=  TableName  %>
        
</ select >
        
        
< select id = " select "  parameterClass = " string "  resultMap = " SelectResult " >
            select 
*
            from [
<%=  TableName  %>
            where 
<%=  GetPrimaryKey()  %>= #value#
        
</ select >
        
        
< insert id = " insert "  parameterClass = " <%= TableRename %> "  resultClass = " int " >
            insert into [
<%=  TableName  %> ]
                (
<%=  GetInsertColumnList( false %> )
            values
                (
<%=  GetInsertColumnList( true %> )
            
< selectKey resultClass = " int "  type = " post "  property = " <%= GetPrimaryKey() %> " >  
                select @@IDENTITY 
as  value
            
</ selectKey >
        
</ insert >
        
        
< update id = " update "  parameterClass = " <%= TableRename %> "  resultClass = " int " >
            update [
<%=  TableName  %> ]
            
set   <%=  GetUpdateColumnList()  %>
            where 
<%=  GetPrimaryKey()  %>= # <%=  GetPrimaryKey()  %> #
            
< selectKey resultClass = " int "  type = " post " >  
                select @@RouCount 
as  value
             
</ selectKey >
        
</ update >
        
        
< delete id = " delete "  parameterClass = " string "  resultClass = " int " >
            delete [
<%=  TableName  %> ]
            where 
<%=  GetPrimaryKey()  %>= #value#
            
< selectKey resultClass = " int "  type = " post " >  
                select @@RouCount 
as  value
             
</ selectKey >
        
</ delete >
        
    
</ statements >
    
</ sqlMap >
< script runat = " template " >
    
///   <summary>
    
///  输出的类名
    
///   </summary>
     private   string  ClassName
    {
        
get
        {
            
return  TableRename.Trim().Length  ==   0   ?   " SourceTable.Name "  : TableRename.Trim();
        }
    }
    
    
///   <summary>
    
///  输入的数据表名
    
///   </summary>
     private   string  TableName
    {
        
get
        {
            
return  SourceTable.Name;
        }
    }
    
    
private   string  GetResultProperty()
    {
        
string  outString  =   string .Empty;
        
for  ( int  i = 0 ; i < SourceTable.Columns.Count; i ++ )
        {
            
string  colName  =  SourceTable.Columns[i].Name.ToString();
            outString 
+=   string .Format( " \t\t\t<result property=\ " { 0 }\ "  column=\ " { 0 }\ "  /> " , colName);
            outString 
+=  (i  ==  SourceTable.Columns.Count - 1 ?   ""  :  " \r\n " ;
        }
        
return  outString;
    }
    
    
private   string  GetInsertColumnList( bool  isValue)
    {
        
string  outString  =   string .Empty;
        
for  ( int  i = 0 ; i < SourceTable.Columns.Count; i ++ )
        {
            
if ( SourceTable.Columns[i].IsPrimaryKeyMember )  continue ;
            
string  colName  =  SourceTable.Columns[i].Name.ToString();
            
if ( isValue )
            {
                outString 
+=   " # "   +  colName  +   " # " ;
            }
            
else
            {
                outString 
+=   " [ "   +  colName  +   " ] " ;
            }
            outString 
+=  (i  ==  SourceTable.Columns.Count - 1 ?   ""  :  " " ;
        }
        
return  outString;
    }
    
    
private   string  GetUpdateColumnList()
    {
        
string  outString  =   string .Empty;
        
for  ( int  i = 0 ; i < SourceTable.Columns.Count; i ++ )
        {
            
if ( SourceTable.Columns[i].IsPrimaryKeyMember )  continue ;
            
string  colName  =  SourceTable.Columns[i].Name.ToString();
            outString 
+=   string .Format( " [{0}]=#{0}# " , colName);
            outString 
+=  (i  ==  SourceTable.Columns.Count - 1 ?   ""  :  " ,\r\n\t\t\t\t " ;
        }
        
return  outString;
    }
    
    
private   string  GetPrimaryKey()
    {
        
string  outString  =   string .Empty;
        
for  ( int  i = 0 ; i < SourceTable.Columns.Count; i ++ )
        {
            
if ( SourceTable.Columns[i].IsPrimaryKeyMember )
            {
                outString 
=  SourceTable.Columns[i].Name;
                
break ;
            }
        }
        
return  outString;
    }
</ script >

你可能感兴趣的:(ibatis)