如何在插入时使用typeHandler映射mybatis中的枚举类型

时间:2013-04-14 15:23:32

标签: java mysql enums mybatis typehandler

我已经用enum打了一段时间,但它不会按我的方式行事。 有没有人可以给我提示? 我试图在MySql中使用Enum类型,我也在我的代码中使用了Enum类。

由于代码现在它将插入星期一,但它也会尝试在workdayID上插入MONDAY ...我没有得到workdayID。 我相信我必须以某种方式处理DAY_TYPE ...定义一个typeHandler也许? 但我试过了,它不会起作用,或者因为我无法正确做到这一点?

我也试过了org.apache.ibatis.type.EnumTypeHandler但没有成功,比如这个

    #{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>  

我的数据库表

    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;      

3 个答案:

答案 0 :(得分:7)

工作日就是这个

    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....

正确的解决方案当然是在工作日表中添加枚举......它会减少我相信的数据库抓取。

    <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>   

现在删除daytaype表 并直接在工作日表中添加枚举。

    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 ;

答案 1 :(得分:0)

您案例中的DAY_TYPE包含成员ID。 现在,如果您将ENUM存储为数据库中的id,即使您没有将Enum与数据库同步,也可以使用可以为您处理DAY_TYPE的CustomEnumTypeHandler。

答案 2 :(得分:0)

覆盖getter setter。我喜欢这个:

@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;
}