MyBatisã§Enumã使ã
MyBatisã§Enumã使ãã«ã¯ã©ãããã°ããã®ãã
ãã¡ããããã¥ã¢ã«ã«æ¸ãã¦ããããã¾ã¨ããã¨ããã ã
- resultMapã§Enumã使ãã¨ãã¯typeHandlerãæ¸ã
- parameterã§Enumã使ãã¨ãã¯mybatis-config.xmlã«handlerãå®ç¾©ãã
å ·ä½ä¾ãããã¥ã¢ã«ããå¼ç¨ããã¨
<resultMap type="org.apache.ibatis.submitted.rounding.User" id="usermap2"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="funkyNumber" property="funkyNumber"/> <result column="roundingMode" property="roundingMode" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/> </resultMap> <!-- mybatis-config.xml --> <typeHandlers> <typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="java.math.RoundingMode"/> </typeHandlers>
ãã®æ¹æ³ãresultMapã¯ãããã©parameterã使ãã¨ãã«ããããmybatis-config.xmlã«handlerãå®ç¾©ããã£ã¦ããã®ã¯ããã©ããããªããã俺ã¯ããã©ãããã
ãªã®ã§ãmybatis-config.xmlã«handlerãå®ç¾©ããªãã¦æ¸ãæ¹æ³ããªããæ¢ããã çµæããã£ãã«ã¯ãã£ããæ£æ»æ³ãããªãã£ã½ãã®ã§æ³¨æãå¿ è¦ã
è¦ã¯typeHandlerã®ä»£ããã«ordinalã使ãã£ã¦ãã¨ã ã
ã¤ã¾ããããã
#{roundingMode} <!-- mybatis-config.xml --> <typeHandlers> <typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="java.math.RoundingMode"/> </typeHandlers>
ãããã
#{roundingMode.ordinal}
ã¡ãªã¿ã«ordinal()ã ã¨ãã¾ããããªãã ã©ãããã#{}ã®ä¸ã§ã¯Reflectionã§ãã£ã¼ã«ããåç §ãããã¨ãã§ãããããã
ä¸ã®ä¾ã§ã¯EnumOrdinalTypeHandlerã®å ´åã ããEnumTypeHandlerã使ãããå ´åã¯ordinalãæ¸ããªãã¦ãããåãEnumã ã¨ããã©ã«ãã§EnumTypeHandlerã使ãããã
ãã¨ãparameterã1ã¤ã ãã§ãããEnumã®å ´åã¯@Paramã¤ããæ¹ããããã¤ããªãã¨ãã¾ãåããªãã£ãã