How to use custom type handler (typeHandler) in ibatis 2.3.4

i am using ibatis to replace JDBC in a old system. Many old bean are using Calendar instead of Date。So i am considering write aTypeHandlerCallbackto handler the translation.

the class implement TypeHandlerCallback:

public class CalendarTypeHandlerCallback implements TypeHandlerCallback {

 public Object getResult(ResultGetter getter) throws SQLException {
  String s = getter.getString();
  DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  Calendar cdate = Calendar.getInstance();
  try {
   Date date = format.parse(s);
   cdate.setTime(date);
  } catch (Exception e) {
  }
  return cdate;
 }

 public void setParameter(ParameterSetter setter, Object parameter)
   throws SQLException {
  Calendar cdate = (Calendar)parameter;
  java.sql.Timestamp time = new java.sql.Timestamp(cdate.getTimeInMillis());
  setter.setTimestamp(time);
 }

 public Object valueOf(String s) {
  Calendar cdate = Calendar.getInstance();
  DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  try {
   Date date = format.parse(s);
   cdate.setTime(date);
  } catch (Exception e) {
  }
  return cdate;
 }

}

the map xml:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMap
        PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
        "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="TunnelsDao"> 

<typeAlias alias="Calendar" type="java.util.Calendar"/>
  <resultMap id="TunnelsDaoResult" class="com.adito.tunnels.DefaultTunnel">
    <result property="resourceId" column="TUNNEL_ID"/>
    <result property="type" column="TYPE"/>
    <result property="resourceName" column="NAME"/>
    <result property="resourceDescription" column="DESCRIPTION"/>
    <result property="transport" column="TRANSPORT"/>
    <result property="username" column="USERNAME"/>
    <result property="sourcePort" column="SOURCE_PORT"/>
    <result property="destination_port" column="DESTINATION_PORT"/>
    <result property="destination_host" column="DESTINATION_HOST"/>
    <result property="autoStart" column="AUTO_START"/> 
<result property="dateCreated" column="DATE_CREATED"
     javaType="Calendar" jdbcType="TIMESTAMPE" typeHandler="an.flex.ibatis.CalendarTypeHandlerCallback"/>
    <result property="dateAmended" column="DATE_AMENDED"
     javaType="Calendar" jdbcType="TIMESTAMPE" typeHandler="an.flex.ibatis.CalendarTypeHandlerCallback"/>  
<!-- this will cause ibatis to invoke getResult function in CalendarTypeHandlerCallback to transfer data from DB to Calendar in bean-->
  </resultMap>

  <select id="selectAll" resultMap="TunnelsDaoResult">
    select * from tunnels
  </select>

<insert id="insert" parameterClass="com.adito.tunnels.DefaultTunnel">
 INSERT INTO tunnels (TYPE, NAME, DESCRIPTION, TRANSPORT, USERNAME,
 SOURCE_PORT, DESTINATION_PORT, DESTINATION_HOST, AUTO_START,
 DATE_CREATED, DATE_AMENDED)
 VALUES (#type#, #resourceName#, #resourceDescription#, #transport#,#username#,
 #sourcePort#,#destination_port#,#destination_host#, #autoStart#,
#dateCreated,handler=an.flex.ibatis.CalendarTypeHandlerCallback#,
 #dateAmended,handler=an.flex.ibatis.CalendarTypeHandlerCallback#)

<!--this will cause ibatis to invoke setParameter function in CalendarTypeHandlerCallback to transfer Calendar data in bean to DB-->
 <selectKey resultClass="int" >
 SELECT LAST_INSERT_ID() AS ID
 </selectKey>
</insert>

<delete id="deleteAll">
 delete from tunnels
</delete>

<delete id="deleteById" parameterClass="Integer">
 delete from tunnels where TUNNEL_ID = #resourceId#
</delete>
</sqlMap>

 

你可能感兴趣的:(ibatis,calendar,datatime)