package com.cnpc.epai.productionmanager.utils;
import com.baomidou.mybatisplus.annotation.TableField;
import com.cnpc.epai.productionmanager.pojo.Menu;
import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 生成resultMap
* @author sunyujie
*/
public class ResUltMapUtil {
public static void production(Class c){
Field[] declaredFields = c.getDeclaredFields();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("<resultMap id=\"");
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("<id column=\"");
} else if ("serialVersionUID".equals(declaredField.getName())) {
continue;
} else {
stringBuffer.append("<result column=\"");
}
stringBuffer.append(humpToLine(declaredField.getName()));
stringBuffer.append("\" property=\"");
stringBuffer.append(declaredField.getName());
stringBuffer.append("\"/>\n");
}
stringBuffer.append("</resultMap>");
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(Menu.class);
}
}
ResUltMapUtil
最新推荐文章于 2024-09-08 13:50:54 发布