1 /*
2 * Copyright 2010-2025 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.mybatis.spring.annotation;
17
18 import java.lang.annotation.Annotation;
19 import java.lang.annotation.Documented;
20 import java.lang.annotation.ElementType;
21 import java.lang.annotation.Repeatable;
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.RetentionPolicy;
24 import java.lang.annotation.Target;
25
26 import org.mybatis.spring.mapper.MapperFactoryBean;
27 import org.mybatis.spring.mapper.MapperScannerConfigurer;
28 import org.springframework.beans.factory.support.AbstractBeanDefinition;
29 import org.springframework.beans.factory.support.BeanNameGenerator;
30 import org.springframework.context.annotation.ComponentScan;
31 import org.springframework.context.annotation.Import;
32 import org.springframework.core.annotation.AliasFor;
33
34 /**
35 * Use this annotation to register MyBatis mapper interfaces when using Java Config. It performs when same work as
36 * {@link MapperScannerConfigurer} via {@link MapperScannerRegistrar}.
37 * <p>
38 * Either {@link #basePackageClasses} or {@link #basePackages} (or its alias {@link #value}) may be specified to define
39 * specific packages to scan. Since 2.0.4, If specific packages are not defined, scanning will occur from the package of
40 * the class that declares this annotation.
41 * <p>
42 * Configuration example:
43 *
44 * <pre class="code">
45 * @Configuration
46 * @MapperScan("org.mybatis.spring.sample.mapper")
47 * public class AppConfig {
48 *
49 * @Bean
50 * public DataSource dataSource() {
51 * return new EmbeddedDatabaseBuilder().addScript("schema.sql").build();
52 * }
53 *
54 * @Bean
55 * public DataSourceTransactionManager transactionManager() {
56 * return new DataSourceTransactionManager(dataSource());
57 * }
58 *
59 * @Bean
60 * public SqlSessionFactory sqlSessionFactory() throws Exception {
61 * SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
62 * sessionFactory.setDataSource(dataSource());
63 * return sessionFactory.getObject();
64 * }
65 * }
66 * </pre>
67 *
68 * @author Michael Lanyon
69 * @author Eduardo Macarron
70 * @author Qimiao Chen
71 *
72 * @since 1.2.0
73 *
74 * @see MapperScannerRegistrar
75 * @see MapperFactoryBean
76 */
77 @Retention(RetentionPolicy.RUNTIME)
78 @Target(ElementType.TYPE)
79 @Documented
80 @Import(MapperScannerRegistrar.class)
81 @Repeatable(MapperScans.class)
82 public @interface MapperScan {
83
84 /**
85 * Alias for the {@link #basePackages()} attribute. Allows for more concise annotation declarations e.g.:
86 * {@code @MapperScan("org.my.pkg")} instead of {@code @MapperScan(basePackages = "org.my.pkg"})}.
87 *
88 * @return base package names
89 */
90 @AliasFor("basePackages")
91 String[] value() default {};
92
93 /**
94 * Base packages to scan for MyBatis interfaces. Note that only interfaces with at least one method will be
95 * registered; concrete classes will be ignored.
96 *
97 * @return base package names for scanning mapper interface
98 */
99 @AliasFor("value")
100 String[] basePackages() default {};
101
102 /**
103 * Type-safe alternative to {@link #basePackages()} for specifying the packages to scan for annotated components. The
104 * package of each class specified will be scanned.
105 * <p>
106 * Consider creating a special no-op marker class or interface in each package that serves no purpose other than being
107 * referenced by this attribute.
108 *
109 * @return classes that indicate base package for scanning mapper interface
110 */
111 Class<?>[] basePackageClasses() default {};
112
113 /**
114 * The {@link BeanNameGenerator} class to be used for naming detected components within the Spring container.
115 *
116 * @return the class of {@link BeanNameGenerator}
117 */
118 Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;
119
120 /**
121 * This property specifies the annotation that the scanner will search for.
122 * <p>
123 * The scanner will register all interfaces in the base package that also have the specified annotation.
124 * <p>
125 * Note this can be combined with markerInterface.
126 *
127 * @return the annotation that the scanner will search for
128 */
129 Class<? extends Annotation> annotationClass() default Annotation.class;
130
131 /**
132 * This property specifies the parent that the scanner will search for.
133 * <p>
134 * The scanner will register all interfaces in the base package that also have the specified interface class as a
135 * parent.
136 * <p>
137 * Note this can be combined with annotationClass.
138 *
139 * @return the parent that the scanner will search for
140 */
141 Class<?> markerInterface() default Class.class;
142
143 /**
144 * Specifies which {@code SqlSessionTemplate} to use in the case that there is more than one in the spring context.
145 * Usually this is only needed when you have more than one datasource.
146 *
147 * @return the bean name of {@code SqlSessionTemplate}
148 */
149 String sqlSessionTemplateRef() default "";
150
151 /**
152 * Specifies which {@code SqlSessionFactory} to use in the case that there is more than one in the spring context.
153 * Usually this is only needed when you have more than one datasource.
154 *
155 * @return the bean name of {@code SqlSessionFactory}
156 */
157 String sqlSessionFactoryRef() default "";
158
159 /**
160 * Specifies a custom MapperFactoryBean to return a mybatis proxy as spring bean.
161 *
162 * @return the class of {@code MapperFactoryBean}
163 */
164 Class<? extends MapperFactoryBean> factoryBean() default MapperFactoryBean.class;
165
166 /**
167 * Whether enable lazy initialization of mapper bean.
168 * <p>
169 * Default is {@code false}.
170 *
171 * @return set {@code true} to enable lazy initialization
172 *
173 * @since 2.0.2
174 */
175 String lazyInitialization() default "";
176
177 /**
178 * Specifies the default scope of scanned mappers.
179 * <p>
180 * Default is {@code ""} (equiv to singleton).
181 *
182 * @return the default scope
183 */
184 String defaultScope() default AbstractBeanDefinition.SCOPE_DEFAULT;
185
186 /**
187 * Specifies a flag that whether execute a property placeholder processing or not.
188 * <p>
189 * The default is {@literal true}. This means that a property placeholder processing execute.
190 *
191 * @since 3.0.3
192 *
193 * @return a flag that whether execute a property placeholder processing or not
194 */
195 boolean processPropertyPlaceHolders() default true;
196
197 /**
198 * Specifies which types are not eligible for mapper scanning.
199 *
200 * @since 3.0.4
201 *
202 * @return array of customized mapper excludeFilter
203 */
204 ComponentScan.Filter[] excludeFilters() default {};
205
206 }