服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - MyBatis实现注册及获取Mapper

MyBatis实现注册及获取Mapper

2022-10-28 11:38Integer_Double Java教程

本文主要介绍了MyBatis实现注册及获取Mapper,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、搭建环境

1.1 pom.xml

?
1
2
3
4
5
6
7
8
<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

?
1
2
3
public interface BlogMapper {
    List<Blog> selectBlog(String id);
}

1.3 BlogMapper.xml

?
1
2
3
4
5
6
7
8
9
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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()–>xmlParser.parse()–>configurationElement(parser.evalNode("/mapper"))–> buildStatementFromContext(context.evalNodes(“select|insert|update|delete”))–> buildStatementFromContext(list, null)–>statementParser.parseStatementNode()–>builderAssistant.addMappedStatement–>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的文章就介绍到这了,更多相关MyBatis 注册及获取Mapper内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/yx444535180/article/details/123736478

延伸 · 阅读

精彩推荐