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的思想都是非常值得学习与使用的.以下 ...
随机推荐
- BZOJ3971 [WF2013]Матрёшка
*XXXIV. BZOJ3971 [WF2013]Матрёшка 摘自 DP 做题记录 II 例题 XXXIV. 仍然是神仙区间 DP. 直接设状态 \(f_{i,j}\) 表示区间 \([i,j] ...
- xshell的快捷复制粘贴设置
今天试着用xshell连接Linux,运行一些命令的时候想快点复制粘贴实现效率,却发现还要右键选择复制,再右键选择粘贴,很是麻烦. 看了一下xshell的设置,其实可以自己设置成快捷方式 以xshel ...
- 【数据处理】python将GO注释结果整理为WEGO文件
通常,比对NR库后为m8格式,通过NR和GO数据库对应关系文件,写代码整理为Gene-->GO文件,如下: 这里是一对一的关系,要转换为WEGO格式文件,即一对多关系,如下: 用python脚本 ...
- C语言 fastq文件转换为fasta文件2
修改可读取压缩格式文件 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #in ...
- Go知识点大纲
目录 1. 基本介绍 2. 安装及配置 3. 变量 4. 常量 5. 数据类型 5.1 numeric(数字) 5.2 string(字符串) 5.3 array(数组) 5.4 slice(切片) ...
- adblock plus-看下图你就懂
- 【Redis】Sentinel 哨兵模式
Sentinel(哨兵模式) 目录 Sentinel(哨兵模式) 哨兵模式的三个定时任务 Sentinel(哨兵)与Sentinel .主服务器.从服务器之间的连接 检测下线状态 选择领头 Senti ...
- C#序号
OnRowCreated="gridViewCorrection_RowCreated" <asp:BoundField HeaderText="序号" ...
- 重学Git(一)
一.最最最基础操作 # 初始化仓库 git init # 添加文件到暂存区 git add readme.md # 提交 git commit -m 'wrote a readme file' 二.简 ...
- Netty4.x 源码实战系列(一): 深入理解ServerBootstrap 与 Bootstrap (1)
从Java1.4开始, Java引入了non-blocking IO,简称NIO.NIO与传统socket最大的不同就是引入了Channel和多路复用selector的概念.传统的socket是基于s ...