MapperScannerConfigurer处理过程源码分析

2023-12-23 16:58

本文主要是介绍MapperScannerConfigurer处理过程源码分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


原文地址https://www.cnblogs.com/fangjian0423/p/spring-mybatis-MapperScannerConfigurer-analysis.html

原文地址https://www.cnblogs.com/zhuxiaojie/p/5836159.html

MapperScannerConfigurer介绍

MapperScannerConfigurer是spring和mybatis整合的mybatis-spring jar包中提供的一个类。

想要了解该类的作用,就得先了解MapperFactoryBean。

MapperFactoryBean的出现为了代替手工使用SqlSessionDaoSupport或SqlSessionTemplate编写数据访问对象(DAO)的代码,使用动态代理实现。

比如下面这个官方文档中的配置:

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"><property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" /><property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

org.mybatis.spring.sample.mapper.UserMapper是一个接口,我们创建一个MapperFactoryBean实例,然后注入这个接口和sqlSessionFactory(mybatis中提供的SqlSessionFactory接口,MapperFactoryBean会使用SqlSessionFactory创建SqlSession)这两个属性。

之后想使用这个UserMapper接口的话,直接通过spring注入这个bean,然后就可以直接使用了,spring内部会创建一个这个接口的动态代理。

当发现要使用多个MapperFactoryBean的时候,一个一个定义肯定非常麻烦,于是mybatis-spring提供了MapperScannerConfigurer这个类,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBean。

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="org.mybatis.spring.sample.mapper" />
</bean>

这段配置会扫描org.mybatis.spring.sample.mapper下的所有接口,然后创建各自接口的动态代理类。

MapperScannerConfigurer底层代码分析

以以下代码为示例进行讲解(部分代码,其他代码及配置省略):

package org.format.dynamicproxy.mybatis.dao;
public interface UserDao {public User getById(int id);public int add(User user);    public int update(User user);    public int delete(User user);    public List<User> getAll();    
}<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="org.format.dynamicproxy.mybatis.dao"/><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

我们先通过测试用例debug查看userDao的实现类到底是什么。

我们可以看到,userDao是1个MapperProxy类的实例。
看下MapperProxy的源码,没错,实现了InvocationHandler,说明使用了jdk自带的动态代理。

public class MapperProxy<T> implements InvocationHandler, Serializable {private static final long serialVersionUID = -6424540398559729838L;private final SqlSession sqlSession;private final Class<T> mapperInterface;private final Map<Method, MapperMethod> methodCache;public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {this.sqlSession = sqlSession;this.mapperInterface = mapperInterface;this.methodCache = methodCache;}public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {if (Object.class.equals(method.getDeclaringClass())) {try {return method.invoke(this, args);} catch (Throwable t) {throw ExceptionUtil.unwrapThrowable(t);}}final MapperMethod mapperMethod = cachedMapperMethod(method);return mapperMethod.execute(sqlSession, args);}private MapperMethod cachedMapperMethod(Method method) {MapperMethod mapperMethod = methodCache.get(method);if (mapperMethod == null) {mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());methodCache.put(method, mapperMethod);}return mapperMethod;}}

下面开始分析MapperScannerConfigurer的源码

MapperScannerConfigurer实现了BeanDefinitionRegistryPostProcessor接口,BeanDefinitionRegistryPostProcessor接口是一个可以修改spring工长中已定义的bean的接口,该接口有个postProcessBeanDefinitionRegistry方法。

然后我们看下ClassPathMapperScanner中的关键是如何扫描对应package下的接口的。

其实MapperScannerConfigurer的作用也就是将对应的接口的类型改造为MapperFactoryBean,而这个MapperFactoryBean的属性mapperInterface是原类型。MapperFactoryBean本文开头已分析过。

所以最终我们还是要分析MapperFactoryBean的实现原理!

MapperFactoryBean继承了SqlSessionDaoSupport类,SqlSessionDaoSupport类继承DaoSupport抽象类,DaoSupport抽象类实现了InitializingBean接口,因此实例个MapperFactoryBean的时候,都会调用InitializingBean接口的afterPropertiesSet方法。

