DBUtils下划线命名转驼峰Pojo

记录DBUtils下划线转驼峰方法

实现ResultsetHandler接口,添加如下两个类

//HumpBeanResultHandler.java
import com.example.autojob.util.bean.ObjectUtil;
import com.example.autojob.util.convert.StringUtils;
import org.apache.commons.dbutils.ResultSetHandler;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 下划线命名转驼峰命名结果处理器
 *
 * @Author Huang Yongxiang
 * @Date 2022/08/17 22:02
 */
public class HumpBeanResultHandler<T> implements ResultSetHandler<T> {
    private final Class<T> type;


    public HumpBeanResultHandler(Class<T> type) {
        this.type = type;
    }

    @Override
    public T handle(ResultSet rs) throws SQLException {
        T instance = ObjectUtil.getClassInstance(type);
        if (instance == null) {
            throw new NullPointerException();
        }
        int count = 0;
        while (rs.next()) {
            for (Field field : type.getDeclaredFields()) {
                field.setAccessible(true);
                String humpName = field.getName();
                String unHumpName = StringUtils.uncamelCase(humpName);
                try {
                    field.set(instance, rs.getObject(unHumpName, field.getType()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (++count == 1) {
                break;
            }
        }
        return instance;
    }
}
//HumpBeanListResultHandler.java
import com.example.autojob.util.bean.ObjectUtil;
import com.example.autojob.util.convert.StringUtils;
import org.apache.commons.dbutils.ResultSetHandler;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 驼峰Bean List结果处理器
 *
 * @Author Huang Yongxiang
 * @Date 2022/08/18 9:44
 */
public class HumpBeanListResultHandler<T> implements ResultSetHandler<List<T>> {
    private final Class<T> thisType;

    public HumpBeanListResultHandler(Class<T> type) {
        this.thisType = type;
    }

    @Override
    public List<T> handle(ResultSet rs) throws SQLException {
        List<T> humpResult = new ArrayList<>();
        while (rs.next()) {
            T instance = ObjectUtil.getClassInstance(thisType);
            if (instance == null) {
                throw new NullPointerException();
            }
            for (Field field : thisType.getDeclaredFields()) {
                field.setAccessible(true);
                String humpName = field.getName();
                String unHumpName = StringUtils.uncamelCase(humpName);
                try {
                    field.set(instance, rs.getObject(unHumpName, field.getType()));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            humpResult.add(instance);
        }
        return humpResult;
    }
}

	/**
     * 驼峰命名法工具
     *
     * @return camelCase(" hello_world ") == "helloWorld"
     * capCamelCase("hello_world") == "HelloWorld"
     * uncamelCase("helloWorld") = "hello_world"
     */
    public static String uncamelCase(String s) {
        if (s == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            boolean nextUpperCase = true;

            if (i < (s.length() - 1)) {
                nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
            }

            if ((i > 0) && Character.isUpperCase(c)) {
                if (!upperCase || !nextUpperCase) {
                    sb.append(SEPARATOR);
                }
                upperCase = true;
            } else {
                upperCase = false;
            }

            sb.append(Character.toLowerCase(c));
        }

        return sb.toString();
    }
	public static <T> T getClassInstance(Class<T> clazz) {
        if (clazz != null) {
            try {
                return clazz.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                logger.error("创建Instance时发生异常:{}", e.getMessage());
            }
        }
        return null;
    }

用上面两个类替换原来query使用的BeanResultHandler和BeanListResultHandler即可。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值