Options.java

  1. /*
  2.  *    Copyright 2009-2024 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.annotations;

  17. import java.lang.annotation.Documented;
  18. import java.lang.annotation.ElementType;
  19. import java.lang.annotation.Repeatable;
  20. import java.lang.annotation.Retention;
  21. import java.lang.annotation.RetentionPolicy;
  22. import java.lang.annotation.Target;

  23. import org.apache.ibatis.mapping.ResultSetType;
  24. import org.apache.ibatis.mapping.StatementType;

  25. /**
  26.  * The annotation that specify options for customizing default behaviors.
  27.  * <p>
  28.  * <b>How to use:</b>
  29.  *
  30.  * <pre>
  31.  * public interface UserMapper {
  32.  *   &#064;Options(useGeneratedKeys = true, keyProperty = "id")
  33.  *   &#064;Insert("INSERT INTO users (name) VALUES(#{name})")
  34.  *   boolean insert(User user);
  35.  * }
  36.  * </pre>
  37.  *
  38.  * @author Clinton Begin
  39.  */
  40. @Documented
  41. @Retention(RetentionPolicy.RUNTIME)
  42. @Target(ElementType.METHOD)
  43. @Repeatable(Options.List.class)
  44. public @interface Options {
  45.   /**
  46.    * The options for the {@link Options#flushCache()}. The default is {@link FlushCachePolicy#DEFAULT}
  47.    */
  48.   enum FlushCachePolicy {
  49.     /** <code>false</code> for select statement; <code>true</code> for insert/update/delete statement. */
  50.     DEFAULT,
  51.     /** Flushes cache regardless of the statement type. */
  52.     TRUE,
  53.     /** Does not flush cache regardless of the statement type. */
  54.     FALSE
  55.   }

  56.   /**
  57.    * Returns whether use the 2nd cache feature if assigned the cache.
  58.    *
  59.    * @return {@code true} if use; {@code false} if otherwise
  60.    */
  61.   boolean useCache() default true;

  62.   /**
  63.    * Returns the 2nd cache flush strategy.
  64.    *
  65.    * @return the 2nd cache flush strategy
  66.    */
  67.   FlushCachePolicy flushCache() default FlushCachePolicy.DEFAULT;

  68.   /**
  69.    * Returns the result set type.
  70.    *
  71.    * @return the result set type
  72.    */
  73.   ResultSetType resultSetType() default ResultSetType.DEFAULT;

  74.   /**
  75.    * Return the statement type.
  76.    *
  77.    * @return the statement type
  78.    */
  79.   StatementType statementType() default StatementType.PREPARED;

  80.   /**
  81.    * Returns the fetch size.
  82.    *
  83.    * @return the fetch size
  84.    */
  85.   int fetchSize() default -1;

  86.   /**
  87.    * Returns the statement timeout.
  88.    *
  89.    * @return the statement timeout
  90.    */
  91.   int timeout() default -1;

  92.   /**
  93.    * Returns whether use the generated keys feature supported by JDBC 3.0
  94.    *
  95.    * @return {@code true} if use; {@code false} if otherwise
  96.    */
  97.   boolean useGeneratedKeys() default false;

  98.   /**
  99.    * Returns property names that holds a key value.
  100.    * <p>
  101.    * If you specify multiple property, please separate using comma(',').
  102.    *
  103.    * @return property names that separate with comma(',')
  104.    */
  105.   String keyProperty() default "";

  106.   /**
  107.    * Returns column names that retrieves a key value.
  108.    * <p>
  109.    * If you specify multiple column, please separate using comma(',').
  110.    *
  111.    * @return column names that separate with comma(',')
  112.    */
  113.   String keyColumn() default "";

  114.   /**
  115.    * Returns result set names.
  116.    * <p>
  117.    * If you specify multiple result set, please separate using comma(',').
  118.    *
  119.    * @return result set names that separate with comma(',')
  120.    */
  121.   String resultSets() default "";

  122.   /**
  123.    * @return A database id that correspond this options
  124.    *
  125.    * @since 3.5.5
  126.    */
  127.   String databaseId() default "";

  128.   /**
  129.    * The container annotation for {@link Options}.
  130.    *
  131.    * @author Kazuki Shimizu
  132.    *
  133.    * @since 3.5.5
  134.    */
  135.   @Documented
  136.   @Retention(RetentionPolicy.RUNTIME)
  137.   @Target(ElementType.METHOD)
  138.   @interface List {
  139.     Options[] value();
  140.   }

  141. }