Mybatis源码学习(三)基于@Mapper注解

发布时间:2022-06-21 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Mybatis源码学习(三)基于@Mapper注解脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

在上面的例子中,我们使用xml加载Mapper文件,在这一篇博客中,我们使用@Mapper注解加载sql映射

1 示例

我们修改Mybatis源码学习(一)中的代码,红色为修改部分:

MyBatisMain.java

public class MybatisMain {
  public static void main(String[] args) throws IOException
  {
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    SqlSession sqlSession = sqlSessionFactory.openSession();
    BlogMapper blogMapper = sqlSession.getMapper(BlogMapper.class);
    Blog blog = blogMapper.query(101);
//    Blog blog = sqlSession.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);
    System.out.println(blog.getContext());
  }
}

新增接口BlogMapper.java

@Mapper
public interface BlogMapper {
  @Select("select * from blog where id=#{id}")
  Blog query(Integer id);
}

mybatis-config.xml

<configuration>
    <properties resource="jdbc.properties"/>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--<mapper resource="mapper/BlogMapper.xml"/>-->
        <mapper class="org.apache.ibatis.demo.mapper.BlogMapper"/>
    </mappers>
</configuration>

 

2 分析

在 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 中,build方法中经过若干步骤,执行如下方法

  private void mapperElement(XNode parent) throws Exception {
    if (parent != null) {
      for (XNode child : parent.getChildren()) {
        if ("package".equals(child.getName())) {
          String mapperPackage = child.getStringAttribute("name");
          configuration.addMappers(mapperPackage);
        } else {
          String resource = child.getStringAttribute("resource");
          String url = child.getStringAttribute("url");
          String mapperClass = child.getStringAttribute("class");
          if (resource != null && url == null && mapperClass == null) {
            ErrorContext.instance().resource(resource);
            try(InputStream inputStream = Resources.getResourceAsStream(resource)) {
              XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments());
              mapperParser.parse();
            }
          } else if (resource == null && url != null && mapperClass == null) {
            ErrorContext.instance().resource(url);
            try(InputStream inputStream = Resources.getUrlAsStream(url)){
              XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments());
              mapperParser.parse();
            }
          } else if (resource == null && url == null && mapperClass != null) {
            Class<?> mapperInterface = Resources.classForName(mapperClass);
            configuration.addMapper(mapperInterface);
          } else {
            throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
          }
        }
      }
    }
  }

当使用注解方式时,执行如下分支,configuration中添加Mapper接口对应的Class。

else if (resource == null && url == null && mapperClass != null) {
            Class<?> mapperInterface = Resources.classForName(mapperClass);
            configuration.addMapper(mapperInterface);
}

 

暂时先记录这些,后面随着理解的深入会充实该博客。

 

脚本宝典总结

以上是脚本宝典为你收集整理的Mybatis源码学习(三)基于@Mapper注解全部内容,希望文章能够帮你解决Mybatis源码学习(三)基于@Mapper注解所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: