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的思想都是非常值得学习与使用的.以下 ...
随机推荐
- P6072 『MdOI R1』Path
考虑我们有这样操作. 我们只要维护两点在子树内和两点在子树外的异或和即可. 前者可以类似于线段树合并的trie树合并. 后者有两种做法: 一种是把dfn序翻倍:然后子树补变成了一个区间最大异或问题,可 ...
- Codeforces 1276D - Tree Elimination(树形 dp)
Codeforces 题面传送门 & 洛谷题面传送门 繁琐的简单树形 dp(大雾),要是现场肯定弃了去做 F 题 做了我一中午,写篇题解纪念下. 提供一种不太一样的思路. 首先碰到这样的题肯定 ...
- PostgreSQL 数据库备份与还原
PostgreSQL 数据库备份与还原 目录 备份 还原 栗子 备份 PostgreSQL提供的一个工具pg_dump,逻辑导出数据,生成sql文件或其他格式文件,pg_dump是一个客户端工具,可以 ...
- 【百奥云GS专栏】全基因组选择之工具篇
目录 1. 免费开源包/库 1.1 R包 1.2 Python库 2. 成熟软件 3. WEB/GUI工具 前面我们已经介绍了基因组选择的各类模型,今天主要来了解一下做GS有哪些可用的软件和工具.基因 ...
- 【代谢组学】Metabolomics资源推送
入门课程 伯明翰大学: Metabolomics: Understanding Metabolism in the 21st Century 数据处理 阿拉巴马大学伯明翰分校5年(2013-2018) ...
- miRNA分析--数据过滤(一)
miRNA 数据过滤我使用cutadapt 1 cutadapt -a AGATCGGAAGAGCACACGTCT -m 15 -q 20 --discard-untrimmed -o outname ...
- mysql-彻底删除方法
一.如果是使用yum安装的mysql,使用如下命令进行卸载(不能确定使用何种方式安装的mysql情况下,按后续步骤一一进行处理即可): yum remove mysql mysql-server my ...
- NAT 工作原理
网络地址转换,就是替换IP报文头部的地址信息.NAT通常部署在一个组织的网络出口位置,通过将内部网络IP地址替换为出口的IP地址提供公网可达性和上层协议的连接能力 规定了三个保留地址段落:10.0.0 ...
- JavaScript中var与let的异同点
var是JavaScript刚出现时就存在的变量声明关键字,而let作为ES6才出现的变量声明关键字,无疑两者之间存在着很大的区别.那么具体有哪些区别呢? 1.作用域表现形式不同,var是函数作用域, ...
- Centos7部署RabbitMQ的镜像队列集群
一.背景 在上一章节中,我们学会了如何搭建一个单节点的RabbitMQ服务器,但是单节点的RabbitMQ不可靠,如果单节点挂掉,则会导致消息队列不可用.此处我们搭建一个3个节点的RabbitMQ集群 ...