Mybatis-tips

Mybatis-tips

MyBatis-Plus Service CRUD 常用方法 Demo_O夫子的博客-CSDN博客


BindingException: Invalid bound statement (not found): com.*..Mapper.get源码分析_码生剑气的博客-CSDN博客

具体的映射方法的查找,在 MapperMethod

public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) {
  final String methodName = method.getName();
  final Class<?> declaringClass = method.getDeclaringClass();
  MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass,
      configuration);
  if (ms == null) {
    if (method.getAnnotation(Flush.class) != null) {
      name = null;
      type = SqlCommandType.FLUSH;
    } else {
      throw new BindingException("Invalid bound statement (not found): "
          + mapperInterface.getName() + "." + methodName);
    }
  } else {
    name = ms.getId();
    type = ms.getSqlCommandType();
    if (type == SqlCommandType.UNKNOWN) {
      throw new BindingException("Unknown execution method for: " + name);
    }
  }
}
private MappedStatement resolveMappedStatement(Class<?> mapperInterface, String methodName,
    Class<?> declaringClass, Configuration configuration) {
  String statementId = mapperInterface.getName() + "." + methodName;
  // 所有的mapper中的方法,都会添加到 configuration 的 mybatisMapperRegistry 的 knownMappers 属性中
  // 然后每一个mapper中的方法,会添加到 configuration 的 mappedStatements 中
  if (configuration.hasStatement(statementId)) {
    return configuration.getMappedStatement(statementId);
  } else if (mapperInterface.equals(declaringClass)) {
    return null;
  }
  for (Class<?> superInterface : mapperInterface.getInterfaces()) {
    if (declaringClass.isAssignableFrom(superInterface)) {
      MappedStatement ms = resolveMappedStatement(superInterface, methodName,
          declaringClass, configuration);
      if (ms != null) {
        return ms;
      }
    }
  }
  return null;
}

SpringBoot 启动的时候进行初始化

mybatis 启动执行 org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration#sqlSessionFactory,自动加载 mapper/*.xml 中的文件

启动时在类 SqlSessionFactoryBeanmapperLocations 加断点,查看加载的 xml 文件:

注:这里是 2.0.2.jar 不同版本位置可能不同,找到 org.mybatis.spring.SqlSessionFactoryBean#buildSqlSessionFactory 方法中 mapperLocations 即可

mybatis-plus 启动

执行方法:com.baomidou.mybatisplus.spring.boot.starter.MybatisPlusAutoConfiguration#sqlSessionFactory

如果

@Mapper
public interface DataaggregationTaskDataShareMapper extends BaseMapper<DataaggregationTaskDataShare> {

}

DataaggregationTaskDataShare 没有通过 @TableId 指定主键,则 BaseMapper 中跟 id 相关的 mapper 都不会生效,比如

selectBatchIds
selectById
updateById
deleteBatchIds
deleteById