1 /** 2 * Copyright 2010-2020 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 * http://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.BeanNameGenerator; 29 import org.springframework.context.annotation.Import; 30 31 /** 32 * Use this annotation to register MyBatis mapper interfaces when using Java Config. It performs when same work as 33 * {@link MapperScannerConfigurer} via {@link MapperScannerRegistrar}. 34 * 35 * <p> 36 * Either {@link #basePackageClasses} or {@link #basePackages} (or its alias {@link #value}) may be specified to define 37 * specific packages to scan. Since 2.0.4, If specific packages are not defined, scanning will occur from the package of 38 * the class that declares this annotation. 39 * 40 * <p> 41 * Configuration example: 42 * </p> 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 * 71 * @since 1.2.0 72 * @see MapperScannerRegistrar 73 * @see MapperFactoryBean 74 */ 75 @Retention(RetentionPolicy.RUNTIME) 76 @Target(ElementType.TYPE) 77 @Documented 78 @Import(MapperScannerRegistrar.class) 79 @Repeatable(MapperScans.class) 80 public @interface MapperScan { 81 82 /** 83 * Alias for the {@link #basePackages()} attribute. Allows for more concise annotation declarations e.g.: 84 * {@code @MapperScan("org.my.pkg")} instead of {@code @MapperScan(basePackages = "org.my.pkg"})}. 85 * 86 * @return base package names 87 */ 88 String[] value() default {}; 89 90 /** 91 * Base packages to scan for MyBatis interfaces. Note that only interfaces with at least one method will be 92 * registered; concrete classes will be ignored. 93 * 94 * @return base package names for scanning mapper interface 95 */ 96 String[] basePackages() default {}; 97 98 /** 99 * Type-safe alternative to {@link #basePackages()} for specifying the packages to scan for annotated components. The 100 * package of each class specified will be scanned. 101 * <p> 102 * Consider creating a special no-op marker class or interface in each package that serves no purpose other than being 103 * referenced by this attribute. 104 * 105 * @return classes that indicate base package for scanning mapper interface 106 */ 107 Class<?>[] basePackageClasses() default {}; 108 109 /** 110 * The {@link BeanNameGenerator} class to be used for naming detected components within the Spring container. 111 * 112 * @return the class of {@link BeanNameGenerator} 113 */ 114 Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class; 115 116 /** 117 * This property specifies the annotation that the scanner will search for. 118 * <p> 119 * The scanner will register all interfaces in the base package that also have the specified annotation. 120 * <p> 121 * Note this can be combined with markerInterface. 122 * 123 * @return the annotation that the scanner will search for 124 */ 125 Class<? extends Annotation> annotationClass() default Annotation.class; 126 127 /** 128 * This property specifies the parent that the scanner will search for. 129 * <p> 130 * The scanner will register all interfaces in the base package that also have the specified interface class as a 131 * parent. 132 * <p> 133 * Note this can be combined with annotationClass. 134 * 135 * @return the parent that the scanner will search for 136 */ 137 Class<?> markerInterface() default Class.class; 138 139 /** 140 * Specifies which {@code SqlSessionTemplate} to use in the case that there is more than one in the spring context. 141 * Usually this is only needed when you have more than one datasource. 142 * 143 * @return the bean name of {@code SqlSessionTemplate} 144 */ 145 String sqlSessionTemplateRef() default ""; 146 147 /** 148 * Specifies which {@code SqlSessionFactory} to use in the case that there is more than one in the spring context. 149 * Usually this is only needed when you have more than one datasource. 150 * 151 * @return the bean name of {@code SqlSessionFactory} 152 */ 153 String sqlSessionFactoryRef() default ""; 154 155 /** 156 * Specifies a custom MapperFactoryBean to return a mybatis proxy as spring bean. 157 * 158 * @return the class of {@code MapperFactoryBean} 159 */ 160 Class<? extends MapperFactoryBean> factoryBean() default MapperFactoryBean.class; 161 162 /** 163 * Whether enable lazy initialization of mapper bean. 164 * 165 * <p> 166 * Default is {@code false}. 167 * </p> 168 * 169 * @return set {@code true} to enable lazy initialization 170 * @since 2.0.2 171 */ 172 String lazyInitialization() default ""; 173 174 }