javamysqlenumsmybatistypehandler

How to map Enum type in mybatis using typeHandler on insert


I have strugled with enum for a while now but it will not go my way. Is there anyone that can give me hint? I am trying to use Enum type in MySql and I also use an Enum class in my code.

As the code is now It will insert MONDAY but it will also try to insert MONDAY on workdayID... I do not get the workdayID. I belive I have to handle the DAY_TYPE in som way ... define a typeHandler maybe?? but I tried that and it would not work, or its becouse I can not do it correct?

I also tried the org.apache.ibatis.type.EnumTypeHandler but with no success, like this

    #{DAY_TYPE,typeHandler=org.apache.ibatis.type.EnumTypeHandler}

DAY_TYPE.java

    package tut.model;
    import java.io.Serializable;
    public enum DAY_TYPE implements Serializable{
        MONDAY(1),TUESDAY(2),WEDNESDAY(3),THURSDAY(4),FRIDAY(5),SATURDAY(6),SUNDAY(7);
        private int id; private int workdayID;

        private DAY_TYPE(int id) { this.id = id; }

        public int getId() { return id;    }
        public void setId(int id) {  this.id = id;    }

        public int getWorkdayID() { return workdayID;    }
        public void setWorkdayID(int workdayID) {this.workdayID = workdayID;}
    }

DAY_TYPE_Mapper.xml

    <insert id="insert" parameterType="DAY_TYPE" useGeneratedKeys="true" keyProperty="iddaytaype">
        INSERT INTO daytaype (DAY_TYPE, workdayID)
        VALUES (#{DAY_TYPE},#{workdayID})
        <selectKey keyProperty="iddaytaype" resultType="long" order="AFTER">
            SELECT LAST_INSERT_ID();
        </selectKey>
    </insert>

<!-- <insert id="insert" parameterMap="insert-params" useGeneratedKeys="true" keyProperty="iddaytaype"> -->
    <parameterMap id="insert-params"   type="DAY_TYPE">
        <parameter property="DAY_TYPE" javaType="DAY_TYPE" typeHandler="mappings.XenumTypeHandler"  />
        <parameter property="workdayID" javaType="int" />
    </parameterMap>  

My database table

    CREATE TABLE `daytaype` (
      `iddaytaype` int(11) NOT NULL AUTO_INCREMENT,
      `DAY_TYPE` enum('MONDAY','TUESDAY','WEDNESDAY','THURSDAY','FRIDAY','SATURDAY','SUNDAY') NOT NULL,
      `workdayID` int(11) unsigned DEFAULT NULL,
      PRIMARY KEY (`iddaytaype`),
      KEY `fk_workDayID_idWorkDay` (`workdayID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;=InnoDB DEFAULT CHARSET=utf8;      

Solution

  • Workday exists as this

        public class Workday implements Serializable{
            private long idWorkDay;
            private Date start;
            private Date end;
            private List<Workbreak> workBreaks;
            private DAY_TYPE DAY_TYPE;
            private long workweekID;
            getter setter....
    

    The correct solution is of course to add enum in the workday table ... and it will reduse database seize a lot I belive.

        <insert id="insert" parameterType="workday" useGeneratedKeys="true" keyProperty="idWorkDay">
          INSERT INTO workday 
            ( start , end , workweekID , DAY_TYPE )
          VALUES (
            #{start},
            #{end},
            #{workweekID},
            #{DAY_TYPE, typeHandler=org.apache.ibatis.type.EnumTypeHandler}
          )
            <selectKey keyProperty="idWorkDay" resultType="long" order="AFTER">
                SELECT LAST_INSERT_ID();
            </selectKey>
        </insert>   
    

    And now delete daytaype table And add enum directly in workday table instead.

        CREATE TABLE `workday` (
          `idworkday` int(11) NOT NULL AUTO_INCREMENT,
          `start` time DEFAULT '08:00:00',
          `end` time DEFAULT '17:00:00',
          `workweekID` int(11) DEFAULT NULL,
          `DAY_TYPE` enum('MONDAY','TUESDAY','WEDNESDAY','THURSDAY','FRIDAY','SATURDAY','SUNDAY') DEFAULT NULL,
          PRIMARY KEY (`idworkday`),
          KEY `fk_workweek_workweekID` (`workweekID`),
          CONSTRAINT `fk_workweek_workweekID` FOREIGN KEY (`workweekID`) REFERENCES `workweek` (`idworkweek`) ON DELETE CASCADE ON UPDATE NO ACTION
        ) ENGINE=InnoDB AUTO_INCREMENT=139 DEFAULT CHARSET=utf8 ;