Oracle SQL DATE conversion problem using iBATIS via Java JDBC

前端 未结 7 622
悲哀的现实
悲哀的现实 2020-12-24 03:36

I\'m currently wrestling with an Oracle SQL DATE conversion problem using iBATIS from Java.

Am using the Oracle JDBC thin driver ojdbc14 version 10.2.0.4.0. iBATIS v

7条回答
  •  长情又很酷
    2020-12-24 03:59

    I found out how to solve this problem. iBATIS permits custom type handlers to be registered. So in my sqlmap-config.xml file I added this:

    
    
    

    And then added this class which implements the iBATIS TypeHandlerCallback interface:

    // corrected getResult()/setParameter() to correctly deal with when value is null
    public class CustomDateHandler implements TypeHandlerCallback {
        @Override
        public Object getResult(ResultGetter getter) throws SQLException {
            final Object obj = getter.getTimestamp();
            return obj != null ? (Date) obj : null;
        }
    
        @Override
        public void setParameter(ParameterSetter setter,Object value) throws SQLException {
            setter.setTimestamp(value != null ? new Timestamp(((Date)value).getTime()) : null);
        }
    
        @Override
        public Object valueOf(String datetime) {
            return Timestamp.valueOf(datetime);
        }
    }
    

    Whennever I need to map an Oracle DATE I now describe it like so:

    
    

提交回复
热议问题