mybatis resultmap自动生成工具类

package com.css.sscservice.modules.mutualservice.util;

import com.css.sscservice.modules.sync.project.entity.ProcurementMeetEntity;
import com.baomidou.mybatisplus.annotation.TableField;

import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ResultMapUtil {

    public static void production(Class c){
        Field[] declaredFields = c.getDeclaredFields();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(");
        stringBuffer.append(c.getSimpleName());
        stringBuffer.append("\" type=\"");
        stringBuffer.append(c.getCanonicalName());
        stringBuffer.append("\">\n");
        for (Field declaredField : declaredFields) {
            if (declaredField.isAnnotationPresent(TableField.class)){
                TableField annotation = declaredField.getAnnotation(TableField.class);
                if (!annotation.exist()){
                    continue;
                }
            }
            if ("id".equals(declaredField.getName())){
                stringBuffer.append(");
            } else if ("serialVersionUID".equals(declaredField.getName())) {
                continue;
            } else {
                stringBuffer.append(");
            }

            stringBuffer.append(humpToLine(declaredField.getName()));
            stringBuffer.append("\" property=\"");
            stringBuffer.append(declaredField.getName());
            stringBuffer.append("\"/>\n");
        }
        stringBuffer.append("");
        System.out.println(stringBuffer);
    }


    public static String humpToLine(String str) {
        Matcher matcher = Pattern.compile("[A-Z]").matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static void main(String[] args) {
        production(ProcurementMeetEntity.class);
    }


}

生成结果:
mybatis resultmap自动生成工具类_第1张图片

你可能感兴趣的:(mybatis)