编程语言
首页 > 编程语言> > spring源码二.与mybatis整合和流程简介

spring源码二.与mybatis整合和流程简介

作者:互联网

一.数据库表

-- 建表语句 

     -- SHOW CREATE TABLE  t_emp;

CREATE TABLE `t_emp` (

  `id` INT(11) NOT NULL AUTO_INCREMENT,

  `name` VARCHAR(20) DEFAULT NULL,

  `age` INT(3) DEFAULT NULL,

  `deptId` INT(11) DEFAULT NULL,

  `empno` INT(11) NOT NULL,

  PRIMARY KEY (`id`),

  KEY `idx_dept_id` (`deptId`)

) ENGINE=INNODB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8

二.整合过程中用到的依赖包:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.12.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.12.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.5</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.1</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
</dependency>

 

三. 整合

 

注意:@Select注解里面包含参数查询时,用法可参考mybatis官网 mybatis – MyBatis 3 | Getting started

四.执行结果

 

 

五.原理学习

  在myabtis-spring 老版本中:

  

上者的执行时间与spring的执行内置BeanDefinitionRegistryPostProcessor实现类ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry中一致

在mybatis-spring 2.0.2之后:

  

它是在工厂中注册了一个MapperScannerConfigurer的bdrpp来实现的,执行时机往后延期了!
本质上这里是使用到了动态代理产生了代理对象,去完成XXDaoImpl对数据库的查询!  

@MapperScan("com.shadow.wang.dao"), 解析此注解,导入一个MapperScannerRegistrar.class在spring中,然后在去扫描包,解析生成bd,实例化对象的!!!

 

 

 

 

 

标签:version,spring,dependency,源码,mybatis,groupId,artifactId
来源: https://blog.csdn.net/ccwangwang/article/details/120382097