记录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即可。