1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.consum.base.core.utils;
 
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.RowMapper;
 
/**
 * @author asus
 * @version 1.0
 * @description: Mapper map工具类</p> 包含驼峰格式和列名别名
 * @date 2023/11/11 14:40
 */
@Slf4j
public class MapperUtil implements RowMapper<Map<String, Object>> {
 
    @Override
    public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
        Map<String, Object> row = new HashMap<>();
        int columnCount = rs.getMetaData().getColumnCount();
        for (int i = 1; i <= columnCount; i++) {
            String columnLabel = rs.getMetaData().getColumnLabel(i);
            String columnName = columnLabel.contains("_") ? MapUtils.underlineToCamel(columnLabel)
                : (Character.isUpperCase(columnLabel.charAt(0)) ? columnLabel.toLowerCase()
                    : columnLabel);
            Object columnValue = rs.getObject(i);
            row.put(columnName, columnValue);
        }
        return row;
    }
}