Create springmvc using T4 template to construct rest code: Article 5 Use T4 template to generate service layer interface code

Source: Internet
Author: User

Step 1: Add a class library in solution javagenerate to store service templates and corresponding files. We name this class library assumervices.

Click "solution javagenerate", right-click, and choose "add" --> "New Project", then select "class library", and enter assumervices in the name. After the result is returned, a project named assumervices is added to the solution javagenerate and a class. CS class is provided. Click it and right-click it to delete it without using it.

5-1. Pay attention to the red box, especially to select. Net framework4.

Figure 5-1

Step 2: Add a blank T4 Template

In solution manager, select the assumervices project, right-click the project, and choose "add" --> "new item". In the displayed form, select and enter an item in Figure 5-2.


Figure 5-2

Step 3: Modify the template and paste it directly, and then explain it slowly. In fact, the service layer interface and data layer are basically the same and can be copied and modified.

 

<# @ 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 "; // dblxh in the edmx project. edmx path: metadataworkspace = NULL; bool allmetadataloaded = loader. tryloadallmetadata (inputfile, out metadataworkspace); edmitemcollection itemcollection = (edmitemcollection) metadataworkspace. getitemcollection (DataSpace. cspace); entityframeworktemplatefilemanager filemanager = entityframeworktemplatefilemanager. create (this); // issue the file foreach (entitytype entity in itemcollection. getitems <entitytype> (). orderby (E => E. name) {string filepascialname = getmodelspascialname (entity. name); // pascial-style class name string filecamelname = getmodelscamelname (entity. name); // The Name Of The camel-style class filemanager. startnewfile (filepascialname + "service. java "); // name of the output class file and start output file #> package com. jiahe. rest. demo2.service; /*************************************** **************************************** * ** copyright (c) XXXXX limited 2012 all rights reserved * system name: * program module File Name: <# = filepascialname + "service. java "#> * Abstract: **************************************** **************************************** */import Java. util. list; import COM. jiahe. rest. demo2.models. <# = filepascialname #>; /*************************************** **************************************** * *** <PRE> * [version description] * 1.0 2/08/30 initial version * </PRE> * @ version 1.0 2013/5/16 * @ author lxh ********** **************************************** * *****************************/public interface <# = filepascialname #> service {/*** adds a * @ Param <# = filecamelname #> * @ return 1, 0 failed */Public int insert (<# = filepascialname #> <# = filecamelname #>) throws exception; /*** modify a * @ Param <# = filecamelname #> * @ return 1, 0 failed */Public int Update (<# = filepascialname #> <# = filecamelname #>) throws exception; /*** delete a record by condition * @ Param <# = filecamelname #> Delete the condition * @ return N number of records deleted */Public int deletebycondition (<# = filepascialname #> <# = filecamelname #>) throws exception;/*** Delete record based on primary key * @ Param <# = filecamelname #> Delete primary key * @ return N number of records deleted: 1 successful, 0 failed, more than 1 deleted */Public int deletebyid (<# = filepascialname #>< # = filecamelname #>) throws exception; /*** query data by condition * @ Param <# = filecamelname #> query condition, fields to be queried are assigned values for query * @ return records meeting the condition */public list <# = filepascialname #> findbycondition (<# = filepascialname #>< # = filecamelname # >) throws exception; /*** query data by primary key * @ Param <# = filecamelname #> the queried primary key * @ return records meeting the conditions */Public <# = filepascialname #> findbyid (<# = filepascialname #>< # = filecamelname #>) throws exception;/*** number of data queried by condition * @ Param <# = filecamelname #> query condition, value assignment of fields to be queried * @ return the number of records meeting the condition */public long findcountbycondition (<# = filepascialname #>< # = filecamelname #>) throws exception ;} <#} filemanager. process (); #> <# + // obtain the pascial style name of the class 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 ;#>< # + // get the camel style name of the class 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) ;}#>< # + // obtain the pascial style name of the attribute 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);} // get the camel style name of the attribute 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);} // data type conversion 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 ;}#>

 



 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.