• 通过反射生成类的utils


    package com.gu.utils;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Writer;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    import som.gu.core.DBManager;
    import som.gu.core.MysqlTypeConvertor;
    import som.gu.core.tableContext;
    import som.gu.core.typeConvetor;
    
    import com.gu.bean.JavaFieldGetSet;
    import com.gu.bean.columnInfo;
    import com.gu.bean.tableInfo;
    
    /**
     * 生成类源码常用的操作
     * @author 谷
     *
     */
    public class javaFieldUtils {
    
        /**
         * 生成类某属性和get,set方法
         * @param column
         * @param converter
         * @return
         */
        public static JavaFieldGetSet createjavaFieldSetGetSRC(columnInfo column,typeConvetor converter){
            JavaFieldGetSet jfgs=new JavaFieldGetSet();
            String javaFieldType=converter.databassType2javatype(column.getDateType());
            jfgs.setFieldInfo("	private "+javaFieldType+" "+column.getName()+";
    ");
            //get方法
            StringBuilder str=new StringBuilder();
            str.append("	public "+javaFieldType+" Get"+StringUtils.firstChar2uppercase(column.getName())+"(){
    ");
            str.append("		return "+column.getName()+";
    	}
    ");
            jfgs.setGetInfo(str.toString());
            //set方法
            StringBuilder set=new StringBuilder();
            set.append("	public "+" void Set"+StringUtils.firstChar2uppercase(column.getName())+"("+javaFieldType+" "+column.getName()+"){
    ");
            set.append("		this."+column.getName()+"="+column.getName()+";
    	}
    ");
            jfgs.setSetInfo(set.toString());
            
            return jfgs;
        }
        /**
         * 生成某个类中所有信息
         * @param table
         * @param conver
         * @return
         */
        public static String JavaSrc(tableInfo table,typeConvetor conver){
            StringBuilder src=new StringBuilder();
             Map<String, columnInfo> columns=table.getColumns();
             List<JavaFieldGetSet>list=new ArrayList<>();//所有属性列表
             
             
             for(columnInfo column:columns.values()){
                 list.add(createjavaFieldSetGetSRC(column, conver));
             }
             //生成包名;
             src.append("package "+DBManager.getPoPackage()+";
    ");
             //生成类名
             src.append("public class "+StringUtils.firstChar2uppercase(table.gettName())+" {
    ");
             //生成属性
             for(JavaFieldGetSet f:list){
                 src.append(f.getFieldInfo());
             }
             src.append("
    
    ");
             //生成get,set方法
             for(JavaFieldGetSet f:list){
                 src.append(f.getGetInfo());
             }
             for(JavaFieldGetSet f:list){
                 src.append(f.getSetInfo());
             }
             src.append("}");
             return src.toString();
        }
        /**
         * 生成表对应的java文件
         * @param table
         * @param conver
         */
        public static void creatJavaPoFile(tableInfo table,typeConvetor conver){
            String po=JavaSrc(table,conver);
            String srcPath=DBManager.getSrcPath().replaceAll("\\", "/");;
            String PoPackagePath=DBManager.getPoPackage().replaceAll("\.","/");
            //String filePath=srcPath+"/"+PoPackagePath+"/"+table.gettName()+".java";
            
            File f=new File(srcPath+"/"+PoPackagePath);
            
            if(!f.exists()){
                f.mkdirs();
            }
        //    System.out.print(f.getAbsolutePath());
            BufferedWriter writer=null;
            try {
             writer=new BufferedWriter(new FileWriter(f.getAbsolutePath()+"/"+StringUtils.firstChar2uppercase(table.gettName())+".java"));
             writer.write(po);
             writer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        } 
            }
    
    }
  • 相关阅读:
    11.2.0.3 实例启动现在提供Large Pages Information大内存页信息了
    RAC中增大db_cache_size引发的ORA04031错误
    11.2 中Oracle Cluster Registry(OCR)可选的存储设备
    所见即所得
    新的开始
    关于asp.net(C#)与C#.net之间的区别
    Asp.Net学习之母板
    Javascript闭包(Closure)初步接触
    JavaScript解析JSON及XML
    JavaScript之JSON详解
  • 原文地址:https://www.cnblogs.com/helloMyworld0001/p/5972939.html
Copyright © 2020-2023  润新知