MyBatis对于时间等转换记载

Mybatis中数据字段转换

需要实现TypeHandler的接口
1、对于时间的转换

public class TimeValueHandler implements TypeHandler{

    private SimpleDateFormat  sd = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss")
    @Override
    public String getResult(ResultSet rs, String str) throws SQLException {
        return sd.format(rs.getTimestamp(str));
    }

    @Override
    public String getResult(ResultSet arg0, int arg1) throws SQLException {
        return null;
    }

    @Override
    public String getResult(CallableStatement arg0, int arg1) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void setParameter(PreparedStatement arg0, int arg1, String arg2, JdbcType arg3) throws SQLException {
        
    }
}

2、对于null的转换

public class NullValueHandler implements TypeHandler {

    @Override
    public String getResult(ResultSet resultSet, String str) throws SQLException {
        String columnValue = resultSet.getString(str);
        if (CommonUtils.isBlank(columnValue))
            return "";
        return columnValue;
    }

    @Override
    public String getResult(ResultSet resultSet, int index) throws SQLException {
        String columnValue = resultSet.getString(index);
        if (CommonUtils.isBlank(columnValue))
            return "";
        return columnValue;
    }

    @Override
    public String getResult(CallableStatement cs, int index) throws SQLException {
        return null;
    }

    @Override
    public void setParameter(PreparedStatement pstmt, int index, String str, JdbcType type) throws SQLException {
    }

}

这里想实现的就是因为之前我的全局转换失败,所以想从数据库层面将时间类型转换为我想要的字符串类型。所以handle实现后,需要做的就是在mapper.xml中做处理。

xml中所作的处理


    
        
        
     


 

浩语

                                          __                                                        
                            __  _  ____ __|  |__ _____    ___
                            \ \/ \/ /  |  \  |  \\__  \  /  _ \   
                             \     /|  |  /   Y  \/ __ \(  <_> )
                              \/\_/ |____/|___|  (____  /\____/ 
                                                    \/     \/          
          走在自己的路上,遇到要遇到的人,经历要经历的事,这才是我们需要面对的。
                                                       

你可能感兴趣的:(MyBatis对于时间等转换记载)