入时使用typeHandler映射mybatis中的Enum类型

入时使用typeHandler映射mybatis中的Enum类型

本文介绍了如何在插入时使用typeHandler映射mybatis中的Enum类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为枚举而苦苦挣扎了一段时间,但这不会走我的路.有没有人可以给我提示?我试图在MySql中使用Enum类型,并且在代码中也使用Enum类.

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.

因为代码是现在的代码,所以它将插入MONDAY,但也会尝试在workdayID上插入MONDAY ...我没有得到workdayID.我相信我必须以som的方式处理DAY_TYPE ...可能定义一个typeHandler ??但是我尝试了一下,但是它不起作用,或者因为我无法正确执行?

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?

我也尝试了org.apache.ibatis.type.EnumTypeHandler,但没有成功,像这样

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

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

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;

推荐答案

工作日就这样存在

    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>

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

And now delete daytaype tableAnd 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 ;

这篇关于如何在插入时使用typeHandler映射mybatis中的Enum类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:23