T4模版產生SpringMVC構造REST代碼:第五篇 用T4模版產生Service服務層介面代碼

來源:互聯網
上載者:User

第一步、在“解決方案JavaGenerate”中添加類庫,用於存放Service的模版及相應檔案,我們命名這個類庫為JavaServices。

點擊“解決方案JavaGenerate”,右鍵,選擇“添加 ”--〉“建立項目”,再選擇"類庫",名稱中輸入JavaServices。返回後,在“解決方案JavaGenerate”中增加了一個叫JavaServices的項目,且內建了一個class.cs的類,我們點擊它,再點右鍵刪除它,不用它。

5-1,注意紅色方框,特別是要選中.NET Framework4

圖5-1

第二步,增加t4空模版

 在解決方案管理器中,選擇JavaServices項目,點擊右鍵,選擇“添加 ”--〉“建立項”,在彈出的表單中做圖5-2的選擇和輸入項。


圖5-2

第三步,修改模版,我直接貼一個,然後慢慢解釋。其實,服務層介面和資料層的基本相同,可以拷貝過來修改。

 

<#@ template language="C#" debug="false" hostspecific="true"#><#@ include file="EF.Utility.CS.ttinclude"#><#@ assembly name="EnvDTE" #><#@ import namespace="EnvDTE"#><#@ output extension=".cs"#><#CodeGenerationTools code = new CodeGenerationTools(this);MetadataLoader loader = new MetadataLoader(this);CodeRegion region = new CodeRegion(this, 1);MetadataTools ef = new MetadataTools(this);string inputFile = @"..\EDMX\dblxh.edmx";//EDMX項目中dblxh.edmx的路徑MetadataWorkspace metadataWorkspace = null;bool allMetadataLoaded =loader.TryLoadAllMetadata(inputFile, out metadataWorkspace);EdmItemCollection ItemCollection = (EdmItemCollection)metadataWorkspace.GetItemCollection(DataSpace.CSpace);EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);// 發出檔案foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)){string filePascialName=getModelsPascialName(entity.Name);//Pascial風格的類名稱string fileCamelName=getModelsCamelName(entity.Name);//Camel風格的類名稱    fileManager.StartNewFile(filePascialName+ "Service.java");//輸出的類檔案名稱,及開始輸出檔案#>package com.jiahe.rest.demo2.service;/********************************************************************************* * Copyright (c) XXXXX LIMITED  2012 All Rights Reserved * 系統名稱: * 程式模組檔案名稱:<#=filePascialName+ "Service.java"#> * 摘要:*********************************************************************************/import java.util.List;import com.jiahe.rest.demo2.models.<#=filePascialName#>;/********************************************************************************* *  * <pre> * [版本說明] * 1.0    2012/08/30   初版 * </pre> *  @version  1.0 2013/5/16 *  @author   lxh *********************************************************************************/public interface <#=filePascialName#>Service {/*** 增加一個* @param <#=fileCamelName#>* @return 1 成功,0失敗*/public int insert(<#=filePascialName#> <#=fileCamelName#>) throws Exception;/*** 修改一個* @param <#=fileCamelName#>* @return 1 成功,0失敗*/public int update(<#=filePascialName#> <#=fileCamelName#>) throws Exception;/*** 按條件刪除記錄* @param <#=fileCamelName#>刪除的條件* @return n 刪除的記錄數量*/public int deleteByCondition(<#=filePascialName#> <#=fileCamelName#>) throws Exception;/*** 根據主鍵刪除記錄* @param <#=fileCamelName#> 刪除的主鍵* @return  n 刪除的記錄數量: 1 成功,0失敗,大於1的多刪了*/public int deleteByID(<#=filePascialName#> <#=fileCamelName#>) throws Exception;/*** 根據條件查詢資料* @param <#=fileCamelName#> 查詢條件,需要查的欄位賦值進行等查詢* @return 滿足條件的記錄*/public List<<#=filePascialName#>> findByCondition(<#=filePascialName#> <#=fileCamelName#>) throws Exception;/*** 根據主鍵查詢資料* @param <#=fileCamelName#> 查詢的主鍵* @return 滿足條件的記錄*/public <#=filePascialName#> findByID(<#=filePascialName#> <#=fileCamelName#>) throws Exception;/*** 根據條件查詢資料數* @param <#=fileCamelName#> 查詢條件,需要查的欄位賦值進行等查詢* @return 滿足條件的記錄數*/public Long findCountByCondition(<#=filePascialName#> <#=fileCamelName#>) throws Exception;}<#}fileManager.Process();#><#+//得到類的Pascial風格名稱string getModelsPascialName(string source){string[] s=source.Split('_');for(int i=0;i<s.Length;i++){string s1=s[i].Substring(0,1).ToUpper();string s2=s[i].Substring(1);s[i]=string.Concat(s1,s2);    }string result=string.Empty;for(int i=1;i<s.Length;i++){result=string.Concat(result,s[i]);}return result;}#><#+//得到類的Camel風格名稱string getModelsCamelName(string source){string[] s=source.Split('_');for(int i=0;i<s.Length;i++){string s1=s[i].Substring(0,1).ToUpper();string s2=s[i].Substring(1);s[i]=string.Concat(s1,s2);    }string result=string.Empty;for(int i=1;i<s.Length;i++){result=string.Concat(result,s[i]);}string s11=result.Substring(0,1).ToLower();string s12=result.Substring(1);return string.Concat(s11,s12);}#><#+//得到屬性的Pascial風格名稱string getPropertyPascialName(string source){string[] s=source.Split('_');for(int i=0;i<s.Length;i++){string s1=s[i].Substring(0,1).ToUpper();string s2=s[i].Substring(1);s[i]=string.Concat(s1,s2);    }return string.Concat(s);}//得到屬性的Camel風格名稱string getPropertyCamelName(string source){string[] s=source.Split('_');for(int i=0;i<s.Length;i++){string s1=s[i].Substring(0,1).ToUpper();string s2=s[i].Substring(1);s[i]=string.Concat(s1,s2);    }string result=string.Concat(s);string s11=result.Substring(0,1).ToLower();string s12=result.Substring(1);return string.Concat(s11,s12);}//資料類型轉換string getPropertyType(string source){string result=string.Empty;if (source=="int") result="Integer";if (source=="integer") result="Integer";if (source=="Integer") result="Integer";if (source=="byte") result="Integer";if (source=="sbyte") result="Integer";if (source=="bool") result="Integer";if (source=="Int16") result="Integer";if (source=="short") result="Integer";if (source=="Int32") result="Integer";if (source=="Nullable<int>") result="Integer";if (source=="Nullable<integer>") result="Integer";if (source=="Nullable<Integer>") result="Integer";if (source=="Nullable<byte>") result="Integer";if (source=="Nullable<sbyte>") result="Integer";if (source=="Nullable<bool>") result="Integer";if (source=="Nullable<boolean>") result="Integer";if (source=="Nullable<Int16>") result="Integer";if (source=="Nullable<short>") result="Integer";if (source=="Nullable<Int32>") result="Integer";if (source=="Int64") result="Long";if (source=="long") result="Long";if (source=="Long") result="Long";if (source=="Nullable<Int64>") result="Long";if (source=="Nullable<long>") result="Long";if (source=="Nullable<Long>") result="Long";if (source=="float") result="Double";if (source=="Float") result="Double";if (source=="decimal") result="Double";if (source=="Decimal") result="Double";if (source=="Nullable<float>") result="Double";if (source=="Nullable<Float>") result="Double";if (source=="Nullable<decimal>") result="Double";if (source=="Nullable<Decimal>") result="Double";if (source=="byte[]") result="byte[]";if (source=="string") result="String";if (source=="String") result="String";if (source=="System.Date") result="String";if (source=="System.Time") result="String";if (source=="System.DateTime") result="String";if (source=="Nullable<System.Date>") result="String";if (source=="Nullable<System.Time>") result="String";if (source=="Nullable<System.DateTime>") result="String";return result;}#>

 



 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.