MyBatis Query Map Custom Enumeration

Source: Internet
Author: User
Background MyBatis Query If you want to map enumerated types, you need to select one from Enumtypehandler or Enumordinaltypehandler to use 1.             Enumordinaltypehandler is accessed according to the sequence, and the corresponding database is designed to use 0 to start in ascending order. 2. The enumtypehandler is accessed according to the name of the enumeration, and the corresponding database is designed to use the enumeration name directly.
But there are also things that don't start with 0 and are not in ascending order 0. None of the two above satisfies our needs, and this time we want to use enumerations. This requires the use of a custom enumeration class.
Use

You can override the type processor or create your own type processor to handle unsupported or non-standard types. The practice is to implement the Org.apache.ibatis.type.TypeHandler interface, or to inherit a very convenient class Org.apache.ibatis.type.BaseTypeHandler
We can write a corresponding handler for each enumerated class, but this is more complicated, so we implement a unified enumeration interface for each enumerated class that needs to be mapped to an enumeration: Ienum.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 /** * * @ClassName: Ienum * @Description: Interface Specification key value for all enumerated implementations of this system is used for MyBatis enumeration mappings * @author jerome_s@qq.com *      Date November 29, 2015 afternoon 9:29:35 * */public interface Ienum {int getkey ();        void Setkey (int key);      String GetValue (); void SetValue (String value); }
Write a handle:EnumKeyTypeHandler.java for this interface The
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 The Yi-A-yi-An-yi-an-ba-yi-yi-yi-/**   *    *  @ClassName:  enumkeytypehand ler    *  @Description:  All enumerations in this system use this processor to set key as Access     *  @author  jerome_ s@qq.com   *  @date  2015 year November 29   PM 9:34:14    *  @see   Reference source Enumordinaltypehandler/enumtypehandler   *  @see   reference  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.