利用反射生成对应的dao参数类

package cn.com.servyou.fkpt.core.base.bean;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import cn.com.servyou.fkpt.core.fak.bean.Fxfa;

/**
 * <p>
 * 标题: BeanUtil.java
 * </p>
 * <p>
 * 描述: 根据bean快速生成dao参数的工具类
 * </p>
 * <p>
 * </p>
 * <p>
 * 创建时间: 2015-1-4
 * </p>
 * <p>
 * 作者:zhuw
 * </p>
 */
public class BeanUtil {


    /**
     * 需要生成的bean类 此处需要修改成你所要的类
     */
    public static Class cls = Fxfa.class;

    /**
     * DAOParamGenerator 的参数名 如果需要修改则修改此变量
     */
    public static String paramName = "pg";

    /**
     * 生成类型 false非批量 /true 批量 如果是批量的要修改成true
     */
    public static boolean batch = true;

    /**
     * batch为true时生效 dao的第几个sql默认为0
     */
    public static String sqlIndex = "0";

    /**
     * batch为true时生效 for循环的参数名称默认为i
     */
    public static String forParam = "i";

    public static void main(String[] args) {
        System.out.println(writeDao(cls));
    }

    /**
     * 控制台输出代码
     */
    public static String writeDao(Class cls) {
        String cname = cls.getName();
        cname = cname.substring(cname.lastIndexOf(".") + 1, cname.length());
        className = cname;
        String firstZm = String.valueOf(cname.charAt(0));
        classNameXx = cname.replaceFirst(firstZm, firstZm.toLowerCase());
        try {
            return writeMethod(writeDAOParamGenerator(writeAttr(cls))).toString();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }

    private static String className;

    private static String classNameXx;

    /**
     * 写入方法
     */
    public static StringBuilder writeMethod(StringBuilder content) {
        StringBuilder sb = new StringBuilder();
        if (batch) {
            sb.append("    public DAOParamGenerator getDaoParam(List<" + className + "> " + classNameXx + "List) {\n");
        } else {
            sb.append("    public DAOParamGenerator getDaoParam(" + className + " " + classNameXx + ") {\n");
        }
        sb.append(content);
        sb.append("    }");
        return sb;
    }

    /**
     * 写入参数
     */
    public static StringBuilder writeDAOParamGenerator(StringBuilder attr) {
        StringBuilder sb = new StringBuilder();
        sb.append("        DAOParamGenerator " + paramName + " = new DAOParamGenerator();\n");
        sb.append(attr);
        sb.append("        return " + paramName + ";\n");
        List<Fxfa> list = new ArrayList<Fxfa>();
        for (int i = 0; i < list.size(); i++) {
            Fxfa fxfa = list.get(i);
        }
        return sb;
    }

    /**
     * 写入javabean属性
     */
    public static StringBuilder writeAttr(Class cls) throws ClassNotFoundException {
        StringBuilder sb = new StringBuilder();
        Method[] methods = cls.getMethods();
        if (batch) {
            sb.append("        for (int i = 0; i < " + classNameXx + "List.size(); i++) {\n");
            sb.append("            " + className + " " + classNameXx + " = " + classNameXx + "List.get(i);\n");
            for (Method m : methods) {
                // String类型才行
                if (m.getName().indexOf("get") != -1 && m.getReturnType() == String.class) {
                    sb.append("            " + paramName + ".addBatchSqlParam(" + sqlIndex + ", " + forParam + ", "
                            + classNameXx + "."
                            + m.getName()
                            + "());\n");
                }
            }
            sb.append("        }\n");
        } else {
            for (Method m : methods) {
                // String类型才行
                if (m.getName().indexOf("get") != -1 && m.getReturnType() == String.class) {
                    sb.append("        " + paramName + ".addFirstSqlParam(" + classNameXx + "." + m.getName()
                            + "());\n");
                }
            }
        }
        return sb;
    }
}

 

你可能感兴趣的:(DAO)