码迷,mamicode.com
首页 > Web开发 > 详细

CodeSmith编写ibatisnet生成模板

时间:2015-07-28 00:41:01      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

首先新建一个实例模板文件

<%@ CodeTemplate Language="C#" TargetLanguage="Xml" ResponseEncoding="UTF-8" Description="Template description here." %>

声明该模板文件使用的模板语言

<%@ Property Name="assemblyname" Type="System.String" Default="SomeValue" Optional="True" Category="Strings" Description="This is a sample string property." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the mapping file is based on" %>

声明属性的作用是为了给用户自定义声明

技术分享

<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Import Namespace="System.Data" %>
<script runat="template">
    public string ToLowercase(string str)
    {
        str = str.ToLower();    
        return str;
    }
    
</script>

script标签一般是用来声明方法

<?xml version="1.0" encoding="UTF-8" ?> 
<sqlMap namespace="<%=SourceTable.Name%>" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <alias>
        <typeAlias alias="<%=SourceTable.Name%>" type="<%=assemblyname%>.<%=SourceTable.Name%>, <%=assemblyname%>" />
    </alias>
    <resultMaps>
        <resultMap id="SelectResult" class="<%=SourceTable.Name%>">
            <%for(int i=0;i<SourceTable.Columns.Count;i++){%>
            <result property="<%=SourceTable.Columns[i].Name%>" column="<%=ToLowercase(SourceTable.Columns[i].Name)%>" />
            <%}%>
        </resultMap>
    </resultMaps>
    
    <statements>
        <select id="Select<%=SourceTable.Name%>" parameterClass="int" resultMap="SelectResult">
            Select 
                <%for(int i=0;i<SourceTable.Columns.Count;i++){%>
                  <%=ToLowercase(SourceTable.Columns[i].Name)%><%if(i<SourceTable.Columns.Count - 1){%>,<%}%>
                <%}%>
            From <%=SourceTable.Name%>
            <dynamic prepend="WHERE">
                <isParameterPresent>
                <% if(SourceTable.HasPrimaryKey){ %>
                <%for(int i=0;i<SourceTable.PrimaryKey.MemberColumns.Count;i++){%>
                    <%=ToLowercase(SourceTable.PrimaryKey.MemberColumns[i].Name)%>=#<%=SourceTable.PrimaryKey.MemberColumns[i].Name%># <%if(i<SourceTable.PrimaryKey.MemberColumns.Count-1){%>AND<%}%>
                <%}%>
                <% } %>
                </isParameterPresent>
            </dynamic>
        </select>
        
        <select id="BaseSelect<%=SourceTable.Name%>" parameterClass="int" resultMap="SelectResult">
            Select 
                <%for(int i=0;i<SourceTable.Columns.Count;i++){%>
                  <%=ToLowercase(SourceTable.Columns[i].Name)%><%if(i<SourceTable.Columns.Count - 1){%>,<%}%>
                <%}%>
            From <%=SourceTable.Name%>
        </select>
        
        <select id="ChildSelect<%=SourceTable.Name%>" parameterClass="int" resultMap="SelectResult" extends="BaseSelect<%=SourceTable.Name%>">
            
        </select>
                
        <insert id="Insert<%=SourceTable.Name%>" parameterClass="<%=SourceTable.Name%>">
            Insert Into <%=SourceTable.Name%> (
                <%for(int i=0;i<SourceTable.Columns.Count;i++){%>
                  <%=ToLowercase(SourceTable.Columns[i].Name)%><%if(i<SourceTable.Columns.Count - 1){%>,<%}%>
                <%}%>
            )Values(
                <%for(int i=0;i<SourceTable.Columns.Count;i++){%>
                 #<%=SourceTable.Columns[i].Name%>#<%if(i<SourceTable.Columns.Count - 1){%>,<%}%>
                <%}%>
            )
        </insert>
        
        <update id="Update<%=SourceTable.Name%>" parameterClass="<%=SourceTable.Name%>">
            Update <%=SourceTable.Name%> Set 
                <%for(int i=0;i<SourceTable.Columns.Count;i++){%>
                <%=ToLowercase(SourceTable.Columns[i].Name)%>=#<%=SourceTable.Columns[i].Name%>#<%if(i<SourceTable.Columns.Count - 1){%>,<%}%>
                <%}%>
            <dynamic prepend="WHERE">
                <isParameterPresent>
                <% if(SourceTable.HasPrimaryKey){ %>
                <%for(int i=0;i<SourceTable.PrimaryKey.MemberColumns.Count;i++){%>
                    <%=ToLowercase(SourceTable.PrimaryKey.MemberColumns[i].Name)%>=#<%=SourceTable.PrimaryKey.MemberColumns[i].Name%>#<%if(i<SourceTable.PrimaryKey.MemberColumns.Count-1){%>AND<%}%>
                <%}%>
                <% } %>
                </isParameterPresent>
            </dynamic>
        </update>
        
        <delete id="Delete<%=SourceTable.Name%>" parameterClass="int">
            Delete From <%=SourceTable.Name%>
            <dynamic prepend="WHERE">
                <isParameterPresent>
                <% if(SourceTable.HasPrimaryKey){ %>
                <%for(int i=0;i<SourceTable.PrimaryKey.MemberColumns.Count;i++){%>
                    <%=ToLowercase(SourceTable.PrimaryKey.MemberColumns[i].Name)%>=#<%=SourceTable.PrimaryKey.MemberColumns[i].Name%>#<%if(i<SourceTable.PrimaryKey.MemberColumns.Count-1){%>AND<%}%>
                <%}%>
                <% } %>
                </isParameterPresent>
            </dynamic>
        </delete>
        
    </statements>
</sqlMap>

声明输出的内容

CodeSmith编写ibatisnet生成模板

标签:

原文地址:http://www.cnblogs.com/ilooking/p/4681573.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!