Spring整合Mybatis思路梳理总结
作者:一切随缘~~~ 时间:2022-04-26 03:56:12
Spring整合Myabtis思路的分析
引入相关依赖
Spring
Myabtis
mysql
Mybatsi-spring
…
如何整合?
Spring:
项目管理框架,主要是用来负责项目中组件对象的创建,使用,销毁。
Mybatis:
持久层框架,主要是用来简化原始jdbc技术对数据库访问操作。
== >整合思路:通过Spring框架接管Mybatis框架中核心对象的创建。
Mybatis框架中核心对象是谁?
sqlSession?
SqlSessionFactory?
SqlSessionFactoryBuilder?
dao?
最核心的对象必然是:SqlSessionFactory。
SqlSessionFactoryBuilder的作用就是读取解析配置文件==【数据源配置,mapper文件配置】==,来创建SqlSessionFactory。
SqlSession的创建又依靠于SqlSessionFactory。
== > SqlSessionFactory是最核心的对象。
SM整合
整合思路:通过Spring框架接管Mybatis中核心的SqlSessionFactory对象的创建。
SqlSessionFactory是简单对象还是复杂对象呢?
如果是简单对象
<bean id="" class=""/>
如果是复杂对象
通过查看源码得知,SqlSessionFactory是一个接口类型的复杂对象。
如何创建?
is = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder(),build(is);
1).SqlSessionFactory(Factory)Bean implements FactoryBean<SqlSessionFactory> {
SqlSessionFactory getObject() {
is = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder(),build(is);
}
Class getClass() {
return SqlSessionFactory.class;
}
boolean isSingleton() {
return true;
}
}
2).工厂管理SqlSessionFactory
<bean id="sqlSessionFactory" class="xxx.SqlSessionFactoryBean">
3).工厂获取
SqlSessionFactory sf = context.getBean("sqlSessionFactory");
写完之后,我们可以发现,这段代码是固定不变的,这个项目要写,别的项目也要写。
于是Mybatis官方替我们写好了,对这段代码进行了封装:Mybatis-spring.jar。
jar包里提供了一个类:SqlSessionFactoryBean。
我们以后只要引Mybatsi-spring依赖就可以了,不用在自己去写了。
值得注意的是:mybatis官方提供SqlSessionFactoryBean,不在使用mybaits主配置文件。
主配置文件的核心就是【数据源】【mapper文件的注册】
所以我们要注入数据源对象,引入druid依赖,注入mapper文件的位置。【DI思想,依赖注入】
<!-- 配置Spring.xml文件 -->
<!-- 创建数据源对象 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >
<property name="driverName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/xxx"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<bean/>
<!-- 创建SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源对象 -->
<property name="dataSource" ref="dataSource" />
<!-- 注入mapper文件的位置 -->
<property name="mapperLocations">
<array>
<value>.....</value>
</array>
</property>
<bean/>
来源:https://blog.csdn.net/Sherlook_Holmes/article/details/122674370
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring的IOC控制反转详解
![](https://img.aspxhome.com/file/2023/1/77101_0s.png)
Android上传文件到服务端并显示进度条
![](https://img.aspxhome.com/file/2023/3/72623_0s.jpg)
简单实现Java通讯录系统
Java Socket编程(五) 简单的WEB服务器
Java异常处理中的一些特殊情况举例
Java实现注册登录与邮箱发送账号验证激活功能
![](https://img.aspxhome.com/file/2023/6/60756_0s.png)
maven中配置项目的jdk版本无效的排查方式
![](https://img.aspxhome.com/file/2023/9/57509_0s.png)
Java实现的计算最大下标距离算法示例
Java基础之练习打印三角形
![](https://img.aspxhome.com/file/2023/7/58187_0s.png)
Java编程中的检查型异常与非检查型异常分析
JetBrains IntelliJ IDEA 配置优化技巧
![](https://img.aspxhome.com/file/2023/7/64067_0s.png)
android 6.0 写入SD卡的权限申请实例讲解
三步轻松搭建springMVC框架
![](https://img.aspxhome.com/file/2023/6/64036_0s.jpg)
c#的dllimport使用方法详解
SpringBoot2 参数管理实践之入参出参与校验的方式
Java用栈实现综合计算器
MybatisPlus如何自定义TypeHandler映射JSON类型为List
MyBatis-Plus框架整合详细方法
![](https://img.aspxhome.com/file/2023/6/73046_0s.png)
详解Android的.aar文件生成方法以及使用技巧
![](https://img.aspxhome.com/file/2023/9/85459_0s.jpg)
static关键字有何魔法?竟让Spring Boot搞出那么多静态内部类(推荐)
![](https://img.aspxhome.com/file/2023/9/70269_0s.png)