How to map Enum type in mybatis using typeHandler on insert

我怕爱的太早我们不能终老 提交于 2019-12-22 03:16:25

问题


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;      

回答1:


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 ;



回答2:


override getter setter. my like this:

@Column(name = "type")
private MailType type;

public String getType(){
    return type.name();
}
public void setType(String type){
    this.type = MailType.valueOf(type);
}
public MailType getTypeEnum(){
    return type;
}
public void setTypeEnum(MailType type){
    this.type = type;
}



回答3:


DAY_TYPE in your case contains a member id. Now if you store your ENUM as id in the database you can always use a CustomEnumTypeHandler which can handle the DAY_TYPE for you even if you don't have your Enum synced with your database.



来源:https://stackoverflow.com/questions/16000709/how-to-map-enum-type-in-mybatis-using-typehandler-on-insert

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