Spring Boot使用配置方式整合MyBatis

devtools/2024/9/29 18:01:44/

文章目录

  • 一、实战目标
  • 二、步骤概览
    • 1. 创建部门映射器接口
    • 2. 创建映射器配置文件
    • 3. 配置全局映射器
    • 4. 测试映射器接口
  • 三、详细步骤
    • 1、创建部门映射器接口
    • 2、创建映射器配置文件
    • 3、配置全局映射器
    • 4、测试映射器接口
  • 四、结语

在这里插入图片描述

一、实战目标

在本实战课程中,我们将学习如何在Spring Boot项目中使用配置方式整合MyBatis框架,并实现部门管理功能。

二、步骤概览

  1. 创建部门映射器接口
  2. 创建映射器配置文件
  3. 配置全局映射器
  4. 测试映射器接口

1. 创建部门映射器接口

net.huawei.hrsys_ssm.mapper包下创建DepartmentMapper接口,使用@Mapper注解标记。

2. 创建映射器配置文件

resources/mapper目录下创建DepartmentMapper.xml,定义SQL映射。

3. 配置全局映射器

在MyBatis配置文件中指定映射器配置文件位置和别名路径。

4. 测试映射器接口

创建TestDepartmentMapper类,使用@SpringBootTest注解进行测试。

三、详细步骤

1、创建部门映射器接口

java">@Mapper
public interface DepartmentMapper {int insert(Department department);int deleteById(int id);int update(Department department);Department findById(int id);List<Department> findAll();
}

