1,工具类代码
package com.icat.datasourcebuild.mybatis;
import com.icat.datasourcebuild.business.blacklist.model.AdminUser;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class MybatisUtilVersion01 {
public static String getResultMap(Class<?> cls) throws Exception {
String str = "";
str = "<resultMap id=\"" + cls.getSimpleName() + "ResultMap\" type=\"" + cls.getName() + "\"> \r\n";
str = str + "<id column= \"\"" + " property= \"\"" + " />" + " \r\n";
String linestr = "";
Field[] declaredFields = cls.getDeclaredFields();
for (Field field : declaredFields) {
linestr = "<result column=\"" + getUpCaseReplace(field.getName()) + "\" property=\"" + field.getName()
+ "\" />";
linestr += "\r\n";
str += linestr;
}
str+="</resultMap>";
return str;
}
public static String getAllField(Class<?> cls) throws Exception {
String str = "";
String linestr = "";
Field[] declaredFields = cls.getDeclaredFields();
int i = 0;
for (Field field : declaredFields) {
linestr = getUpCaseReplace(field.getName()) + ", ";
if (i % 4 == 0 && i != 0){
linestr += "\r\n";
}
str += linestr;
i++;
}
return str.substring(0,str.length()-2);
}
private static String getUpCaseReplace(String str) {
List<String> listChar = getUpCaseList(str);
for (int i = 0; i < listChar.size(); i++) {
str = str.replace(listChar.get(i), "_" + listChar.get(i).toUpperCase());
}
String result = getAllUpCaseList(str);
return result;
}
private static String getAllUpCaseList(String str) {
char[] ch = str.toCharArray();
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < ch.length; i++) {
stringBuffer.append(String.valueOf(ch[i]).toUpperCase());
}
return stringBuffer.toString();
}
private static List<String> getUpCaseList(String str) {
List<String> listChar = new ArrayList<String>();
char[] ch = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
if (ch[i] >= 'A' && ch[i] <= 'Z') {
listChar.add(String.valueOf(ch[i]));
}
}
return listChar;
}
public static void main(String[] args) throws Exception {
AdminUser a = new AdminUser();
System.out.println(getResultMap(a.getClass()));
System.out.println("------------------------------------------------------------------------");
System.out.println(getAllField(a.getClass()));
}
}
2,控制台打印结果:
<resultMap id="AdminUserResultMap" type="com.icat.datasourcebuild.business.blacklist.model.AdminUser">
<id column= "" property= "" />
<result column="ID" property="id" />
<result column="USERNAME" property="username" />
<result column="PASSWORD" property="password" />
<result column="NAME" property="name" />
<result column="STATUS" property="status" />
<result column="TYPE" property="type" />
<result column="DEPT_ID" property="dept_id" />
<result column="MOBILE" property="mobile" />
<result column="EMAIL" property="email" />
<result column="CREATE_TIME" property="create_time" />
<result column="LAST_LOGIN_IP" property="last_login_ip" />
<result column="LAST_LOGIN_TIME" property="last_login_time" />
</resultMap>
------------------------------------------------------------------------
ID, USERNAME, PASSWORD, NAME, STATUS,
TYPE, DEPT_ID, MOBILE, EMAIL,
CREATE_TIME, LAST_LOGIN_IP, LAST_LOGIN_TIME
3,实体类
@Data
public class AdminUser {
private Long id;
private String username;
private String password;
private String name;
private Integer status;
private Integer type;
private Long dept_id = 0L;
private String mobile;
private String email;
private Date create_time;
private String last_login_ip;
private Date last_login_time;
}