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的思想都是非常值得学习与使用的.以下 ...
随机推荐
- Stupid && 祖传Fortran代码救赎之路(编译Dll)
Stupid && 祖传Fortran代码救赎之路(编译Dll) gfortran编译动态库 在Windows平台下,Intel Fortran安装过于庞大且费事(现在集成到OneAP ...
- 洛谷 P6071 『MdOI R1』Treequery(LCA+线段树+主席树)
题目链接 题意:给出一棵树,有边权,\(m\) 次询问,每次给出三个数 \(p,l,r\),求边集 \(\bigcap\limits_{i=l}^rE(p,i)\) 中所有边的权值和. 其中 \(E( ...
- Atcoder Regular Contest 125 E - Snack(最小割转化+贪心)
Preface: 这是生平第一道现场 AC 的 arc E,也生平第一次经历了 performance \(\ge 2800\),甚至还生平第一次被 hb 拉到会议里讲题,讲的就是这个题,然鹅比较尬 ...
- 基于PASA进行基因预测
PASA, acronym for Program to Assemble Spliced Alignments, is a eukaryotic genome annotation tool tha ...
- 字符scanf 的输入注意
1.注意scanf 不能有空格,如果有空格会将空格给输入进去 scanf("d "):---有空格 和scanf("d");--没有空格 有很大的区别
- Java 堆、栈、队列(遇见再更新)
目录 Java 栈.队列 栈 常用方法 案例 队列 Java 栈.队列 栈 常用方法 boolean empty() 测试堆栈是否为空 Object peek() 查看堆栈顶部的对象 Object p ...
- Learning Spark中文版--第六章--Spark高级编程(2)
Working on a Per-Partition Basis(基于分区的操作) 以每个分区为基础处理数据使我们可以避免为每个数据项重做配置工作.如打开数据库连接或者创建随机数生成器这样的操作,我们 ...
- ES6必知,箭头函数与普通函数的区别。
1. 箭头函数没有prototype(原型),所以箭头函数本身没有this let a = () =>{}; console.log(a.prototype); // undefined 2. ...
- Oracle中常用的系统函数
本文主要来梳理下Oracle中的常用的系统函数,掌握这些函数的使用,对于我们编写SQL语句或PL/SQL代码时很有帮助,所以这也是必须掌握的知识点. 本文主要包括以下函数介绍:1.字符串函数2. 数值 ...
- 视图View,获取视图大小
一.获得LayoutInflater实例: LayoutInflater layoutInflater=LayoutInflater.from(context); 得到LayoutInflater实例 ...