工具之生成mybatis对应手册工具类

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;

/**
 * 生成mybatis对应手册工具类
 * @ClassName MybatisUtil
 * @Description TODO
 * @Author 李锦涛
 * @Date 2020/9/3 0003 10:34
 * @Version 1.0
 */
public class MybatisUtilVersion01 {
    // 获取bean的属性 根据属性评价 resultMap
    // 并将驼峰修改为'_'
    public static String getResultMap(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";
        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 = "";
//        // 头部 <resultMap id="BaseResultMap" type="com.huajie.entity.sys.SysMenuinfo">
//        str = "<resultMap id=" + cls.getSimpleName() + "ResultMap type=" + cls.getName() + "> \r\n";
//        str = str + "<id column= \"\""   +  " property= \"\"" +  " />" + " \r\n";
        // 每一行字符串
        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);
    }

    /**
     * 将字符串中的驼峰写法替换成'_'
     *
     * @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).toUpperCase());
        }
        //将字段全部装换为大写
        String result = getAllUpCaseList(str);
        return result;
    }

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





    public static void main(String[] args) throws Exception {
        //TODO 需要生成的属性
        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;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java我跟你拼了

您的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值