生成xml版属性映射关系
/**
* 1.用于获取结果集的映射关系
*/
public static String getResultsStr(Class origin) {
StringBuilder stringBuilder = new StringBuilder();
for (Field field : origin.getDeclaredFields()) {
if(field.getAnnotation(Transient.class) != null){//有@Transient注解的忽略
continue;
}
String property = field.getName();
//映射关系:对象属性(驼峰)->数据库字段(下划线)
// String column = new PropertyNamingStrategy.SnakeCaseStrategy().translate(field.getName()).toUpperCase();//数据库自字段大写
String column = new PropertyNamingStrategy.SnakeCaseStrategy().translate(field.getName());
stringBuilder.append(String.format("<result property = \"%s\" column = \"%s\"/>\n", property, column));
}
System.out.println(stringBuilder.toString());
if(origin.getSuperclass() != null){ //父类属性递归调用
getResultsStr(origin.getSuperclass());
}
return stringBuilder.toString();
}
生成注解版属性映射关系
/**
* 1.用于获取结果集的映射关系
*/
public static String getResultsSt(Class origin) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("@Results({\n");
for (Field field : origin.getDeclaredFields()) {
if(field.getAnnotation(Transient.class) != null){//有@Transient注解的忽略
continue;
}
String property = field.getName();
//映射关系:对象属性(驼峰)->数据库字段(下划线)
String column = new PropertyNamingStrategy.SnakeCaseStrategy().translate(field.getName()).toUpperCase();
stringBuilder.append(String.format("@Result(property = \"%s\", column = \"%s\"),\n", property, column));
}
stringBuilder.append("})");
return stringBuilder.toString();
}