JDBC开源框架:DBUtils自定义业务类型相关转换器

Posted 落孤秋叶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC开源框架:DBUtils自定义业务类型相关转换器相关的知识,希望对你有一定的参考价值。

dbutils提供的handler转换不能满足实际业务开发的需求。比如枚举转int,时间类型LocalDateTime,实体对象的属性名与字段未能相对应。

mysql表member结构字段: id、member_name、sex、createTime

public class Member {
    private long id;
    private String memberName;
    private Sex sex;
    private LocalDateTime createTime;
}

枚举Sex 时间LocalDateTime没有默认提供,需要转换。数据的转换需要实现ColumnHandler接口。此接口提供二个简单方法:1、类型是否匹配public boolean match(Class<?> propType);

2、转换数据 public Object apply(ResultSet rs, int columnIndex)。

一、定义二个类型转换handler

public class LocalDateTimeHandler implements ColumnHandler {

    @Override
    public boolean match(Class<?> propType) {
        return propType.equals(LocalDateTime.class);
    }

    @Override
    public Object apply(ResultSet rs, int columnIndex) throws SQLException {
        if (rs.getTimestamp(columnIndex) != null) {
            return rs.getTimestamp(columnIndex).toLocalDateTime();
        }
        return null;
    }
}
public class SexHandler implements ColumnHandler {

    @Override
    public boolean match(Class<?> propType) {
        return propType.equals(Sex.class);
    }

    @Override
    public Object apply(ResultSet rs, int columnIndex) throws SQLException {
        for (Sex sex : Sex.values()){
            if (sex.getIndex() == rs.getInt(columnIndex)){
                return sex;
            }
        }
        return null;
    }
}
public enum Sex {
    male(1,"男"),
    female(0,"女")
    ;
    private int index;
    private String description;

    Sex(int index, String description){
        this.index = index;
        this.description = description;
    }

    public int getIndex() {
        return index;
    }
    public String getDescription() {
        return description;
    }
}
二、重构BeanProcessor

public class MyBeanProcessor extends BeanProcessor {

    private static ServiceLoader<ColumnHandler> columnHandlers = ServiceLoader.load(ColumnHandler.class);

    private static List<ColumnHandler> customList = new ArrayList<>();

    static {
        customList.add(new LocalDateTimeHandler());
        customList.add(new SexHandler());
    }

public MyBeanProcessor(Map<String, String> columnToPropertyMap){
        super(columnToPropertyMap);
    }

    @Override
    protected Object processColumn(ResultSet rs, int index, Class<?> propType)
            throws SQLException {
        Object retval = rs.getObject(index);
        if ( !propType.isPrimitive() && retval == null ) {
            return null;
        }
        for (ColumnHandler handler : columnHandlers) {
            if (handler.match(propType)) {
                retval = handler.apply(rs, index);
                break;
            }
        }

        for (ColumnHandler handler : customList){
            if (handler.match(propType)){
                retval = handler.apply(rs, index);
                break;
            }
        }
        return retval;
    }
}

 

三、传自定义参数转换数据

@Test
    public void customQuery() throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        String sql = "select * from member";

        Map<String, String> map = new HashMap();
        map.put("member_name","memberName");
        MyBeanProcessor bean = new MyBeanProcessor(map);
        RowProcessor convert = new BasicRowProcessor(bean);

        BeanListHandler<Member> handler = new BeanListHandler(Member.class,convert);
        List<Member> list = queryRunner.query(getConn(),sql, handler);
        System.out.println(JSON.toJSONString(list));
    }

 









































































































以上是关于JDBC开源框架:DBUtils自定义业务类型相关转换器的主要内容,如果未能解决你的问题,请参考以下文章

开源框架:DBUtils使用详解

JDBC

DBUtils框架的使用(上)

Apache的DBUtils框架学习

你不知道的DBUtils框架

Apache—DBUtils框架简介DbUtils类QueryRunner类 ResultSetHandler接口