MyBatis實現(xiàn)注冊及獲取Mapper
一、搭建環(huán)境
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) {
//創(chuàng)建數(shù)據(jù)源
DataSource dataSource = getDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
//創(chuàng)建sql運行環(huán)境
Environment environment = new Environment("development", transactionFactory, dataSource);
//創(chuàng)建mybatis的所有配置
Configuration configuration = new Configuration(environment);
//注冊mapper
configuration.addMapper(BlogMapper.class);
// configuration.addInterceptor(new PaginationInterceptor());
//根據(jù)配置創(chuàng)建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詳細(xì)分析
2.1 MapperRegistry

這塊就是判斷這個mapper.xml解析過沒有,解析是在 parser.parse();中做的,來看

loadXmlResource();根據(jù)xml解析每個mapper接口的方法,將得到的MapperStatement放進(jìn)了configuration,然后記錄該xml的namespace表示已經(jīng)處理過。具體調(diào)用鏈:
loadXmlResource()–>xmlParser.parse()–>configurationElement(parser.evalNode("/mapper"))–> buildStatementFromContext(context.evalNodes(“select|insert|update|delete”))–> buildStatementFromContext(list, null)–>statementParser.parseStatementNode()–>builderAssistant.addMappedStatement–>configuration.addMappedStatement(statement);

parseStatement(method);根據(jù)注解解析每個mapper接口的方法,因此xml和注解可以同時使用。但是同一個方法兩者同時使用會報錯

2.2 MapperProxyFactory

放入knownMappers的是MapperProxyFactory,它是一個Mapper代理的工廠,這個工廠提供newInstance方法,產(chǎn)生一個代理類(也就是BlogMapper接口的代理實現(xiàn)類),調(diào)用BlogMapper所有的方法將在MapperProxy的invoke方法中執(zhí)行
三、getMapper詳細(xì)分析
getMapper會調(diào)用MapperRegistry的getMapper從knownMappers中獲取代理工廠,再調(diào)用newInstance方法產(chǎn)生一個代理類MapperProxy。

3.1 MapperProxy
在執(zhí)行mapper.selectBlog(“001”)時,就會調(diào)用MapperProxy的invoke方法

根據(jù)method(selectBlog)生成對應(yīng)的MapperMethod,并將MapperMethod放入本地緩存。
mapperMethod.execute(sqlSession, args);執(zhí)行真正的sql邏輯。
3.2 MapperMethod

MapperMethod的構(gòu)造方法,根據(jù)接口信息、方法信息、配置信息得到SqlCommand(sql名稱、類型)、method(方法簽名),方便后續(xù)執(zhí)行命令、處理結(jié)果集等。
到此這篇關(guān)于MyBatis實現(xiàn)注冊及獲取Mapper的文章就介紹到這了,更多相關(guān)MyBatis 注冊及獲取Mapper內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Servlet+MyBatis項目轉(zhuǎn)Spring Cloud微服務(wù),多數(shù)據(jù)源配置修改建議
- SpringBoot+Mybatis使用Mapper接口注冊的幾種方式
- SpringBoot+Mybatis實現(xiàn)登錄注冊的示例代碼
- IDEA下創(chuàng)建SpringBoot+MyBatis+MySql項目實現(xiàn)動態(tài)登錄與注冊功能
- Spring boot+mybatis+thymeleaf 實現(xiàn)登錄注冊增刪改查功能的示例代碼
- Spring MVC+mybatis實現(xiàn)注冊登錄功能
- Mybatis與微服務(wù)注冊的詳細(xì)過程
相關(guān)文章
SpringBoot中的CompletableFuture類詳解
這篇文章主要介紹了SpringBoot中的CompletableFuture類詳解,在?Java8中,引入了CompletableFuture類,它提供了一種簡單而強大的方式來執(zhí)行異步任務(wù),今天我們就來詳細(xì)解讀一下這個類,需要的朋友可以參考下2023-07-07
MybatisPlus多條件?or()的使用問題小結(jié)
這篇文章主要介紹了MybatisPlus多條件?or()的使用問題小結(jié),本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-05-05
利用Spring?Boot和JPA創(chuàng)建GraphQL?API
這篇文章主要介紹了利用Spring?Boot和JPA創(chuàng)建GraphQL?API,GraphQL既是API查詢語言,也是使用當(dāng)前數(shù)據(jù)執(zhí)行這些查詢的運行時,下文更多相關(guān)內(nèi)容介紹需要的小伙伴可以參考一下2022-04-04
SpringBoot實現(xiàn)在webapp下直接訪問html,jsp
這篇文章主要介紹了SpringBoot實現(xiàn)在webapp下直接訪問html,jsp問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
SpringBoot+Mybatis使用Enum枚舉類型總是報錯No enum constant&n
這篇文章主要介紹了SpringBoot+Mybatis使用Enum枚舉類型總是報錯No enum constant XX問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
JAVA線程池監(jiān)控以及動態(tài)調(diào)整示例詳解
這篇文章主要為大家介紹了JAVA線程池監(jiān)控以及動態(tài)調(diào)整示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
SpringBoot實現(xiàn)服務(wù)接入nacos注冊中心流程詳解
這篇文章主要介紹了SpringBoot實現(xiàn)服務(wù)接入nacos注冊中心流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01

