mybatis 根据实体类自动生成resultMap工具类 拿来即用!

2023-12-19 20:50:05

作为java后端程序员,每日都免不了接触一些重复且繁琐的代码工作,下面是一个平时自用断电根据实体类自动生成resultMap工具类,现把代码贴出来供大家参考使用:

package com.koron.imj.utils;

import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ResultMapUtil {
    private  Pattern humpPattern = Pattern.compile("[A-Z]");
    List<String> ListObject = new LinkedList<>();
    List<String> stringList = new LinkedList<>();
    List<String> Lists = new LinkedList<>();
    
    public void getResultMap(Class<?> clazz) throws ClassNotFoundException {
        try{
            getresultMap(clazz,0);
        }catch (Exception e){
            e.printStackTrace();
            return ;
        }
    }

    private void getresultMap(Class<?> clazz, int i) throws ClassNotFoundException {
        getresultMap(clazz,0,null,"");
    }

    private void getresultMap (Class<?> clazz,int num,String strd,String fieldName) throws ClassNotFoundException {

        Field[] fields = clazz.getDeclaredFields();
        if (num == 0){
            System.out.println("<resultMap id=\""+ "set"+ clazz.getName() +"\" type=\""+ clazz.getName()+"\">");
        }else {
            for (int i = 0 ;i < num+2;i++){
                System.out.print(" ");
            }
            System.out.println("<collection property=\""+ fieldName + "\" javaType=\""+ strd +"\" ofType=\""+ clazz.getName()+"\" >");
        }
        for(Field field : fields){
            String javaType = field.getType().getName();
            if (!javaType.equals(javaType2jdbcType(javaType))){
                for (int i = 0 ;i < num+4;i++){
                    System.out.print(" ");
                }
                System.out.println("<result property=\""+field.getName()+"\" column=\""+property2Column(field.getName())+"\"></result>");
            }else {
                javaType = String.valueOf(field.getGenericType());
                Lists.add(field.getName());
                if(javaType(javaType).equals("list")){
                    javaType = javaType.substring(javaType.indexOf("<")+1,javaType.indexOf(">"));
                    if(!javaType.equals(javaType2jdbcType(javaType))) {
                        for (int i = 0 ;i < num+4;i++){
                            System.out.print(" ");
                        }
                        System.out.println("<collection property=\""+field.getType().getName()+"\" ofType=\""+javaType+"\" >");
                        for (int i = 0 ;i < num+6;i++){
                            System.out.print(" ");
                        }
                        System.out.println(" <result column=\"" +field.getName() +"\"/>");
                        for (int i = 0 ;i < num+4;i++){
                            System.out.print(" ");
                        }
                        System.out.println("</collection>");
                        continue;
                    }
                    stringList.add("list");

                }else {
                    stringList.add(javaType(javaType));
                    javaType = field.getType().getName();
                }
                ListObject.add(javaType);
            }
        }
        ResultMapUtil resultMapUtil = new ResultMapUtil();
        for (int i = 0;i <  ListObject.size();i++){
            resultMapUtil.getresultMap(Class.forName(ListObject.get(i)),num+2,stringList.get(i),Lists.get(i));
        }
        if (num == 0){
            System.out.println("</resultMap>");
        }else {
            for (int i = 0 ;i < num+2;i++){
                System.out.print(" ");
            }
            System.out.println("</collection>");
        }
    }

    private  String property2Column(String property){
        Matcher matcher = humpPattern.matcher(property);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()){
            matcher.appendReplacement(sb, "_"+matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    private  String javaType2jdbcType(String Type){
        String javaType = Type.toLowerCase();
        if(javaType.contains("string")){
            return "VARCHAR";
        }else if(javaType.contains("boolean")){
            return "BIT";
        }else if(javaType.contains("byte")){
            return "TINYINT";
        }else if(javaType.contains("short")){
            return "SMALLINT";
        }else if(javaType.contains("int")){
            return "INTEGER";
        }else if(javaType.contains("long")){
            return "BIGINT";
        }else if(javaType.contains("double")){
            return "DOUBLE";
        }else if(javaType.contains("float")){
            return "REAL";
        }else if(javaType.contains("date")){
            return "DATE";
        }else if(javaType.contains("timestamp")){
            return "TIMESTAMP";
        }else if(javaType.contains("time")){
            return "TIME";
        }else if(javaType.contains("bigdecimal")){
            return "DECIMAL";
        }else {
            return Type;
        }
    }

    private  String javaType(String Type){
        String javaType = Type.toLowerCase();
        if(javaType.contains("list")){
            return "list";
        }else {
            return "Object";
        }
    }
}

直接在引用上面的工具类即可生成

public class MainApplication {
    public static void main(String[] args) throws ClassNotFoundException {
        ResultMapUtil resultMapUtil = new ResultMapUtil();
        resultMapUtil.getResultMap(JobTransVirAndStatusDto.class);
    }
}

以下是控制台输出结果:

<resultMap id="setcom.koron.imj.job.entity.dto.JobTransVirAndStatusDto" type="com.koron.imj.job.entity.dto.JobTransVirAndStatusDto">
    <result property="transId" column="trans_id"></result>
    <result property="sendJobId" column="send_job_id"></result>
    <result property="recvJobId" column="recv_job_id"></result>
    <result property="status" column="status"></result>
    <result property="remarks" column="remarks"></result>
    <result property="confirmBy" column="confirm_by"></result>
    <result property="transDt" column="trans_dt"></result>
    <result property="lstUpdDt" column="lst_upd_dt"></result>
    <result property="statusDesc" column="status_desc"></result>
    <result property="sendJobNo" column="send_job_no"></result>
    <result property="recvJobNo" column="recv_job_no"></result>
    <result property="engineer" column="engineer"></result>
    <result property="inspector" column="inspector"></result>
    <result property="worksup" column="worksup"></result>
    <result property="sendActivated" column="send_activated"></result>
    <result property="recvActivated" column="recv_activated"></result>
    <result property="recvJobType" column="recv_job_type"></result>
    <result property="recoverFlag" column="recover_flag"></result>
</resultMap>

Process finished with exit code 0

文章来源:https://blog.csdn.net/weixin_45886609/article/details/135091178
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。