Spring学习(五)Spring和Mybatis的整合
1、前言
在整合之前、要搞清楚是谁整合谁、后续会学到很多整合的例子。在这里、是Spring整合Mybatis、Spring中集成了很多关于Mybatis中一些关键类的jar包、通过这些、可以更加方便的联系Mybatis和数据库之间的关系。以前、Mybatis操作数据库的思路是这样的SqlSessionFactory -> SqlSession ->StudentMapper ->CRUD。可以发现 ,MyBatis最终是通过SqlSessionFactory来操作数据库。Spring整合MyBatis 其实就是 将MyBatis的SqlSessionFactory 交给Spring
2、项目骨架

3、步骤
1、下载相关的jar包

2、创建实体类和表

3、配置MyBatis配置文件conf.xml
其实里面没用到啥东西、单纯需要配一下。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 数据库信息 -->
<!-- 加载映射文件 -->
<!-- <mappers>-->
<!-- <mapper resource="com/feng/mapper/studentMapper.xml"></mapper>-->
<!-- </mappers>-->
</configuration>
4、通过mapper.xml将 类、表建立映射关系
studentMapper
package com.feng.mapper;
import com.feng.enetity.Student;
public interface StudentMapper {
public void addStudent(Student student);
}
studentMapper.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">
<!--namespace:该mapper.xml映射文件的唯一标识-->
<mapper namespace="com.feng.mapper.StudentMapper">
<select id="queryStudentByStuNo" parameterType="int" resultType="com.feng.enetity.Student">
select * from student where stuno = #{stuNo}
</select>
<insert id="addStudent" parameterType="com.feng.enetity.Student">
insert into student(stuno, stuname, stuage) values(#{stuNo},#{stuName},#{stuAge})
</insert>
</mapper>
5、Spring管理SqlSessionFactory
之前使用MyBatis: conf.xml ->SqlSessionFacotry .现在整合的时候,需要通过Spring管理SqlSessionFacotry ,因此 产生qlSessionFacotry 所需要的数据库信息 不在放入conf.xml,而需要放入spring配置文件中配置Spring配置文件(applicationContext.xml).
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 加载db.properties文件 -->
<bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:db.properties</value>
</array>
</property>
</bean>
<!-- 配置数据库信息(替代mybatis的conf.xml) -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 在SpringIoc容器中创建Mybatis核心类SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 加载Mybatis配置文件 -->
<property name="configLocation" value="classpath:conf.xml"></property>
<!-- 加载mapper.xml路径 -->
<property name="mapperLocations" value="com/feng/mapper/*.xml"></property>
</bean>
<bean id="studentMapper" class="com.feng.dao.impl.StudentDaoImpl">
<!--将Spring配置的sqlSessionFactory 对象交给mapper(dao)-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="studentService" class="com.feng.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"></property>
</bean>
</beans>
6、使用Spring-Mybatis整合产物开发程序
目标:通过spring产生mybatis最终操作需要的 动态mapper对象(StudentMapper对象)、Spring产生 动态mapper对象 有3种方法:
a.第一种方式
DAO层实现类继承 SqlSessionDaoSupport类SqlSessionDaoSupport类提供了一个属性 SqlSession
b.第二种方式
就是省略掉 第一种方式的 实现类、直接MyBatis提供的 Mapper实现类:org.mybatis.spring.mapper.MapperFactoryBean 。缺点:每个mapper都需要一个配置一次
c.第三种方式
批量配置 实现类
注:本文用的是第一种方式。
7、三层架构实现
1、Dao层
StudentDaoImpl
package com.feng.dao.impl;
import com.feng.mapper.StudentMapper;
import com.feng.enetity.Student;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentMapper {
@Override
public void addStudent(Student student) {
SqlSession session = super.getSqlSession();
StudentMapper stuDao = session.getMapper(StudentMapper.class);
stuDao.addStudent(student);
}
}
2、Service层
1、接口IStudentService
package com.feng.service;
import com.feng.enetity.Student;
public interface IStudentService {
public void addStudent(Student student);
}
2、实现类StudentServiceImpl
package com.feng.service.impl;
import com.feng.enetity.Student;
import com.feng.mapper.StudentMapper;
import com.feng.service.IStudentService;
public class StudentServiceImpl implements IStudentService {
private StudentMapper studentMapper;
public void setStudentMapper(StudentMapper studentMapper) {
this.studentMapper = studentMapper;
}
@Override
public void addStudent(Student student) {
// 调用Dao
studentMapper.addStudent(student);
}
}
3、数据库配置db.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/xiao?serverTimezone=GMT%2B8
username=root
password=123456
4、测试
1、Test
package com.feng.test;
import com.feng.enetity.Student;
import com.feng.service.IStudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IStudentService studentService = (IStudentService)context.getBean("studentService");
Student student = new Student();
student.setStuNo(4);
student.setStuAge(81);
student.setStuName("xiaofeng1");
studentService.addStudent(student);
}
}

