Does a library exist with Joda Time / iBATIS integration classes?

我怕爱的太早我们不能终老 提交于 2019-12-11 08:11:53

问题


It looks like I have to implement com.ibatis.sqlmap.client.extensions.TypeHandlerCallback for both DateTime and LocalDateTime Joda types. This isn't a big deal, but if it's implemented somewhere else I'd rather just use that.


回答1:


There are no clean and flexible solutions, IMO. Jodatime dates are more flexible than the JDBC native types (java.sql.Date,java.sql.Timestamp...) and if you map to them you might be losing information (timezones). An "full" implementation, (perhaps bypassing the getDate()/getTimestamp() etc JDBC methods, using strings for example) would depend on your JDBC driver and database (and practically no database has datetime type as expressive as Jodatime's).

Here's a rather trivial implementation I did once for LocalDateTime. Not very tested, and it assumes your DB timestamps represent local datetimes.

public Object getResult(ResultGetter getter) throws SQLException {
   Timestamp date = getter.getTimestamp();
   if(date == null) return null; 
   LocalDateTime ldt = null;
   try {
      ldt = LocalDateTime.fromDateFields(date);
   } catch(IllegalArgumentException e) {
      throw new SQLException("illegal value for a LocalDateTime : " + date);
   }
   return ldt; 
}

public void setParameter(ParameterSetter setter, Object parameter) throws SQLException {
   java.sql.Timestamp date = null; 
   if(parameter != null) { 
      LocalDateTime ldt = (LocalDateTime) parameter;
      date = new Timestamp(ldt.toDateTime().getMillis());
   } 
   setter.setTimestamp(date);
}



回答2:


I'll put this out there... we just moved our code to do this for both iBatis and myBatis. As everyone has mentioned, timezones are not fun to work with because of the limitations in the java.sql package. However, it is possible with the proper setup at least with LocalDate and DateTime

  • https://github.com/intouchfollowup/joda-mybatis
  • https://github.com/intouchfollowup/joda-ibatis

Please give feedback! This package is early



来源:https://stackoverflow.com/questions/2916942/does-a-library-exist-with-joda-time-ibatis-integration-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!