首页 > 其他分享 >实体类生成resultMap工具类

实体类生成resultMap工具类

时间:2023-12-08 17:55:07浏览次数:24  
标签:实体类 String resultMap 生成 Field private str linestr cls

将实体类转为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>

 

标签:实体类,String,resultMap,生成,Field,private,str,linestr,cls
From: https://www.cnblogs.com/Oxyy/p/17888746.html

相关文章

  • 转载:ReportLab生成带表格和图文的PDF
    转载来自于:https://zhuanlan.zhihu.com/p/456486769龙在天涯 项目环境:环境:AnacondaPython3.10编辑器:PyCharm2021.2.3Packages:Reportlab3.6.2ReportLab简介“ThisisasoftwarelibrarythatletsyoudirectlycreatedocumentsinAdobe'sPortableDocumentForma......
  • Springboot+FastJson实现解析第三方http接口json数据为实体类(时间格式化转换、字段包
    场景若依前后端分离版手把手教你本地搭建环境并运行项目:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108465662在上面搭建SpringBoot项目的基础上,并且在项目中引入fastjson、hutool、lombok等所需依赖后。系统需要对接第三方http接口获取返回的数据,并将json数......
  • 【scikit-learn基础】--『数据加载』之样本生成器
    除了内置的数据集,scikit-learn还提供了随机样本的生成器。通过这些生成器函数,可以生成具有特定特性和分布的随机数据集,以帮助进行机器学习算法的研究、测试和比较。目前,scikit-learn库(v1.3.0版)中有20个不同的生成样本的函数。本篇重点介绍其中几个具有代表性的函数。1.分类聚类......
  • 代码中公用生成使用token方法
    生成使用token引入pom<dependency>     <groupId>com.auth0</groupId>     <artifactId>java-jwt</artifactId>     <version>3.14.0</version>   </dependency>添加工具类packagecom.mashibing.interin......
  • window 使用cmd命令生成项目的目录树
    window使用tree命令生成目录树,只有/F和/A命令,并不满足我们需要过滤不必要文件和排序等等需求,所以我使用了一个插件tree-node-cli。 在cmd窗口安装tree-node-cli插件npminstall-gtree-node-cli 插件安装成功后在cmd窗口执行命令,执行命令前使用cd命令切到项目文件夹......
  • 支持生成接口文档!Apipost IDEA插件使用体验
    前言Idea是一款功能强大的集成开发环境(IDE),它可以帮助开发人员更加高效地编写、调试和部署软件应用程序,Idea还具有许多插件和扩展,可以根据开发人员的需要进行定制和扩展,从而提高开发效率,今天我们就来介绍一款国产的API调试插件:Apipost-Helper什么是Apipost-Helper?Apipost-He......
  • 一张图生成一个视频大模型公开
    一张图生成一个视频大模型公开AnimateAnyone:ConsistentandControllableImage-to-VideoSynthesisforCharacterAnimation为任何人制作动画:用于角色动画的一致且可控的图像到视频合成;论文地址:https://arxiv.org/pdf/2311.17117.pdf项目地址:https://github.com/HumanAIG......
  • captcha模块——生成图片验证码
    安装captcha库pipinstallcaptcha 基本使用方法(生成图片验证码)importcaptchafromcaptcha.imageimportImageCaptcha#设置图片宽高image=ImageCaptcha(width=200,height=100)#验证码内容captcha_text='1234'#验证码写入图片captcha_image=image.g......
  • [转]python 随机生成一个请求User-Agent
    前言全局说明爬虫程序的第一部分通常都是导入我们爬虫所需要的库。为了安全,我通常都是使用fake_useragent库随机生成一个请求头User-Agent。一、安装模块pip3installfake_useragent二、模块使用#导入fake_useragent库中的UserAgent类fromfake_useragentimportUser......
  • 架构师的知行合一(内容由AI的全文生成,满分100分我打99分)
    大型架构是怎么来的随着科技的不断发展,越来越多的企业和组织开始意识到数字化转型的重要性。为了更好地适应市场的变化,满足客户的需求,提高企业的竞争力,大型架构成为了企业和组织不可或缺的一部分。那么,大型架构到底是怎么来的呢?本文将为您深入剖析。一、业务需求推动架构演进......