zoukankan      html  css  js  c++  java
  • T4模版生成SpringMVC构造REST代码:第六篇 用T4模版生成ServiceImpl服务实现层代码

    第一步、在“解决方案JavaGenerate”中添加类库,用于存放ServiceImpl的模版及相应文件,我们命名这个类库为JavaServiceImpls。

    点击“解决方案JavaGenerate”,右键,选择“添加 ”--〉“新建项目”,再选择"类库",名称中输入JavaServiceImpls。返回后,在“解决方案JavaGenerate”中增加了一个叫JavaServiceImpls的项目,且自带了一个class.cs的类,我们点击它,再点右键删除它,不用它。

    如图6-1,注意红色方框,特别是要选中.NET Framework4


    图6-1

    第二步,增加t4空模版

    在解决方案管理器中,选择JavaServiceImpls项目,点击右键,选择“添加 ”--〉“新建项”,在弹出的窗体中做图6-2的选择和输入项。


    图6-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+ "ServiceImpl.java");//输出的类文件名称,及开始输出文件
    #>
    package com.jiahe.rest.demo2.service.impl;
    /*********************************************************************************
     * Copyright (c) XXXXX LIMITED  2012 All Rights Reserved
     * 系统名称:
     * 程序模块文件名称:<#=filePascialName+ "ServiceImpl.java"#>
     * 摘要:
    *********************************************************************************/
    
    import java.util.List;
    
    import javax.annotation.Resource;
    
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.jiahe.rest.demo2.dao.<#=filePascialName#>Mapper;
    import com.jiahe.rest.demo2.models.<#=filePascialName#>;
    import com.jiahe.rest.demo2.service.<#=filePascialName#>Service;
    
    /*********************************************************************************
     * 
     * <pre>
     * [版本说明]
     * 1.0    2012/08/30   初版
     * </pre>
     *  @version  1.0 2013/5/16
     *  @author   lxh
     *********************************************************************************/
    
    @Service("<#=fileCamelName#>Service")
    public class <#=filePascialName#>ServiceImpl implements <#=filePascialName#>Service {
    	
    	@Resource  
        private <#=filePascialName#>Mapper mapper;
    
    	/**
    	* 增加一个
    	* @param <#=fileCamelName#>
    	* @return 1 成功,0失败
    	*/
    	@Override@Transactional
    	public int insert(<#=filePascialName#> <#=fileCamelName#>) throws Exception{
    		return mapper.insert(<#=fileCamelName#>);
    	}
    
    	/**
    	* 修改一个
    	* @param <#=fileCamelName#>
    	* @return 1 成功,0失败
    	*/
    	@Override@Transactional
    	public int update(<#=filePascialName#> <#=fileCamelName#>) throws Exception{
    		return mapper.update(<#=fileCamelName#>);
    	}
    
    	/**
    	* 按条件删除记录
    	* @param <#=fileCamelName#>删除的条件
    	* @return n 删除的记录数量
    	*/
    	@Override@Transactional
    	public int deleteByCondition(<#=filePascialName#> <#=fileCamelName#>) throws Exception{
    		return mapper.deleteByCondition(<#=fileCamelName#>);
    	}
    
    	/**
    	* 根据主键删除记录
    	* @param <#=fileCamelName#> 删除的主键
    	* @return  n 删除的记录数量: 1 成功,0失败,大于1的多删了
    	*/
    	@Override@Transactional
    	public int deleteByID(<#=filePascialName#> <#=fileCamelName#>) throws Exception{
    		return mapper.deleteByID(<#=fileCamelName#>);
    	}
    
    	/**
    	* 根据条件查询数据
    	* @param <#=fileCamelName#> 查询条件,需要查的字段赋值进行等查询
    	* @return 满足条件的记录
    	*/
    	@Override
    	public List<<#=filePascialName#>> findByCondition(<#=filePascialName#> <#=fileCamelName#>) throws Exception {
    		return mapper.findByCondition(<#=fileCamelName#>);
    	}
    
    	/**
    	* 根据主键查询数据
    	* @param <#=fileCamelName#> 查询的主键
    	* @return 满足条件的记录
    	*/
    	@Override
    	public <#=filePascialName#> findByID(<#=filePascialName#> <#=fileCamelName#>) throws Exception {
    		return mapper.findByID(<#=fileCamelName#>);
    	}
    
    	/**
    	* 根据条件查询数据数
    	* @param <#=fileCamelName#> 查询条件,需要查的字段赋值进行等查询
    	* @return 满足条件的记录数
    	*/
    	@Override
    	public Long findCountByCondition(<#=filePascialName#> <#=fileCamelName#>) throws Exception {
    		return mapper.findCountByCondition(<#=fileCamelName#>);
    	}
    
    	public <#=filePascialName#>Mapper getMapper() {
    		return mapper;
    	}
    
    	public void setMapper(<#=filePascialName#>Mapper mapper) {
    		this.mapper = mapper;
    	}
    }
    <#
    }
    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;
    }
    #>




  • 相关阅读:
    CSS的学习
    nodejs
    eureka注册中心
    netty的机制
    分布式事务
    spring cloud的基础
    一个发散动画的菜单控件(主要记录控件x,y坐标的运动状况)
    关于ViewPager高度自适应(随着pager页的高度改变Viewpager的高度)
    ViewPager的使用总结
    Android实现沉浸式状态栏(必须高逼格)
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3098023.html
Copyright © 2011-2022 走看看