13

我已经与 enum 斗争了一段时间,但它不会如我所愿。有没有人可以给我提示?我正在尝试在 MySql 中使用 Enum 类型,并且在我的代码中也使用了 Enum 类。

由于现在的代码它将插入星期一,但它也会尝试在工作日 ID 上插入星期一...我没有得到工作日 ID。我相信我必须以某种方式处理 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;      
4

3 回答 3

8

工作日就这样存在

    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 ;
于 2013-04-22T00:19:06.213 回答
1

覆盖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;
}
于 2018-04-13T03:07:05.570 回答
0

在您的情况下,DAY_TYPE 包含一个成员 ID。现在,如果您将 ENUM 作为 id 存储在数据库中,您始终可以使用 CustomEnumTypeHandler 来为您处理 DAY_TYPE,即使您没有将 ENUM 与数据库同步。

于 2013-09-12T07:26:15.297 回答