模板化生成代码

代码生成工具:


package com.dhcc.framework.common.codegen;

import java.io.*;
import java.util.ArrayList;

public abstract class CodeGenUtils
{

    public CodeGenUtils()
    {
    }

    private static String createTemplateType(String type)
    {
        String templateName = "";
        String s;
        switch((s = type.trim().toUpperCase()).hashCode())
        {
        default:
            break;

        case -1592831339: 
            if(s.equals("SERVICE"))
                templateName = SERVICE_TEMPLATE;
            break;

        case 65854: 
            if(s.equals("BLH"))
                templateName = BLH_TEMPLATE;
            break;

        case 67442: 
            if(s.equals("DAO"))
                templateName = DAO_TEMPLATE;
            break;

        case 68031: 
            if(s.equals("DTO"))
                templateName = DTO_TEMPLATE;
            break;

        case 225204789: 
            if(s.equals("SERVICEIMPL"))
                templateName = SERVICEIMPL_TEMPLATE;
            break;

        case 1925345846: 
            if(s.equals("ACTION"))
                templateName = ACTION_TEMPLATE;
            break;
        }
        return templateName;
    }

    private static String to1Upper(String str)
    {
        return (new StringBuilder(String.valueOf((char)((str.charAt(0) - 97) + 65)))).append(str.substring(1)).toString();
    }

    private static String to1Lower(String str)
    {
        return (new StringBuilder(String.valueOf((char)((str.charAt(0) + 97) - 65)))).append(str.substring(1)).toString();
    }

