实体类生成resultMap工具类

将实体类转为resultMap

实体类:

import lombok.Data;

import java.sql.Timestamp;
import java.util.Date;

@Data
public class TestVo {
    private String metric;
    private int value;
    private Timestamp timeStamp;
    private String valueType;
    private String name;
    private String hostTag;
    private Date createDate;
    private Date insertTime;
}

工具类:

import com.xxx.xx.TestVo;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 获取resultMap的工具类
 */
class Test {
    public Test() {
    }

    // 获取bean的属性 根据属性评价 resultMap
    public static String getResultMap(Class<?> cls) throws Exception {
        String str = "";
        // 每一行字符串 <result column="BID_SECTION_CODE" property="BID_SECTION_CODE"
        // jdbcType="VARCHAR" />
        String linestr = "";
        Field[] declaredFields = cls.getDeclaredFields();
        for (Field field : declaredFields) {
            if (field.getType().getName().equals("java.lang.String")) {
                linestr = "<result column=\"" + field.getName() + "\" property=\"" + field.getName()
                        + "\" jdbcType=\"VARCHAR\" />";
            } else {
                linestr = "<result column=\"" + field.getName() + "\" property=\"" + field.getName()
                        + "\" jdbcType=\"INTEGER\" />";
            }
            System.out.println(linestr);
        }

        return str;
    }

    // 获取bean的属性 根据属性评价 resultMap
    // 并将驼峰修改为'_'
    public static String getResultMapNew(Class<?> cls) throws Exception {
        String str = "";
        // 头部 <resultMap id="BaseResultMap" type="com.huajie.entity.sys.SysMenuinfo">
        str = "<resultMap id=\"" + cls.getSimpleName() + "ResultMap\" type=\"" + cls.getName() + "\"> \r\n";
        // 每一行字符串
        String linestr = "";
        Field[] declaredFields = cls.getDeclaredFields();
        for (Field field : declaredFields) {
            if (field.getType().getName().equals("java.lang.String")) {
                linestr = "<result column=\"" + getUpCaseReplace(field.getName()) + "\" property=\"" + field.getName()
                        + "\" jdbcType=\"VARCHAR\" />";
            } else {
                linestr = "<result column=\"" + getUpCaseReplace(field.getName()) + "\" property=\"" + field.getName()
                        + "\" jdbcType=\"INTEGER\" />";
            }
            linestr += "\r\n";
            str += linestr;
        }
        str += "</resultMap>";
        return str;
    }

    // 获取Base_Column_List sql语句字段
    public static String getColumnList(Class<?> cls) throws Exception {
        // 每一行字符串 <result column="BID_SECTION_CODE" property="BID_SECTION_CODE"
        // jdbcType="VARCHAR" />
        String linestr = "";
        Field[] declaredFields = cls.getDeclaredFields();
        for (Field field : declaredFields) {
            linestr = linestr + field.getName() + ",";
        }
        String str = linestr.substring(0, linestr.length() - 1);
        System.out.println(str);
        return str;
    }

    /**
     * 将字符串中的驼峰写法替换成'_'
     *
     * @param str
     * @return
     */
    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).toLowerCase());
        }
        return str;
    }

    /**
     * @param str
     * @Description: 输出字符串中的大写字母
     */
    private static List<String> getUpCaseList(String str) {
        List<String> listChar = new ArrayList<String>();
        // 转为char数组
        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;
    }

    /**
     * @param
     * @Description: 输出字符串中的大写字母
     */
    private static String getColumnListNew(Class<?> cls) throws Exception {
        // 每一行字符串 <result column="BID_SECTION_CODE" property="BID_SECTION_CODE"
        // jdbcType="VARCHAR" />
        String linestr = "";
        Field[] declaredFields = cls.getDeclaredFields();
        for (Field field : declaredFields) {
            linestr = linestr + getUpCaseReplace(field.getName()) + ",";
        }
        String str = linestr.substring(0, linestr.length() - 1);
        System.out.println(str);
        return str;
    }

    public static void main(String[] args) throws Exception {
        TestVo a = new TestVo();
        System.out.println(getResultMapNew(a.getClass()));
    }
}

输出结果:

<resultMap id="TestVoResultMap" type="com.xxx.xx.TestVo"> 
<result column="metric" property="metric" jdbcType="VARCHAR" />
<result column="value" property="value" jdbcType="INTEGER" />
<result column="time_stamp" property="timeStamp" jdbcType="INTEGER" />
<result column="value_type" property="valueType" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="host_tag" property="hostTag" jdbcType="VARCHAR" />
<result column="create_date" property="createDate" jdbcType="INTEGER" />
<result column="insert_time" property="insertTime" jdbcType="INTEGER" />
</resultMap>

 

posted @ 2023-12-08 17:53  IIIID  阅读(154)  评论(0编辑  收藏  举报