5、总结
借助Spring相关的jar包、极大的简化了Mybatis的操作、为后续的开发简便了很多步骤。
Spring学习(五)Spring和Mybatis的整合的更多相关文章
- Spring学习(十一)-----Spring使用@Required注解依赖检查
Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...
- Spring学习(六)-----Spring使用@Autowired注解自动装配
Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...
- Spring学习之Spring与Mybatis的两种整合方式
本机使用IDEA 2020.1.MySql 8.0.19,通过Maven进行构建 环境准备 导入maven依赖包 <dependencies> <dependency> < ...
- Spring学习笔记--spring+mybatis集成
前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...
- Spring学习笔记(六)—— SSH整合
一.整合原理 二.整合步骤 2.1 导包 [hibernate] hibernate/lib/required hibernate/lib/jpa 数据库驱动 [struts2] struts-bla ...
- Spring学习五----------Bean的配置之Bean的生命周期
© 版权声明:本文为博主原创文章,转载请注明出处 Bean的生命周期 1.定义 2.初始化 3.使用 4.销毁 初始化和销毁的三种方式 1.实现org.springframework.beans.fa ...
- spring学习(三) ———— spring事务操作
前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...
- spring boot(五)Spring data jpa介绍
在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项 使用spr ...
- spring学习12 -Spring 框架模块以及面试常见问题注解等
以下为spring常见面试问题: 1.Spring 框架中都用到了哪些设计模式? Spring框架中使用到了大量的设计模式,下面列举了比较有代表性的: 代理模式—在AOP和remoting中被用的比较 ...
- Spring学习【Spring概述】
从本文開始,我们就要一起学习Spring框架,首先不得不说Spring框架是一个优秀的开源框架. 当中採用IoC原理实现的基于Java Beans的配置管理和AOP的思想都是非常值得学习与使用的.以下 ...
随机推荐
- 难道你还不知道Spring之事务的回滚和提交的原理吗,这篇文章带你走进源码级别的解读。
上一篇文章讲解了获取事务,并通过获取的connection设置只读,隔离级别等:这篇文章讲事务剩下的回滚和提交. 事务的回滚处理 之前已经完成了目标方法运行前的事务准备工作.而这些准备工作的最大目的无 ...
- 洛谷 P4135 作诗(分块)
题目链接 题意:\(n\) 个数,每个数都在 \([1,c]\) 中,\(m\) 次询问,每次问在 \([l,r]\) 中有多少个数出现偶数次.强制在线. \(1 \leq n,m,c \leq 10 ...
- 关闭 IDEA 自动更新
关闭 IDEA 的自动检查更新(截图idea 2020 2.x) idea 右下角会有这样的更新提示 2. 关闭 idea 自动检查更新 取消勾选 Automatically check update ...
- Nginx 动态增加扩展
Nginx 动态增加扩展 1. 先查看目前nginx已加载模块 /home/nginx-1.18.0 # nginx -V nginx version: nginx/1.18.0 built by g ...
- perl 子函数传入多个数组
perl中的引用和C中的指针一样,用"\"标识,引用后可使用符号"->"取值.解引用则在对应的数据类型前加$,@ 或%. 这里这里用两数组求和做示例,引用 ...
- 8.7 进程间的通讯:管道、消息队列、共享内存、信号量、信号、Socket
进程间的通讯 进程间为什么需要通讯? 共享数据.数据传输.消息通知.进程控制 进程间的通讯有哪些类型? 首先,联系前面讲过的知识,进程之间的用户地址空间是相互独立的,不能进行互相访问,但是,内核空间却 ...
- CSS区分Chrome和Firefox
CSS区分Chrome和FireFox 描述:由于Chrome和Firefox浏览器内核不同,对CSS解析有差别,因此常会有在两个浏览器中显示效果不同的问题出现,解决办法如下: /*Chrome*/ ...
- MYSQL获取更新行的主键ID 【转】
在某些情况下我们需要向数据表中更新一条记录的状态,然后再把它取出来,但这时如果你在更新前并没有一个确认惟一记录的主键就没有办法知道哪条记录被更新了. 举例说明下: 有一个发放新手卡的程序,设计数据库时 ...
- 最长公共子序列问题(LCS) 洛谷 P1439
题目:P1439 [模板]最长公共子序列 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 关于LCS问题,可以通过离散化转换为LIS问题,于是就可以使用STL二分的方法O(nlogn ...
- 100个Shell脚本——【脚本5】数字求和
[脚本5]数字求和 编写shell脚本,要求输入一个数字,然后计算出从1到输入数字的和,要求,如果输入的数字小于1,则重新输入,直到输入正确的数字为止,示例: 一.脚本 #!/bin/bash whi ...