码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2017-12-29 15:26:56      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:hash   getc   time()   err   select   mem   ice   tco   extends   

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自定义业务类型相关转换器

标签:hash   getc   time()   err   select   mem   ice   tco   extends   

原文地址:https://www.cnblogs.com/song27/p/8143706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!