数据库中字段和entity对象字段进行映射,生成resultMap工具类
1 package com.mybatis.util; 2 3 import com.mopom.transaction.module.entity.HireVoucher; 4 5 import java.lang.reflect.Field; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 public class MybatisUtil { 10 // 获取bean的属性 根据属性评价 resultMap 11 // 并将驼峰修改为'_' 12 public static String getResultMap(Class<?> cls) throws Exception { 13 String str = ""; 14 // 头部 <resultMap id="BaseResultMap" type="com.huajie.entity.sys.SysMenuinfo"> 15 str = "<resultMap id=" + cls.getSimpleName() + "ResultMap type=" + cls.getName() + "> \r\n"; 16 str = str + "<id column= \"\"" + " property= \"\"" + " />" + " \r\n"; 17 // 每一行字符串 18 String linestr = ""; 19 Field[] declaredFields = cls.getDeclaredFields(); 20 for (Field field : declaredFields) { 21 linestr = "<result column=\"" + getUpCaseReplace(field.getName()) + "\" property=\"" + field.getName() 22 + " />"; 23 linestr += "\r\n"; 24 str += linestr; 25 } 26 str+="</resultMap>"; 27 return str; 28 } 29 30 public static String getAllField(Class<?> cls) throws Exception { 31 String str = ""; 32 // // 头部 <resultMap id="BaseResultMap" type="com.huajie.entity.sys.SysMenuinfo"> 33 // str = "<resultMap id=" + cls.getSimpleName() + "ResultMap type=" + cls.getName() + "> \r\n"; 34 // str = str + "<id column= \"\"" + " property= \"\"" + " />" + " \r\n"; 35 // 每一行字符串 36 String linestr = ""; 37 Field[] declaredFields = cls.getDeclaredFields(); 38 int i = 0; 39 for (Field field : declaredFields) { 40 linestr = getUpCaseReplace(field.getName()) + ", "; 41 if (i % 4 == 0 && i != 0){ 42 linestr += "\r\n"; 43 } 44 str += linestr; 45 i++; 46 } 47 return str.substring(0,str.length()-2); 48 } 49 50 /** 51 * 将字符串中的驼峰写法替换成'_' 52 * 53 * @param str 54 * @return 55 */ 56 private static String getUpCaseReplace(String str) { 57 List<String> listChar = getUpCaseList(str); 58 //先将字原属性加上"_"和原属性的首字母大写 59 for (int i = 0; i < listChar.size(); i++) { 60 str = str.replace(listChar.get(i), "_" + listChar.get(i).toUpperCase()); 61 } 62 //将字段全部装换为大写 63 String result = getAllUpCaseList(str); 64 return result; 65 } 66 67 private static String getAllUpCaseList(String str) { 68 // 转为char数组 69 char[] ch = str.toCharArray(); 70 // 得到大写字母 71 StringBuffer stringBuffer = new StringBuffer(); 72 for (int i = 0; i < ch.length; i++) { 73 stringBuffer.append(String.valueOf(ch[i]).toUpperCase()); 74 } 75 return stringBuffer.toString(); 76 } 77 /** 78 * @Description: 输出字符串中的大写字母 79 * @param str 80 */ 81 private static List<String> getUpCaseList(String str) { 82 List<String> listChar = new ArrayList<String>(); 83 // 转为char数组 84 char[] ch = str.toCharArray(); 85 // 得到大写字母 86 for (int i = 0; i < ch.length; i++) { 87 if (ch[i] >= 'A' && ch[i] <= 'Z') { 88 listChar.add(String.valueOf(ch[i])); 89 } 90 } 91 return listChar; 92 } 93 94 public static void main(String[] args) throws Exception { 95 //TODO 需要生成的属性 96 HireVoucher a = new HireVoucher(); 97 System.out.println(getResultMap(a.getClass())); 98 //所有属性 99 System.out.println("------------------------------------------------------------------------"); 100 System.out.println(getAllField(a.getClass())); 101 } 102 103 }