import org.springframework.core.annotation.AnnotationAttributes; //导入方法依赖的package包/类
private Map findMyBatisBeanDefinitions() {
String[] enumsLocations = attributes.getStringArray(Constant.ENUMS_LOCATIONS_ATTRIBUTE_NAME);
String[] basePackages = attributes.getStringArray(Constant.BASE_PACKAGES_ATTRIBUTE_NAME);
AnnotationAttributes[] plugins = attributes.getAnnotationArray(Constant.PLUGINS_ATTRIBUTE_NAME);
if (ArrayUtils.isEmpty(enumsLocations)) {
enumsLocations = findDefaultPackage(metadata);
}
if (ArrayUtils.isEmpty(basePackages)) {
basePackages = findDefaultPackage(metadata);
}
Map beanDefinitions &#61; new HashMap<>(16);
BeanDefinitionBuilder sqlSessionFactoryBean &#61; BeanDefinitionBuilder.genericBeanDefinition(SqlSessionFactoryBean.class);
if (useFlyway) {
sqlSessionFactoryBean.addDependsOn(Constant.FLYWAY_BEAN_NAME);
}
sqlSessionFactoryBean.addPropertyReference("dataSource", Constant.DATA_SOURCE_BEAN_NAME);
sqlSessionFactoryBean.addPropertyValue("mapperLocations", "classpath*:/META-INF/mybatis/**/*Mapper.xml");
sqlSessionFactoryBean.addPropertyValue("configLocation", "classpath:/META-INF/mybatis/mybatis.xml");
TypeHandlerScanner scanner &#61; new TypeHandlerScanner();
sqlSessionFactoryBean.addPropertyValue("typeHandlers", scanner.find(StringUtils.join(enumsLocations, ",")));
List pluginsList &#61; new ArrayList<>(plugins.length);
List> clazz &#61; new ArrayList<>(plugins.length);
for (AnnotationAttributes plugin : plugins) {
Class extends Interceptor> pluginClass &#61; plugin.getClass("value");
AnnotationAttributes[] params &#61; plugin.getAnnotationArray("params");
clazz.add(pluginClass);
Interceptor interceptor &#61; BeanUtils.instantiate(pluginClass);
BeanWrapper beanWrapper &#61; new BeanWrapperImpl(interceptor);
for (AnnotationAttributes param : params) {
String key &#61; param.getString("key");
String value &#61; param.getString("value");
PropertyDescriptor descriptor &#61; beanWrapper.getPropertyDescriptor(key);
Method writeMethod &#61; descriptor.getWriteMethod();
Method readMethod &#61; descriptor.getReadMethod();
writeMethod.setAccessible(true);
try {
Class> returnType &#61; readMethod.getReturnType();
Object valueObject &#61; value;
if (Integer.class.equals(returnType) || int.class.equals(returnType)) {
valueObject &#61; Integer.valueOf(value);
} else if (Long.class.equals(returnType) || long.class.equals(returnType)) {
valueObject &#61; Long.valueOf(value);
} else if (Boolean.class.equals(returnType) || boolean.class.equals(returnType)) {
valueObject &#61; Boolean.valueOf(value);
} else if (Double.class.equals(returnType) || double.class.equals(returnType)) {
valueObject &#61; Double.valueOf(value);
}
writeMethod.invoke(interceptor, valueObject);
} catch (Exception e) {
e.printStackTrace();
}
}
pluginsList.add(interceptor);
}
if (!clazz.contains(PaginationInterceptor.class)) {
pluginsList.add(BeanUtils.instantiate(PaginationInterceptor.class));
}
sqlSessionFactoryBean.addPropertyValue("plugins", pluginsList);
BeanDefinitionBuilder scannerConfigurerBean &#61; BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class);
scannerConfigurerBean.addPropertyValue("basePackage", StringUtils.join(basePackages, ","));
scannerConfigurerBean.addPropertyValue("annotationClass", Repository.class);
scannerConfigurerBean.addPropertyValue("sqlSessionFactoryBeanName", "sqlSessionFactory");
beanDefinitions.put("sqlSessionFactory", sqlSessionFactoryBean.getBeanDefinition());
beanDefinitions.put("mapperScannerConfigurer", scannerConfigurerBean.getBeanDefinition());
return beanDefinitions;
}