DaoSupport的afterPropertiesSet方法:

MapperFactoryBean重写了checkDaoConfig方法:

然后通过spring工厂拿对应的bean的时候:

这里的SqlSession是SqlSessionTemplate,SqlSessionTemplate的getMapper方法:

Configuration的getMapper方法,会使用MapperRegistry的getMapper方法:

MapperRegistry的getMapper方法:

MapperProxyFactory构造MapperProxy:

没错! MapperProxyFactory就是使用了jdk组带的Proxy完成动态代理。
MapperProxy本来一开始已经提到。MapperProxy内部使用了MapperMethod类完成方法的调用:

下面,我们以UserDao的getById方法来debug看看MapperMethod的execute方法是如何走的。

@Test
public void testGet() {int id = 1;System.out.println(userDao.getById(id));
}
<select id="getById" parameterType="int" resultType="org.format.dynamicproxy.mybatis.bean.User">SELECT * FROM users WHERE id = #{id}
</select>


示例代码:https://github.com/fangjian0423/dynamic-proxy-mybatis-study


SpringBoot整合MyBatis


方法一:多数据源的mytis

强烈推荐:只要一行注解,使用mybatis的多数据源,无需要任何配置

 

github地址:https://gitee.com/xiaojiezhu/mybadis-starter.git

使用方法如下,先加入如下配置文件,在yaml中

复制代码
mysql:server:saas:url: "jdbc:mysql://localhost:3306/saas?useUnicode=true&characterEncoding=utf8"username: rootpassword: 123driverClassName: com.mysql.jdbc.DriverinitialSize: 1  #初始化大小minIdle: 1  #空闲连接池的大小maxActive: 50 #最大激活数量saas2:url: "jdbc:mysql://localhost:3306/saas2?useUnicode=true&characterEncoding=utf8"username: rootpassword: 123driverClassName: com.mysql.jdbc.DriverinitialSize: 1  #初始化大小minIdle: 1  #空闲连接池的大小maxActive: 50 #最大激活数量
复制代码

如上是定义了两个数据源

然后在main方法所在类加上此注解

 

复制代码
@MyBadisLoader({"saas = com.llc.admin.web.dao.saas = classpath:mapper/*xml" , "saas2 = com.llc.admin.web.dao.saas2 = classpath:mapper/*.xml,classpath:mapper/user/*.xml"})
@SpringBootApplication
public class WebApplication {public static void main(String[] args) {SpringApplication.run(WebApplication.class,args);}
}
复制代码

 

上面的注解中 saas是上方配置文件,数据源的名称,后面是扫描的接口包名,可以用逗号分隔传入多个,再后面是扫描xml的配置文件路径,也支持多个 注解中接收的是一个数组,所以支持多个数据源,除此不需要任何代码就可以使用

 

就可以直接支持多数据源

 

下面方法二是使用mybatis官方的starter,需要经过一些配置才行

 

 

 

 

 

 

 

 

方法二:使用mybatis官方starter

首先加入mybatis-spring-boot-stater的Maven依赖

复制代码
        <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.1.1</version></dependency>
复制代码

 

 

配置数据源,这里使用的dbcp的数据源,具体大家可以看自己的情况来使用

在src/main/resource中,添加一个prop.properties配置文件,这里面添加了一些数据库连接的信息

复制代码
#jdbc
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.137.2:3306/weichat?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
jdbc.maxActive=2335
jdbc.maxIdel=120
jdbc.maxWait=100
复制代码

 

然后加上下面的代码注入数据源

复制代码
@Configuration
//这个注解导入刚才增加的jdbc配置文件
@PropertySource("classpath:prop.properties")
public class DataSourceConfiguration {@Value("${jdbc.driver}")private String driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;@Value("${jdbc.maxActive}")private int maxActive;@Value("${jdbc.maxIdel}")private int maxIdel;@Value("${jdbc.maxWait}")private long maxWait;@Beanpublic BasicDataSource dataSource(){BasicDataSource dataSource = new BasicDataSource();dataSource.setDriverClassName(driver);dataSource.setUrl(url);dataSource.setUsername(username);dataSource.setPassword(password);dataSource.setMaxActive(maxActive);dataSource.setMaxIdle(maxIdel);dataSource.setMaxWait(maxWait);dataSource.setValidationQuery("SELECT 1");dataSource.setTestOnBorrow(true);return dataSource;}
}
复制代码

 

 

 

