Java代码生成器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenpengjia006/article/details/54575914

Java代码生成器

发现编写中的代码很多都是结构化的东西,大体上完全相同,既然这样,为什么不写一个属于自己的代码生成器呢?省时省力还不易出错。

此项目使用FreeMaker作为处理器,废话不多说,先看下结构然后上关键源码。

项目结构

1.注解代码

@Target({ ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ClassName {

    String value() default "";
}

@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ColunmName {

    String value() default "";

}

@Target({ ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PrimaryKey {

}

@Target({ ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TableName {
    String value() default "";
}

2.利用反射处理注解并生成对应文件

public static void createCurdFile(Class<?> clazz,String classPackageName,String ftlPackageName){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("SimpleName", clazz.getSimpleName());
        map.put("packageName", classPackageName);
        map.put("Name", StringUtils.getFirstLetterLower(clazz.getSimpleName()));
        Field[] fields = clazz.getDeclaredFields();
        List<MyField> fieldList = new ArrayList<MyField>();
        List<MyField> colunList = new ArrayList<MyField>();
        int primaryKeyNum = 0;
        for (Field field : fields) {
            MyField myField = new MyField();
            myField.setType(field.getType().getSimpleName());
            myField.setName(field.getName());
            myField.setUpperName(StringUtils.getFirstLetterUpper(field.getName()));
            if(field.isAnnotationPresent(ColunmName.class)){//处理列名注解
                ColunmName colunmName = (ColunmName) field.getAnnotation(ColunmName.class);
                myField.setColunmName(colunmName.value());

                if(field.isAnnotationPresent(PrimaryKey.class)){//处理主键注解
                    myField.setPrimarykey(true);
                    primaryKeyNum ++;
                }

                colunList.add(myField);
            }
            fieldList.add(myField);
        }
        map.put("colunList", colunList);
        map.put("fieldsList", fieldList);
        /** 中文名称 注释用 **/
        if(clazz.isAnnotationPresent(ClassName.class)){
            ClassName name = (ClassName)clazz.getAnnotation(ClassName.class);
            map.put("className", name.value());
        }else{
            map.put("className", clazz.getSimpleName());
        }
        /** 关联表名 **/
        if(clazz.isAnnotationPresent(TableName.class)){
            TableName tableName = (TableName)clazz.getAnnotation(TableName.class);
            map.put("tableName", tableName.value());
        }else{
            log.error("error : can not find the tableName for "+clazz.getName()+",please add tablename by yourself in DaoSql file.");
            map.put("tableName", "");
        }

        if(primaryKeyNum>1){
            log.error("error : can not create DaoSql file for "+clazz.getName()+",because this class has more than one primaryKey.");
            return;
        }
        if(primaryKeyNum==0){
            log.error("error : can not create DaoSql file for "+clazz.getName()+",because this class has no primaryKey.");
        }

        try {
            File[] list = ScanUtil.scan(ftlPackageName);
            for (File file : list) {
                if(file.isFile()){
                    String content = FileUtil.getFileContent(file,map);
                    FileUtil.WriteFile("C://Users//chen//Desktop//Common//", clazz.getSimpleName()+file.getName().replace("ftl", "java"), content);                 
                }
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (TemplateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        createJavaBean(clazz,classPackageName);
    }

附部分模版文件代码

package ${packageName}.service;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;
import ${packageName}.model.${SimpleName};

public interface ${SimpleName}Manager {


    /**
     * 根据id查询${className}
     * 
     * @param ${Name}Id
     * @return
     */
    public ${SimpleName} get${SimpleName}ById(String ${Name}Id);


    /**
     * 保存${className}
     * @param ${Name}
     * @return
     */
    public int insert${SimpleName}(${SimpleName} ${Name});

    /**
     * 更新${className}
     * @param ${Name}
     * @return
     */
    public int update${SimpleName}(${SimpleName} ${Name});


    /**
     * 根据条件查询${className}
     * @param Map<String, String>
     * @return List<${SimpleName}>
     */
    public  List<${SimpleName}> get${SimpleName}s(Map<String, String> searchMap);

    /**
     * 根据条件查询${className}
     * @param Map<String, String>
     * @return JSONObject
     */
    public  JSONObject get${SimpleName}LstData(Map<String, String> searchMap);
}

github
百度云

猜你喜欢

转载自blog.csdn.net/chenpengjia006/article/details/54575914