|
23 | 23 | import org.apache.ibatis.session.SqlSessionFactory;
|
24 | 24 | import org.mybatis.spring.SqlSessionFactoryBean;
|
25 | 25 | import org.mybatis.spring.SqlSessionTemplate;
|
26 |
| -import org.mybatis.spring.mapper.MapperFactoryBean; |
27 | 26 | import org.slf4j.Logger;
|
28 | 27 | import org.slf4j.LoggerFactory;
|
29 | 28 | import org.springframework.beans.BeansException;
|
|
54 | 53 | import org.springframework.util.CollectionUtils;
|
55 | 54 | import org.springframework.util.ObjectUtils;
|
56 | 55 | import org.springframework.util.StringUtils;
|
| 56 | +import tk.mybatis.spring.annotation.BaseProperties; |
57 | 57 | import tk.mybatis.spring.mapper.ClassPathMapperScanner;
|
| 58 | +import tk.mybatis.spring.mapper.MapperFactoryBean; |
| 59 | +import tk.mybatis.spring.mapper.SpringBootBindUtil; |
58 | 60 |
|
59 | 61 | import javax.annotation.PostConstruct;
|
60 | 62 | import javax.sql.DataSource;
|
| 63 | +import java.util.Arrays; |
61 | 64 | import java.util.List;
|
62 | 65 |
|
63 | 66 | /**
|
@@ -194,15 +197,20 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
|
194 | 197 | if (this.resourceLoader != null) {
|
195 | 198 | scanner.setResourceLoader(this.resourceLoader);
|
196 | 199 | }
|
197 |
| - |
198 | 200 | List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
|
199 | 201 | if (logger.isDebugEnabled()) {
|
200 | 202 | for (String pkg : packages) {
|
201 | 203 | logger.debug("Using auto-configuration base package '{}'", pkg);
|
202 | 204 | }
|
203 | 205 | }
|
204 |
| - |
205 |
| - scanner.setAnnotationClass(Mapper.class); |
| 206 | + BaseProperties properties = SpringBootBindUtil.bind(environment, BaseProperties.class, BaseProperties.MYBATIS_PREFIX); |
| 207 | + String[] basePackages = properties.getBasePackages(); |
| 208 | + if(basePackages != null && basePackages.length > 0){ |
| 209 | + packages.addAll(Arrays.asList(basePackages)); |
| 210 | + } else { |
| 211 | + //设置了包名的情况下,不需要指定该注解 |
| 212 | + scanner.setAnnotationClass(Mapper.class); |
| 213 | + } |
206 | 214 | scanner.registerFilters();
|
207 | 215 | scanner.doScan(StringUtils.toStringArray(packages));
|
208 | 216 | } catch (IllegalStateException ex) {
|
|
0 commit comments