zoukankan      html  css  js  c++  java
  • 代码生成T4模版生成SpringMVC构造REST代码:第八篇 用T4模版生成请求实体类代码

    这两天一直在学习代码生成之类的问题,上午正好有机会和大家讨论一下.

        现在的SpringMVC,还不支撑泛型输入参数,所以我们不得不再添加一层请求实体类”。通过这类model,Json解析器把传入的json字符串转换成响应的请求实体类,然后控制层的action(方法)使用它作为输入参数进行响应的功能处理。

        第一步,在“解决方案JavaGenerate”中添加类库,用于存放请求实体类的模版及响应文件,我们命名这个类库为JavaRequertModels。注意框架选择。

        第二步,增长t4空模版,在解决方案管理器中,选择JavaRequertModels项目,点击右键,选择“添加 ”--〉“新建项”,选择"Blank T4 Template",输入名称“JavaRequertModels.tt",然后点击"添加".

        第三步,修改JavaRequertModels.tt模版,我直接贴代码

        

        每日一道理
    人生好似一条河,既有波澜壮阔,汹涌澎湃,也有清风徐来,水波不兴;人生好似一首歌,既有欢乐的音符,也有悲壮的旋律;人生好似一条船,既有一帆风顺时,也有急流险滩处。愿我们都能勇于经受暴风雨的洗礼,接受生活的挑战和考验!
    <#@ 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+ "Request.java");//输出的类文件名称,及开始输出文件
    #>
    package com.jiahe.rest.demo2.models;
    /*********************************************************************************
     * Copyright (c) Jiahe LIMITED  2012 All Rights Reserved
     * 系统名称:
     * 程序模块文件名称:<#=filePascialName+ "Request.java"#>
     * 择要:
    *********************************************************************************/
    
    import java.io.Serializable;
    
    /*********************************************************************************
     * 
     * <pre>
     * [版本说明]
     * 1.0    2012/08/30   初版
     * </pre>
     *  @version  1.0 2013/5/16
     *  @author   lxh
     *********************************************************************************/
    public class <#=filePascialName#>Request implements Serializable	{
    
    	private static final long serialVersionUID = 1L;
    
    	private Header header;//求情头
    	
    	private <#=filePascialName#> <#=fileCamelName#>;//请求对象
    
    	public Header getHeader() {
    		return header;
    	}
    
    	public void setHeader(Header header) {
    		this.header = header;
    	}
    
    	public <#=filePascialName#> get<#=filePascialName#>() {
    		return <#=fileCamelName#>;
    	}
    
    	public void set<#=filePascialName#>(<#=filePascialName#> <#=fileCamelName#>) {
    		this.<#=fileCamelName#> = <#=fileCamelName#>;
    	}
    
    	@Override
    	public String toString() {
    		return "<#=filePascialName#>Reauest [header=" + header + ", <#=fileCamelName#>="
    				+ <#=fileCamelName#> + "]";
    	}
    
    	public <#=filePascialName#>Request() {
    		super();
    	}
    
    	public <#=filePascialName#>Request(Header header, <#=filePascialName#> <#=fileCamelName#>) {
    		super();
    		this.header = header;
    		this.<#=fileCamelName#> = <#=fileCamelName#>;
    	}
    
    <#
    }
    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;
    }
    #>

        

        

        

        

    文章结束给大家分享下程序员的一些笑话语录: 人在天涯钻,哪儿能不挨砖?日啖板砖三百颗,不辞长做天涯人~

    --------------------------------- 原创文章 By
    代码和生成
    ---------------------------------

  • 相关阅读:
    app卡顿问题检测--KMCGeigerCounter
    报错---[UIApplication _runWithMainScene:transitionContext:completion:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3505.16/UIApplication.m:3294**
    键盘工具栏的快速集成--IQKeyboardManager
    iOS 对网络视频采集视频截图
    iOS-label出现未知边框线的bug
    iOS开发中图片方向的获取与更改
    通过代码设置button中文字的对齐方式
    util.date
    统计字符串每个字母的个数
    异常处理之多重catch
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3102375.html
Copyright © 2011-2022 走看看