View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.submitted.enumtypehandler_on_annotation;
17  
18  import org.apache.ibatis.annotations.Arg;
19  import org.apache.ibatis.annotations.Case;
20  import org.apache.ibatis.annotations.ConstructorArgs;
21  import org.apache.ibatis.annotations.Result;
22  import org.apache.ibatis.annotations.Results;
23  import org.apache.ibatis.annotations.Select;
24  import org.apache.ibatis.annotations.TypeDiscriminator;
25  import org.apache.ibatis.submitted.enumtypehandler_on_annotation.Person.PersonType;
26  import org.apache.ibatis.type.EnumOrdinalTypeHandler;
27  
28  /**
29   * @since #444
30   *
31   * @author Kazuki Shimizu
32   */
33  public interface PersonMapper {
34  
35    // @formatter:off
36    @ConstructorArgs({
37          @Arg(column = "id", javaType = Integer.class, id = true)
38        , @Arg(column = "firstName", javaType = String.class)
39        , @Arg(column = "lastName", javaType = String.class)
40        // target for test (ordinal number -> Enum constant)
41        , @Arg(column = "personType", javaType = PersonType.class, typeHandler = EnumOrdinalTypeHandler.class)
42      })
43    // @formatter:on
44    @Select("SELECT id, firstName, lastName, personType FROM person WHERE id = #{id}")
45    Person findOneUsingConstructor(int id);
46  
47    // @formatter:off
48    @Results({
49        // target for test (ordinal number -> Enum constant)
50        @Result(property = "personType", column = "personType", typeHandler = EnumOrdinalTypeHandler.class)
51      })
52    // @formatter:on
53    @Select("SELECT id, firstName, lastName, personType FROM person WHERE id = #{id}")
54    Person findOneUsingSetter(int id);
55  
56    // @formatter:off
57    @TypeDiscriminator(
58        // target for test (ordinal number -> Enum constant)
59        column = "personType", javaType = PersonType.class, typeHandler = EnumOrdinalTypeHandler.class,
60        // Switch using enum constant name(PERSON or EMPLOYEE) at cases attribute
61        cases = {
62              @Case(value = "PERSON", type = Person.class, results = {@Result(property = "personType", column = "personType", typeHandler = EnumOrdinalTypeHandler.class)})
63            , @Case(value = "EMPLOYEE", type = Employee.class, results = {@Result(property = "personType", column = "personType", typeHandler = EnumOrdinalTypeHandler.class)})
64          })
65    // @formatter:on
66    @Select("SELECT id, firstName, lastName, personType FROM person WHERE id = #{id}")
67    Person findOneUsingTypeDiscriminator(int id);
68  
69  }