交代: SqlSession 作为MyBatis工作的主要顶层API,表示和数据库交互的会话,完成必要数据库增删改查功能。里面主要有两个参数,一个是configuration另一个是executor。其中Configuration就像一个大管家,承载了所有的配置文件的信息,例如
Map
,mappedStatements Map
,``MapresultMaps parameterMaps`等;还有一个是executor,执行crud用的。其中mappedStatements很重要,因为xml文件里的crud标签的sql语句都会封装到这个对象中。
所用版本为:
Group ID: org.mybatis Artifact ID: mybatis-spring Version: 2.0.2
Group ID: org.mybatis Artifact ID: mybatis Version: 3.5.2
1.文件入口
通常我们会将@MapperScan("xxx"),写到SpringBoot的主函数入口,就是为了能够扫描到包
@SpringBootApplication
@MapperScan("com.runchuang.liangpi.dao")
public class LiangpiApplication {
public static void main(String[] args) {
SpringApplication.run(LiangpiApplication.class, args);
}
}
2.MapperScan注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(MapperScannerRegistrar.class)
@Repeatable(MapperScans.class)
public @interface MapperScan {
我们可以看到Import这个注解,其实就是和@Bean这种注解差不多,可以将括号里的类,加载到Bean容器中;这里将MapperScannerRegistrar
加到了容器中,这个类主要负责扫描接口,然后偷天换日将本应该属于接口的BeanDefinition的class,换成MapperFactoryBean,这个类就是最终就能通过getObject来获取真正的要执行的代理类。
3.MapperScannerRegistrar
public class MapperScannerRegistrar implements ImportBeanDefinitionRegistrar{
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AnnotationAttributes mapperScanAttrs = AnnotationAttributes
.fromMap(importingClassMetadata.getAnnotationAttributes(MapperScan.class.getName()));
if (mapperScanAttrs != null) {
registerBeanDefinitions(mapperScanAttrs, registry, generateBaseBeanName(importingClassMetadata, 0));
}
}
}
获取MapperScann注解做标注的类,然后获取他写的属性名字,例如我这里MapperScan写了"com.runchuang.liangpi.dao"
4.registerBeanDefinitions
用一个BeanDefinitionBuilder来构造关于MapperScannerConfigurer的BeanDefinition,获取扫描的包名,然后设置到BeanDefinition的属性中。然后动态注册到Bean的容器中。
void registerBeanDefinitions(AnnotationAttributes annoAttrs, BeanDefinitionRegistry registry, String beanName) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class);
...
builder.addPropertyValue("basePackage", StringUtils.collectionToCommaDelimitedString(basePackages));
registry.registerBeanDefinition(beanName, builder.getBeanDefinition());
}
5.接下来我们看看MapperConfigurer
public class MapperScannerConfigurer
implements BeanDefinitionRegistryPostProcessor, InitializingBean, ApplicationContextAware, BeanNameAware {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
if (this.processPropertyPlaceHolders) {
processPropertyPlaceHolders();
}
ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
....
scanner.scan(
StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
}
}
由于实现了BeanDefinitionRegistryPostPostProcesser这个后置处理器,所以在实例化过程中,做了包扫描注册以及jdk反向代理。
6.这时候会执行scan方法
public int scan(String... basePackages) {
int beanCountAtScanStart = this.registry.getBeanDefinitionCount();
doScan(basePackages);
// Register annotation config processors, if necessary.
if (this.includeAnnotationConfig) {
AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
}
return (this.registry.getBeanDefinitionCount() - beanCountAtScanStart);
}
这个doScan方法并不是调用自己本类的doScan方法,而是调用父类ClassPathMapperScanner方法
public class ClassPathMapperScanner extends ClassPathBeanDefinitionScanner {`
@Override
public Set doScan(String... basePackages) {
Set beanDefinitions = super.doScan(basePackages);
if (beanDefinitions.isEmpty()) {
LOGGER.warn(() -> "No MyBatis mapper was found in '" + Arrays.toString(basePackages)
+ "' package. Please check your configuration.");
} else {
processBeanDefinitions(beanDefinitions);
}
return beanDefinitions;
}
}
第一个是扫描包,将接口的注册成BeanDefinitionHolder,但是这个BeanDefinition肯定不能实例化啦,因为是接口嘛,然后将这个Set集合的BeanDefinitions处理然后注册,这里就涉及了偷天换日
7.偷天换日
private void processBeanDefinitions(Set beanDefinitions) {
GenericBeanDefinition definition;
for (BeanDefinitionHolder holder : beanDefinitions) {
definition = (GenericBeanDefinition) holder.getBeanDefinition();
String beanClassName = definition.getBeanClassName();
// the mapper interface is the original class of the bean
// but, the actual class of the bean is MapperFactoryBean
definition.getConstructorArgumentValues().addGenericArgumentValue(beanClassName); // issue #59
definition.setBeanClass(this.mapperFactoryBeanClass);
....省略...
if (!explicitFactoryUsed) {
definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
}
definition.setLazyInit(lazyInitialization);
}
}
这里将传进来的beanDefinitions的class全部换掉换成MapperFactoryBean,自此,这个Bean的实例化不再是接口,而是MapperFactoryBean,这个Bean还是一个FactoryBean,会在获取真正的bean的时候,通过getObject去获取。
8.MapperFactoryBean
@Override
public T getObject() throws Exception {
return getSqlSession().getMapper(this.mapperInterface);
}
在这里调用的是MapperRegistry的getMapper方法
public T getMapper(Class type, SqlSession sqlSession) {
final MapperProxyFactory mapperProxyFactory = (MapperProxyFactory) knownMappers.get(type);
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
}
try {
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
}
}
注意一个问题这个mapperProxyFactory.newInstance(sqlSession);就是调用的动态代理了。
9.动态代理的调用。
protected T newInstance(MapperProxy mapperProxy) {
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
public T newInstance(SqlSession sqlSession) {
final MapperProxy mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}
10.代理类MapperProxy实现了InvocationHandler
public class MapperProxy implements InvocationHandler, Serializable {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
final MapperMethod mapperMethod = cachedMapperMethod(method);
return mapperMethod.execute(sqlSession, args);
}
}
11.执行方法
public class MapperMethod {
private final SqlCommand command;
private final MethodSignature method;
public MapperMethod(Class> mapperInterface, Method method, Configuration config) {
this.command = new SqlCommand(config, mapperInterface, method);
this.method = new MethodSignature(config, mapperInterface, method);
}
public Object execute(SqlSession sqlSession, Object[] args) {
Object result;
switch (command.getType()) {
case INSERT: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.insert(command.getName(), param));
break;
}
case UPDATE: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.update(command.getName(), param));
break;
}
case DELETE: {
Object param = method.convertArgsToSqlCommandParam(args);
result = rowCountResult(sqlSession.delete(command.getName(), param));
break;
}
……
12.到底在哪里装配的Configuration呢?
其实是在MapperFactoryBean他的父类里,DaoSupport,他实现了InitializingBean,所以在初始化之后,实现了afterPropertiesSet方法
@Override
public final void afterPropertiesSet() throws IllegalArgumentException, BeanInitializationException {
// Let abstract subclasses check their configuration.
checkDaoConfig();
// Let concrete implementations initialize themselves.
try {
initDao();
}
catch (Exception ex) {
throw new BeanInitializationException("Initialization of DAO failed", ex);
}
}
主要在checkDaoConfig();配置了Configuration;
具体实现在MapperFactoryBean里
public class MapperFactoryBean extends SqlSessionDaoSupport implements FactoryBean {
private Class mapperInterface;
private boolean addToConfig = true;
public MapperFactoryBean() {
// intentionally empty
}
public MapperFactoryBean(Class mapperInterface) {
this.mapperInterface = mapperInterface;
}
/**
* {@inheritDoc}
*/
@Override
protected void checkDaoConfig() {
super.checkDaoConfig();
notNull(this.mapperInterface, "Property 'mapperInterface' is required");
Configuration configuration = getSqlSession().getConfiguration();
if (this.addToConfig && !configuration.hasMapper(this.mapperInterface)) {
try {
configuration.addMapper(this.mapperInterface);
}
}
}
addMapper具体实现,并且将接口类型穿进来了,以便jdk动态代理。
public void addMapper(Class type) {
if (type.isInterface()) {
if (hasMapper(type)) {
throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
}
boolean loadCompleted = false;
try {
knownMappers.put(type, new MapperProxyFactory<>(type));
MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
parser.parse();
loadCompleted = true;
} finally {
if (!loadCompleted) {
knownMappers.remove(type);
}
}
}
}