增加MyBatis的配置

复制代码
import javax.sql.DataSource;import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;/*** @author zxj**/
@Configuration
//加上这个注解,使得支持事务
@EnableTransactionManagement
public class MyBatisConfig implements TransactionManagementConfigurer {@Autowiredprivate DataSource dataSource;@Overridepublic PlatformTransactionManager annotationDrivenTransactionManager() {return new DataSourceTransactionManager(dataSource);}@Bean(name = "sqlSessionFactory")public SqlSessionFactory sqlSessionFactoryBean() {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);try {return bean.getObject();} catch (Exception e) {e.printStackTrace();throw new RuntimeException(e);}}@Beanpublic SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {return new SqlSessionTemplate(sqlSessionFactory);}
}
复制代码

 

 

然后需要配置MyBatis配置文件的路径,这个配置需要与上面的配置分开来写,因为它们有着一个先后顺序

复制代码
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 扫描mybatis的接口* * @author zxj**/
@Configuration
// 因为这个对象的扫描,需要在MyBatisConfig的后面注入,所以加上下面的注解
@AutoConfigureAfter(MyBatisConfig.class)
public class MyBatisMapperScannerConfig {@Beanpublic MapperScannerConfigurer mapperScannerConfigurer() {MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();//获取之前注入的beanName为sqlSessionFactory的对象mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");//指定xml配置文件的路径mapperScannerConfigurer.setBasePackage("com.framework.msg.mapper");return mapperScannerConfigurer;}
}
复制代码

 

 

然后这就是配置完了,真的很简单,但是细心的朋友可能会问,代码里面怎么没有配置MyBatis接口的地址呢?

 

在这里,使用@Mapper注解来标识一个接口为MyBatis的接口,MyBatis会自动寻找这个接口,如下

复制代码
import java.util.Map;import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;@Mapper
public interface TestDao {@Select("select * from wx_userinfo;")public Map<String,Object> find();@Insert("insert into wx_userinfo(openid,status,nickname,sex,city,province,country,headimgurl,subscribe_time) "+"values(#{id},1,'nick',1,'city','provi','contr','img',now())")public int insert(@Param("id")int id);
}
复制代码

 

这样就可以使用了,当然,在这之前,你得开启@ComponentScan注解,或者直接使用@SpringBootApplication(推荐)


这篇关于MapperScannerConfigurer处理过程源码分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/528749

相关文章

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

go中的时间处理过程

《go中的时间处理过程》:本文主要介绍go中的时间处理过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 获取当前时间2 获取当前时间戳3 获取当前时间的字符串格式4 相互转化4.1 时间戳转时间字符串 (int64 > string)4.2 时间字符串转时间

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

python中Hash使用场景分析

《python中Hash使用场景分析》Python的hash()函数用于获取对象哈希值,常用于字典和集合,不可变类型可哈希,可变类型不可,常见算法包括除法、乘法、平方取中和随机数哈希,各有优缺点,需根... 目录python中的 Hash除法哈希算法乘法哈希算法平方取中法随机数哈希算法小结在Python中,

Java Stream的distinct去重原理分析

《JavaStream的distinct去重原理分析》Javastream中的distinct方法用于去除流中的重复元素,它返回一个包含过滤后唯一元素的新流,该方法会根据元素的hashcode和eq... 目录一、distinct 的基础用法与核心特性二、distinct 的底层实现原理1. 顺序流中的去重

关于MyISAM和InnoDB对比分析

《关于MyISAM和InnoDB对比分析》:本文主要介绍关于MyISAM和InnoDB对比分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录开篇:从交通规则看存储引擎选择理解存储引擎的基本概念技术原理对比1. 事务支持:ACID的守护者2. 锁机制:并发控制的艺

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