2、创建映射器配置文件

  • DepartmentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="net.huawei.hrsys_ssm.mapper.DepartmentMapper"><!-- 插入部门记录 --><insert id="insert" parameterType="Department"useGeneratedKeys="true" keyProperty="id">insert into department (name, number) values (#{name}, #{number});</insert><!-- 删除部门记录 --><delete id="deleteById" parameterType="int">delete from department where id = #{id};</delete><!-- 更新部门记录 --><update id="update" parameterType="Department">update department set name = #{name}, number = #{number} where id = #{id};</update><!-- 查询全部部门 --><select id="findAll" resultType="Department">select * from department;</select><!-- 创建结果映射,一个部门对应多个员工构成的集合 --><resultMap id="DepartmentWithEmployees" type="Department"><id property="id" column="id"/><result property="name" column="name"/><result property="number" column="number"/><collection property="employees" javaType="list" ofType="Employee"><id property="id" column="e_id"/><result property="age" column="age"/><result property="gender" column="gender"/><result property="name" column="e_name"/><result property="number" column="e_number"/><result property="depId" column="dep_id"/></collection></resultMap><!-- 按标识符查询部门记录 --><select id="findById" resultMap="DepartmentWithEmployees">select d.*, e.id e_id, e.age, e.gender, e.name e_name, e.number e_number, e.dep_idfrom department d left outer join employee e on d.id = e.dep_idwhere d.id = #{id};</select>
</mapper>

3、配置全局映射器

mapper-locations: classpath:mapper/*.xml
type-aliases-package: net.huawei.hrsys_ssm.bean

在这里插入图片描述

4、测试映射器接口

java">package net.huawei.hrsys_ssm;import net.huawei.hrsys_ssm.bean.Department;
import net.huawei.hrsys_ssm.mapper.DepartmentMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;/*** 功能:测试部门映射器接口* 作者:华卫* 日期:2024年09月25日*/
@SpringBootTest
public class TestDepartmentMapper {@Autowired // 自动装配部门映射器private DepartmentMapper departmentMapper;@Test // 测试查询全部部门public void testFindAll() {// 调用部门映射器的查询全部部门方法List<Department> departments = departmentMapper.findAll();// 利用Lambda表达式遍历部门列表departments.forEach(department -> System.out.println(department));}@Test // 测试按标识符查询部门public void testFindById() {// 定义标识符int id = 1;// 调用部门映射器的按标识符查询部门方法Department department = departmentMapper.findById(id);// 判断部门是否查询成功if (department != null) {System.out.println(department);} else {System.out.println("标识符为[" + id + "]的部门不存在~");}}@Test // 测试插入部门public void testInsert() {// 创建部门对象Department department = new Department();// 设置部门对象属性department.setName("后勤部");department.setNumber(5);// 调用部门映射器的插入方法int count = departmentMapper.insert(department);// 判断部门是否插入成功if (count > 0) {System.out.println("恭喜,部门记录插入成功~");System.out.println("插入的记录:" + departmentMapper.findById(5));} else {System.out.println("遗憾,部门记录插入失败~");}}@Test // 测试更新部门public void testUpdate() {// 查询id为5的部门Department department = departmentMapper.findById(5);// 输出更新前记录System.out.println("记录更新前:" + department);// 设置部门对象属性department.setName("保卫部");department.setNumber(555);// 调用部门映射器的更新部门方法int count = departmentMapper.update(department);// 判断部门是否更新成功if (count > 0) {System.out.println("恭喜,部门记录更新成功~");// 输出更新后记录System.out.println("记录更新后:" + departmentMapper.findById(5));} else {System.out.println("遗憾,部门记录更新失败~");}}@Test // 测试按标识符删除员工public void testDeleteById() {// 输出待删除记录System.out.println("待删除记录:" + departmentMapper.findById(5));// 调用部门映射器的按标识符删除部门方法int count = departmentMapper.deleteById(5);// 判断部门是否删除成功if (count > 0) {System.out.println("恭喜,部门记录删除成功~");} else {System.out.println("遗憾,部门记录删除失败~");}}
}

四、结语

通过本课程,你将掌握Spring Boot与MyBatis的整合,并能够实现部门管理的CRUD操作。


http://www.ppmy.cn/devtools/118812.html

相关文章

2024年配置YOLOX运行环境+windows+pycharm24.0.1+GPU

1.配置时间2024/9/25 2.Anaconda-python版本3.7&#xff0c;yolox版本0.2.0 YOLOX网址: https://github.com/Megvii-BaseDetection/YOLOX 本人下载的这个版本 1.创建虚拟环境 conda create -n yolox37 python37 激活 conda activate yolox37 2.安装Pytorch cuda等&…

关于PHP 匿名函数在处理数据结构中的应用

PHP 的匿名函数&#xff08;也称为闭包&#xff09;在处理数据结构时非常有用。它们可以在需要一次性函数的情况下使用&#xff0c;例如数组函数的回调、事件处理或作为其他函数的参数。以下是一些常见的应用场景&#xff1a; 数组操作&#xff1a; 使用 array_map、array_fil…

【RabbitMQ——SpringBoot整合】

1. fanout模式 Fanout 交换器&#xff08;Exchange&#xff09;是 RabbitMQ 中的一种消息路由模式。它是一种广播式的交换器&#xff0c;会将接收到的消息发送给所有绑定到该交换器的队列&#xff0c;而不考虑路由键&#xff08;routing key&#xff09;。这意味着无论消息发布…

边缘计算网关:轻工行业的智能化新引擎

在轻工行业迈向智能化转型的浪潮中&#xff0c;边缘计算网关作为关键技术载体&#xff0c;正逐步展现其不可替代的作用。这一前沿技术&#xff0c;通过在数据源附近进行高效数据处理与分析&#xff0c;不仅加速了数据价值挖掘&#xff0c;还推动了生产流程的优化与效率的提升。…

c语言和c++一样吗

C语言和C并不一样&#xff0c;尽管它们有很多相似之处。以下是一些主要区别&#xff1a; 编程范式 C语言&#xff1a;主要是一种过程式编程语言&#xff0c;强调函数和过程。 C&#xff1a;支持面向对象编程&#xff08;OOP&#xff09;&#xff0c;引入了类、对象、继承、多态…

用通义灵码如何快速合理解决遗留代码问题?

本文首先介绍了遗留代码的概念&#xff0c;并对遗留代码进行了分类。针对不同类型的遗留代码&#xff0c;提供了相应的处理策略。此外&#xff0c;本文重点介绍了通义灵码在维护遗留代码过程中能提供哪些支持。 什么是遗留代码 与过时技术相关的代码&#xff1a; 与不再受支持的…

vue3基础九问,你会几问

1. Vue是什么&#xff1f; Vue.js 是一个用于构建用户界面的渐进式 JavaScript 框架。它的核心库只关注视图层&#xff0c;采用自下而上的增量开发设计&#xff0c;这使得你可以将 Vue 轻松地整合到现有的项目中&#xff0c;或者与其他前端库一起使用。Vue 的目标是通过提供反…

【习题】应用开发安全

判断题 1. TEE是Trusted execution environment的缩写&#xff1a; A、正确(True) B、错误(False) 2. OS Kernel&#xff08;操作系统内核&#xff09;的安全性要强于TEE&#xff1a; A、正确(True) B、错误(False) 单选题 1. 以下哪个不是HarmonyOS安全设计…