    private static void readTemplateFile2Code(ArrayList parmsList, String type, String proLocation)
    {
        String templateFileName;
        String dirSeparator;
        String postfix;
        String innerDir;
        BufferedReader reader;
        BufferedWriter output;
        templateFileName = (new StringBuilder("/codeTemplate/")).append(createTemplateType(type)).toString();
        dirSeparator = File.separator;
        postfix = to1Upper(type);
        innerDir = ((String)parmsList.get(0)).replaceAll("\\.", "\\\\");
        reader = null;
        output = null;
        try
        {
            InputStreamReader fr = new InputStreamReader(com/dhcc/framework/common/codegen/CodeGenUtils.getResourceAsStream(templateFileName));
            reader = new BufferedReader(fr);
            String tempString = null;
            File outFile = null;
            if("serviceimpl".equals(type))
            {
                System.out.println("======start gene serviceimpl code ======");
                String filePath = (new StringBuilder("src")).append(dirSeparator).append("main").append(dirSeparator).append("java").append(dirSeparator).append(innerDir).append(dirSeparator).append("service").append(dirSeparator).append(((String)parmsList.get(1)).toLowerCase()).append(dirSeparator).append("impl").append(dirSeparator).append((String)parmsList.get(1)).append("ServiceImpl").append(".java").toString();
                if(proLocation != null)
                    filePath = (new StringBuilder(String.valueOf(proLocation))).append(dirSeparator).append(filePath).toString();
                outFile = new File(filePath);
            } else
            if("action".equals(type))
            {
                System.out.println("======start gene action code======");
                String filePath = (new StringBuilder("src")).append(dirSeparator).append("main").append(dirSeparator).append("java").append(dirSeparator).append(innerDir).append(dirSeparator).append("web").append(dirSeparator).append(((String)parmsList.get(1)).toLowerCase()).append(dirSeparator).append(type).append(dirSeparator).append((String)parmsList.get(1)).append(postfix).append(".java").toString();
                if(proLocation != null)
                    filePath = (new StringBuilder(String.valueOf(proLocation))).append(dirSeparator).append(filePath).toString();
                outFile = new File(filePath);
            } else
            if("dao".equals(type))
            {
                System.out.println("======start gene dao code======");
                String filePath = (new StringBuilder("src")).append(dirSeparator).append("main").append(dirSeparator).append("java").append(dirSeparator).append(innerDir).append(dirSeparator).append(type).append(dirSeparator).append(((String)parmsList.get(1)).toLowerCase()).append(dirSeparator).append((String)parmsList.get(2)).append(postfix).append(".java").toString();
                if(proLocation != null)
                    filePath = (new StringBuilder(String.valueOf(proLocation))).append(dirSeparator).append(filePath).toString();
                outFile = new File(filePath);
            } else
            {
                if("dto".equals(type))
                    System.out.println("======start gene dto code======");
                else
                if("blh".equals(type))
                    System.out.println("======start gene blh code======");
                else
                if("service".equals(type))
                    System.out.println("======start gene service interface code======");
                String filePath = (new StringBuilder("src")).append(dirSeparator).append("main").append(dirSeparator).append("java").append(dirSeparator).append(innerDir).append(dirSeparator).append(type).append(dirSeparator).append(((String)parmsList.get(1)).toLowerCase()).append(dirSeparator).append((String)parmsList.get(1)).append(postfix).append(".java").toString();
                if(proLocation != null)
                    filePath = (new StringBuilder(String.valueOf(proLocation))).append(dirSeparator).append(filePath).toString();
                outFile = new File(filePath);
            }
            try
            {
                if(outFile.exists() || outFile.createNewFile())
                    output = new BufferedWriter(new FileWriter(outFile));
            }
            catch(IOException ex)
            {
                outFile.getParentFile().mkdirs();
                output = new BufferedWriter(new FileWriter(outFile));
            }
            while((tempString = reader.readLine()) != null) 
            {
                String replaceString = tempString.replace("{$packageName}", (CharSequence)parmsList.get(0)).replace("{$businessName}", (CharSequence)parmsList.get(1)).replace("{$businessName_lower}", ((String)parmsList.get(1)).toLowerCase()).replace("{$businessName_1lower}", to1Lower((String)parmsList.get(1))).replace("{$entityName}", (CharSequence)parmsList.get(2)).replace("{$entityName_1lower}", to1Lower((String)parmsList.get(2)));
                output.write(replaceString);
                output.newLine();
            }
            reader.close();
            output.close();
            if("dto".equals(type))
                System.out.println("======end gene dto code======");
            else
            if("blh".equals(type))
                System.out.println("======end gene blh code======");
            else
            if("service".equals(type))
                System.out.println("======end gene service interface code======");
            else
            if("action".equals(type))
                System.out.println("======end gene action code======");
            else
            if("serviceimpl".equals(type))
                System.out.println("======end gene service implements code======");
            else
            if("dao".equals(type))
                System.out.println("======end gene dao code======");
            break MISSING_BLOCK_LABEL_1174;
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        if(reader != null)
            try
            {
                reader.close();
            }
            catch(IOException ioexception) { }
        if(output != null)
            try
            {
                output.close();
            }
            catch(IOException ioexception1) { }
        break MISSING_BLOCK_LABEL_1204;
        Exception exception;
        exception;
        if(reader != null)
            try
            {
                reader.close();
            }
            catch(IOException ioexception2) { }
        if(output != null)
            try
            {
                output.close();
            }
            catch(IOException ioexception3) { }
        throw exception;
        if(reader != null)
            try
            {
                reader.close();
            }
            catch(IOException ioexception4) { }
        if(output != null)
            try
            {
                output.close();
            }
            catch(IOException ioexception5) { }
    }

    public static void createCode(String packageName, String businessName, String entityName)
    {
        ArrayList parmsList = new ArrayList();
        parmsList.add(packageName);
        parmsList.add(businessName);
        parmsList.add(entityName);
        String as[];
        int j = (as = (new String[] {
            "action", "blh", "serviceimpl", "service", "dao", "dto"
        })).length;
        for(int i = 0; i < j; i++)
        {
            String type = as[i];
            readTemplateFile2Code(parmsList, type, null);
        }

    }

    public static void createCode(String packageName, String businessName, String entityName, String proLocation)
    {
        ArrayList parmsList = new ArrayList();
        parmsList.add(packageName);
        parmsList.add(businessName);
        parmsList.add(entityName);
        String as[];
        int j = (as = (new String[] {
            "action", "blh", "serviceimpl", "service", "dao", "dto"
        })).length;
        for(int i = 0; i < j; i++)
        {
            String type = as[i];
            readTemplateFile2Code(parmsList, type, proLocation);
        }

    }

    public static void main(String args[])
    {
        createCode("com.dhcc.ehr", "UserManager", "User");
    }

    private static String ACTION_TEMPLATE = "WebActionTemplate.dhcctemplate";
    private static String BLH_TEMPLATE = "BlhTemplate.dhcctemplate";
    private static String SERVICE_TEMPLATE = "ServiceTemplate.dhcctemplate";
    private static String DAO_TEMPLATE = "DaoTemplate.dhcctemplate";
    private static String DTO_TEMPLATE = "DtoTemplate.dhcctemplate";
    private static String SERVICEIMPL_TEMPLATE = "ServiceImplTemplate.dhcctemplate";

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
系统地讲解了如何构建一个日常生产环境实用的基于Spring Boot并且集成springmvc + shiro + mybatis-plus + beetl的后台管理系统,可管理代码生成模版,管理连接生成代码的数据库. Guns框架自带的功能:1.用户管理 2.角色管理 3.部门管理 4.菜单管理 5.字典管理 6.业务日志 7.登录日志 8.监控管理 9.通知管理 10.代码生成 Guns特点: 1. 基于SpringBoot,简化了大量项目配置和maven依赖,让您更专注于业务开发,独特的分包方式,代码多而不乱。 2. 完善的日志记录体系,可记录登录日志,业务操作日志(可记录操作前和操作后的数据),异常日志到数据库,通过@BussinessLog注解和LogObjectHolder.me().set()方法,业务操作日志可具体记录哪个用户,执行了哪些业务,修改了哪些数据,并且日志记录为异步执行,详情请见@BussinessLog注解和LogObjectHolder,LogManager,LogAop类。 3. 利用beetl模板引擎对前台页面进行封装和拆分,使臃肿的html代码变得简洁,更加易维护。 4. 对常用js插件进行二次封装,使js代码变得简洁,更加易维护,具体请见webapp/static/js/common文件夹内js代码。 5. 利用ehcache框架对经常调用的查询进行缓存,提升运行速度,具体请见ConstantFactory类中@Cacheable标记的方法。 6. controller层采用map + warpper方式的返回结果,返回给前端更为灵活的数据,具体参见com.stylefeng.guns.modular.system.warpper包中具体类。 7. 防止XSS攻击,通过XssFilter类对所有的输入的非法字符串进行过滤以及替换。 8. 简单可用的代码生成体系,通过SimpleTemplateEngine可生成带有主页跳转和增删改查的通用控制器、html页面以及相关的js,还可以生成Service和Dao,并且这些生成项都为可选的,通过ContextConfig下的一些列xxxSwitch开关,可灵活控制生成模板代码,让您把时间放在真正的业务上。 9. 控制器层统一的异常拦截机制,利用@ControllerAdvice统一对异常拦截,具体见com.stylefeng.guns.core.aop.GlobalExceptionHandler类。 10. 页面统一的js key-value单例模式写法,每个页面生成一个唯一的全局变量,提高js的利用效率,并且有效防止多个人员开发引起的函数名/类名冲突,并且可以更好地去维护代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值