没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
试读
17页
本文深入分析了MyBatis执行SQL的整个流程,从SqlSessionFactory的创建到SQL语句的执行,揭示了MyBatis工作的内部机制。首先,通过SqlSessionFactoryBuilder,MyBatis解析配置文件生成Configuration对象,并创建SqlSessionFactory。SqlSessionFactory负责生成SqlSession,SqlSession是与数据库交互的主要接口。在SqlSession中,通过动态代理机制获取Mapper对象。这些Mapper对象是通过MapperProxyFactory生成的,负责将接口方法调用转换为SQL语句的执行。 当执行Mapper方法时,MyBatis使用MapperProxy的invoke方法创建或获取缓存的MapperMethod对象。MapperMethod负责判断SQL操作类型(增删改查)并执行相应操作。在此过程中,Executor组件发挥关键作用,它封装了JDBC操作,管理SQL语句的生成和查询缓存的维护。Executor根据配置可分为SimpleExecutor、ReuseExecutor
资源推荐
资源详情
资源评论
Spring整合Mybatis原理
Spring整合Mybatis原理
前言
Mybatis集成Spring:
1.Mybatis集成Spring的适配器源码下载:
2.Spring集成MyBatis
1.SqlSessionFactoryBean
2.Spring是怎么管理Mapper接口的动态代理的
前言
Spring整合MyBatis的原理也是一道非常高频的面试题,下面我们一起来记录一下其中的原理,主要是Spring是怎么管
理MyBatis中的Mapper动态代理的。
Mybatis集成Spring:
1.Mybatis集成Spring的适配器源码下载:
1.https://github.com/mybatis/spring
下载时注意版本:
2.为了在Spring源码中能够看到MyBatis的源码,需要将Mybatis的源码和MyBatis-Spring的源码设置标识名称和添加安
装源码到本地仓库的插件:
安装源码到本地仓库的插件:
1 <plugin>
2 <artifactId>maven‐source‐plugin</artifactId>
3 <version>3.0.1</version>
4 <configuration>
5 <attach>true</attach>
6 </configuration>
7 <executions>
8 <execution>
9 <phase>compile</phase>
10 <goals>
11 <goal>jar</goal>
12 </goals>
13 </execution>
14 </executions>
15 </plugin>
2.Spring集成MyBatis
1.在Spring源码中添加Mybatis集成测试模块:
2.添加gradle依赖:
1
2 dependencies{
3 testCompilegroup:'junit',name:'junit',version:'4.12'
4 compile("mysql:mysql‐connector‐java:5.1.46")
5 compile("com.alibaba:druid:1.1.8")
6 compile("org.mybatis:mybatis‐spring:2.0.3‐xsls")
7 compile("org.mybatis:mybatis:3.5.3‐xsls")
8 compile("org.projectlombok:lombok:1.18.4")
9 compile("com.github.pagehelper:pagehelper:4.1.6")
10 optional(project(":spring‐context"))
11 compile(project(":spring‐jdbc"))
12 compile("ch.qos.logback:logback‐core:1.1.2")
13 compile("ch.qos.logback:logback‐classic:1.1.2")
14 compile("org.slf4j:slf4j‐api:1.7.7")
15 optional(project(":spring‐aop"))
16 compile(project(":spring‐jdbc"))
17 compile("org.mybatis.caches:mybatis‐ehcache:1.1.0")
18 compile("net.sf.ehcache:ehcache‐core:2.6.11")
19 }
3.添加mybatis的测试代码:
请参考提供的项目,这里就不一一放代码了...放上配置类:
1 @EnableTransactionManagement
2 @Configuration
3 @MapperScan(basePackages={"com.tuling.mapper"})
4 @ComponentScan(basePackages={"com.tuling"})
5 @Repository
6 publicclassMyBatisConfig{//=====>spring.xml
7
8
9 /**
10 *<beanclass="com.alibaba.druid.pool.DruidDataSource"id="dataSource"></bean>
11 *
12 *<beanclass="org.mybatis.spring.SqlSessionFactoryBean"id="sqlSessionFactory">
13 *datasource
14 *mapper文件的路径
15 *别名
16 *
17 *</bean>
18 *
19 *<mapper‐scanbasePackage=""/>
20 *@return
21 *@throwsIOException
22 */
23 @Bean//=====><beanclass="org.mybatis.spring.SqlSessionFactoryBean">
24 publicSqlSessionFactoryBeansqlSessionFactory()throwsIOException{
25 SqlSessionFactoryBeanfactoryBean=newSqlSessionFactoryBean();
26 factoryBean.setDataSource(dataSource());
27 //设置MyBatis配置文件路径
28 factoryBean.setConfigLocation(newClassPathResource("mybatis/mybatis‐config.xml"));
29 //设置SQL映射文件路径
30 factoryBean.setMapperLocations(new
PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/*.xml"));
31 factoryBean.setTypeAliases(User.class);
32
33 returnfactoryBean;
34
35
36 }
37
38 publicDataSourcedataSource(){
39 DruidDataSourcedataSource=newDruidDataSource();
40 dataSource.setUsername("root");
41 dataSource.setPassword("123456");
42 dataSource.setDriverClassName("com.mysql.jdbc.Driver");
43 dataSource.setUrl("jdbc:mysql://localhost:3306/mybatis_example");
44 returndataSource;
45 }
1.SqlSessionFactoryBean
1 publicclassSqlSessionFactoryBeanimplementsFactoryBean<SqlSessionFactory>,Initializin
gBean,ApplicationListener<ApplicationEvent>{
实现FactoryBean接口的getObject方法:
1 /**
2 *
3 *将SqlSessionFactory对象注入spring容器
4 *{@inheritDoc}
5 */
6 @Override
7 publicSqlSessionFactorygetObject()throwsException{
8 if(this.sqlSessionFactory==null){
9 afterPropertiesSet();
10 }
11
12 returnthis.sqlSessionFactory;
13 }
SqlSessionFactoryBean实现InitializingBean接口,需要实现其afterPropertiesSet():
1 /**
2 *{@inheritDoc}
3 */
4 /**
5 *方法实现说明:我们自己配置文件中配置了SqlSessionFactoryBean,我们发现配置了该类实现了FactoryB
ean接口,也实现了bean的生命周期回调接口InitializingBean
6 *首先我们会调用生命周期的回调afterPropertiesSet()就是我们的SqlSessionFactorybean已经调用了
构造方法,已经调用了我们的
7 *
8 *@author:xsls
9 *@return:
10 *@exception:
11 *@date:2019/8/2319:33
12 */
13 @Override
14 publicvoidafterPropertiesSet()throwsException{
15 notNull(dataSource,"Property'dataSource'isrequired");
16 notNull(sqlSessionFactoryBuilder,"Property'sqlSessionFactoryBuilder'isrequired");
17 state((configuration==null&&configLocation==null)||!(configuration!=null&&c
onfigLocation!=null),
剩余16页未读,继续阅读
资源评论
- shiweixia2023-12-15感谢支持,内容很好
光芒软件工匠
- 粉丝: 789
- 资源: 64
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功