温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MyBatis如何实现注册及获取Mapper

发布时间:2022-03-30 09:00:25 来源:亿速云 阅读:422 作者:小新 栏目:开发技术

这篇文章主要为大家展示了“MyBatis如何实现注册及获取Mapper”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“MyBatis如何实现注册及获取Mapper”这篇文章吧。

一、搭建环境

1.1 pom.xml

 <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>

1.2 BlogMapper.java

public interface BlogMapper {
    List<Blog> selectBlog(String id);
}

1.3 BlogMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mybatis.source.study.BlogMapper">
    <select id="selectBlog" resultType="mybatis.source.study.Blog">
    select * from t_blog where id= #{id}
  </select>
</mapper>

BlogMapper.xml放在resource目录下与BlogMapper.java包路径相同的路径下

1.4 MyBatisDemo.java

public class MyBatisDemo {
    public static void main(String[] args) {
    	//创建数据源
        DataSource dataSource = getDataSource();
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        //创建sql运行环境
        Environment environment = new Environment("development", transactionFactory, dataSource);
        //创建mybatis的所有配置
        Configuration configuration = new Configuration(environment);
        //注册mapper
        configuration.addMapper(BlogMapper.class);
//        configuration.addInterceptor(new PaginationInterceptor());
		//根据配置创建sql会话工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        System.out.println(mapper.selectBlog("001"));
    }

    private static DataSource getDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8&serverTimezone=Asia/Shanghai");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("root");
        return druidDataSource;
    }

二、addMapper详细分析

2.1 MapperRegistry

MyBatis如何实现注册及获取Mapper

这块就是判断这个mapper.xml解析过没有,解析是在 parser.parse();中做的,来看

MyBatis如何实现注册及获取Mapper

loadXmlResource();根据xml解析每个mapper接口的方法,将得到的MapperStatement放进了configuration,然后记录该xml的namespace表示已经处理过。具体调用链:

loadXmlResource()&ndash;>xmlParser.parse()&ndash;>configurationElement(parser.evalNode("/mapper"))&ndash;> buildStatementFromContext(context.evalNodes(“select|insert|update|delete”))&ndash;> buildStatementFromContext(list, null)&ndash;>statementParser.parseStatementNode()&ndash;>builderAssistant.addMappedStatement&ndash;>configuration.addMappedStatement(statement);

MyBatis如何实现注册及获取Mapper

parseStatement(method);根据注解解析每个mapper接口的方法,因此xml和注解可以同时使用。但是同一个方法两者同时使用会报错

MyBatis如何实现注册及获取Mapper

2.2 MapperProxyFactory

MyBatis如何实现注册及获取Mapper

放入knownMappers的是MapperProxyFactory,它是一个Mapper代理的工厂,这个工厂提供newInstance方法,产生一个代理类(也就是BlogMapper接口的代理实现类),调用BlogMapper所有的方法将在MapperProxy的invoke方法中执行

三、getMapper详细分析

getMapper会调用MapperRegistry的getMapper从knownMappers中获取代理工厂,再调用newInstance方法产生一个代理类MapperProxy。

MyBatis如何实现注册及获取Mapper

3.1 MapperProxy

在执行mapper.selectBlog(“001”)时,就会调用MapperProxy的invoke方法

MyBatis如何实现注册及获取Mapper

根据method(selectBlog)生成对应的MapperMethod,并将MapperMethod放入本地缓存。
mapperMethod.execute(sqlSession, args);执行真正的sql逻辑。

3.2 MapperMethod

MyBatis如何实现注册及获取Mapper

MapperMethod的构造方法,根据接口信息、方法信息、配置信息得到SqlCommand(sql名称、类型)、method(方法签名),方便后续执行命令、处理结果集等。

以上是“MyBatis如何实现注册及获取Mapper”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI