我已经与 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;