当前位置: 首页 > news >正文

【数据库系列】Spring Boot 中使用 MyBatis 详细指南

在这里插入图片描述

一、基础介绍

1.1 MyBatis

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJO(Plain Old Java Objects)映射成数据库中的记录。

1.2 Spring Boot 集成 MyBatis 的优势

将 Spring Boot 和 MyBatis 集成,能够充分发挥 Spring Boot 的快速开发特性和 MyBatis 灵活的数据库操作能力。通过这种集成,可以快速搭建一个稳定、高效的数据库访问层,简化开发流程,提高开发效率。

二、集成步骤

2.1 创建 Spring Boot 项目

可以使用 Spring Initializr(https://start.spring.io/)来快速创建一个 Spring Boot 项目。在创建项目时,需要选择以下依赖:

  • Spring Web
  • Spring Data JPA
  • MySQL Driver
  • MyBatis Framework

2.2 配置数据源

application.properties 文件中配置数据库连接信息:

spring.datasource.url=jdbc:mysql://localhost:3306/yourdatabase
spring.datasource.username=yourusername
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

2.3 配置 MyBatis

application.properties 文件中配置 MyBatis 的相关属性:

mybatis.mapper-locations=classpath:/mapper/*.xml
mybatis.type-aliases-package=com.example.demo.entity

mybatis.mapper-locations 配置 MyBatis 映射文件的位置,mybatis.type-aliases-package 配置实体类的包名,这样在 MyBatis 映射文件中就可以直接使用实体类名而无需写全限定名。

2.4 创建实体类

创建一个 Java 实体类,用于映射数据库表中的记录。例如,创建一个 User 实体类:

package com.example.demo.entity;public class User {private Long id;private String username;private String password;// 省略 getters 和 setters
}

2.5 创建 Mapper 接口

创建一个 Mapper 接口,用于定义数据库操作方法。例如,创建一个 UserMapper 接口:

package com.example.demo.mapper;import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.util.List;@Mapper
public interface UserMapper {@Select("SELECT * FROM user")List<User> findAll();
}

@Mapper 注解用于将该接口标记为 MyBatis 的 Mapper 接口。

2.6 创建 Mapper 映射文件

resources/mapper 目录下创建一个 UserMapper.xml 文件,用于实现 Mapper 接口中的方法:

<?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">
<mapper namespace="com.example.demo.mapper.UserMapper"><select id="findAll" resultType="com.example.demo.entity.User">SELECT * FROM user</select>
</mapper>

namespace 属性指定 Mapper 接口的全限定名,id 属性指定要实现的方法名,resultType 属性指定返回结果的类型。

2.7 创建 Service 层

创建一个 Service 层,用于调用 Mapper 接口中的方法。例如,创建一个 UserService 接口和其实现类 UserServiceImpl

package com.example.demo.service;import com.example.demo.entity.User;import java.util.List;public interface UserService {List<User> findAll();
}
package com.example.demo.service.impl;import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic List<User> findAll() {return userMapper.findAll();}
}

2.8 创建 Controller 层

创建一个 Controller 层,用于处理客户端请求。例如,创建一个 UserController

package com.example.demo.controller;import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
@RequestMapping("/users")
public class UserController {@Autowiredprivate UserService userService;@GetMappingpublic List<User> findAll() {return userService.findAll();}
}

三、示例完整代码

1. 创建 application.yml

src/main/resources 目录下创建 application.yml 文件。

2. 配置数据源和 MyBatis

将原本 application.properties 中的配置内容转换为 YAML 格式,如下:

spring:datasource:url: jdbc:mysql://localhost:3306/yourdatabaseusername: yourusernamepassword: yourpassworddriver-class-name: com.mysql.cj.jdbc.Driver
mybatis:mapper-locations: classpath:/mapper/*.xmltype-aliases-package: com.example.demo.entity

说明

  • 在 YAML 中,使用缩进表示层级关系。例如,springmybatis 是顶级配置项,datasourcespring 的子配置项,urlusernamepassworddriver-class-namedatasource 的子配置项。
  • 冒号后面需要跟一个空格,然后再写具体的值。

完整项目示例

以下是基于上述 application.yml 配置的完整项目示例,包括之前提到的各个部分:

项目结构
src
├── main
│   ├── java
│   │   ├── com
│   │   │   ├── example
│   │   │   │   ├── demo
│   │   │   │   │   ├── controller
│   │   │   │   │   │   └── UserController.java
│   │   │   │   │   ├── entity
│   │   │   │   │   │   └── User.java
│   │   │   │   │   ├── mapper
│   │   │   │   │   │   ├── UserMapper.java
│   │   │   │   │   │   └── UserMapper.xml
│   │   │   │   │   ├── service
│   │   │   │   │   │   ├── UserService.java
│   │   │   │   │   │   └── impl
│   │   │   │   │   │       └── UserServiceImpl.java
│   │   │   │   │   └── DemoApplication.java
│   └── resources
│       ├── application.yml
│       └── mapper
│           └── UserMapper.xml
└── test└── java└── com└── example└── demo└── DemoApplicationTests.java
依赖配置(pom.xml)
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.mybatis.spring</groupId><artifactId>mybatis-spring</artifactId><version>2.0.6</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.9</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies>
实体类(User.java)
package com.example.demo.entity;public class User {private Long id;private String username;private String password;// 构造函数public User() {}public User(String username, String password) {this.username = username;this.password = password;}// getters 和 setterspublic Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}
Mapper 接口(UserMapper.java)
package com.example.demo.mapper;import com.example.demo.entity.User;
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper
public interface UserMapper {@Select("SELECT * FROM user")List<User> findAll();@Select("SELECT * FROM user WHERE id = #{id}")User findById(Long id);@Insert("INSERT INTO user (username, password) VALUES (#{username}, #{password})")@Options(useGeneratedKeys = true, keyProperty = "id")void save(User user);@Update("UPDATE user SET username = #{username}, password = #{password} WHERE id = #{id}")void update(User user);@Delete("DELETE FROM user WHERE id = #{id}")void delete(Long id);
}
Mapper 映射文件(UserMapper.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">
<mapper namespace="com.example.demo.mapper.UserMapper"><select id="findAll" resultType="com.example.demo.entity.User">SELECT * FROM user</select><select id="findById" resultType="com.example.demo.entity.User">SELECT * FROM user WHERE id = #{id}</select><insert id="save" keyProperty="id" useGeneratedKeys="true">INSERT INTO user (username, password) VALUES (#{username}, #{password})</insert><update id="update">UPDATE user SET username = #{username}, password = #{password} WHERE id = #{id}</update><delete id="delete">DELETE FROM user WHERE id = #{id}</delete>
</mapper>
Service 层

UserService.java

package com.example.demo.service;import com.example.demo.entity.User;import java.util.List;public interface UserService {List<User> findAll();User findById(Long id);void save(User user);void update(User user);void delete(Long id);
}

UserServiceImpl.java

package com.example.demo.service.impl;import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic List<User> findAll() {return userMapper.findAll();}@Overridepublic User findById(Long id) {return userMapper.findById(id);}@Overridepublic void save(User user) {userMapper.save(user);}@Overridepublic void update(User user) {userMapper.update(user);}@Overridepublic void delete(Long id) {userMapper.delete(id);}
}
Controller 层(UserController.java)
package com.example.demo.controller;import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/users")
public class UserController {@Autowiredprivate UserService userService;@GetMappingpublic ResponseEntity<List<User>> findAll() {List<User> users = userService.findAll();return new ResponseEntity<>(users, HttpStatus.OK);}@GetMapping("/{id}")public ResponseEntity<User> findById(@PathVariable Long id) {User user = userService.findById(id);if (user!= null) {return new ResponseEntity<>(user, HttpStatus.OK);} else {return new ResponseEntity<>(HttpStatus.NOT_FOUND);}}@PostMappingpublic ResponseEntity<Void> save(@RequestBody User user) {userService.save(user);return new ResponseEntity<>(HttpStatus.CREATED);}@PutMappingpublic ResponseEntity<Void> update(@RequestBody User user) {userService.update(user);return new ResponseEntity<>(HttpStatus.NO_CONTENT);}@DeleteMapping("/{id}")public ResponseEntity<Void> delete(@PathVariable Long id) {userService.delete(id);return new ResponseEntity<>(HttpStatus.NO_CONTENT);}
}
测试(DemoApplicationTests.java)
package com.example.demo;import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.*;@SpringBootTest
public class DemoApplicationTests {@Autowiredprivate UserService userService;@Testpublic void testFindAll() {assertEquals(0, userService.findAll().size());}@Testpublic void testSaveAndFindById() {User user = new User("testuser", "testpassword");userService.save(user);assertNotNull(userService.findById(user.getId()));}@Testpublic void testUpdate() {User user = new User("testuser", "testpassword");userService.save(user);user.setPassword("newpassword");userService.update(user);assertEquals("newpassword", userService.findById(user.getId()).getPassword());}@Testpublic void testDelete() {User user = new User("testuser", "testpassword");userService.save(user);userService.delete(user.getId());assertNull(userService.findById(user.getId()));}
}

通过上述配置和代码示例,你可以在 Spring Boot 项目中使用 application.yml 配置文件来集成 MyBatis,并实现完整的用户管理功能。

相关文章:

【数据库系列】Spring Boot 中使用 MyBatis 详细指南

一、基础介绍 1.1 MyBatis MyBatis 是一款优秀的持久层框架&#xff0c;它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息&#xff0c;将接口和 Java 的 P…...

Azure Airflow 中配置错误可能会使整个集群受到攻击

网络安全研究人员在 Microsoft 的 Azure 数据工厂 Apache Airflow 中发现了三个安全漏洞&#xff0c;如果成功利用这些漏洞&#xff0c;攻击者可能会获得执行各种隐蔽操作的能力&#xff0c;包括数据泄露和恶意软件部署。 “利用这些漏洞可能允许攻击者以影子管理员的身份获得…...

Python跨年烟花

目录 系列文章 写在前面 技术需求 完整代码 下载代码 代码分析 1. 程序初始化与显示设置 2. 烟花类 (Firework) 3. 粒子类 (Particle) 4. 痕迹类 (Trail) 5. 烟花更新与显示 6. 主函数 (fire) 7. 游戏循环 8. 总结 注意事项 写在后面 系列文章 序号直达链接爱…...

【代码】Python|Windows 批量尝试密码去打开加密的 Word 文档(docx和doc)

文章目录 前言完整代码Githubdocxdoc 代码解释1. msoffcrypto 方法&#xff08;用于解密 .docx 文件&#xff09;read_secret_word_file 函数密码生成与解密尝试try_decrypt_file 函数 2. comtypes 方法&#xff08;用于解密 .doc 文件&#xff09;read_secret_word_file 函数注…...

java开发中注解汇总​​

注解作用位置注意mybatis Data Getter Setter ToString EqualsAndHashCode AllArgsConstructor NoArgsConstructor Data 代替&#xff1a;无参构造&#xff0c;get&#xff0c;set&#xff0c;toString&#xff0c;hashCode&#xff0c;equals Getter Setter 可放在类和方法上&…...

C# 设计模式(结构型模式):外观模式

C# 设计模式&#xff08;结构型模式&#xff09;&#xff1a;外观模式 (Facade Pattern) 在复杂系统中&#xff0c;往往会涉及到多个子系统、模块和类。这些子系统的接口和功能可能会让使用者感到困惑和复杂。在这种情况下&#xff0c;我们可以使用外观模式&#xff08;Facade…...

PowerShell 常见问题解答

PowerShell 是微软开发的一种功能强大的命令行界面和脚本语言&#xff0c;广泛应用于系统管理和自动化任务。以下是一些使用 PowerShell 时常见的问题及其解决方法。 什么是 PowerShell&#xff1f; PowerShell 是基于 .NET 的命令行界面&#xff08;CLI&#xff09;和脚本语言…...

计算机网络 (15)宽带接入技术

前言 计算机网络宽带接入技术是指通过高速、大容量的通信信道或网络&#xff0c;实现用户与互联网或其他通信网络之间的高速连接。 一、宽带接入技术的定义与特点 定义&#xff1a;宽带接入技术是指能够传输大量数据的通信信道或网络&#xff0c;其传输速度通常较高&#xff0c…...

前端Python应用指南(六)构建RESTful API:使用Flask和Django实现用户认证与授权

《写给前端的python应用指南》系列&#xff1a; &#xff08;一&#xff09;快速构建 Web 服务器 - Flask vs Node.js 对比&#xff08;二&#xff09;深入Flask&#xff1a;理解Flask的应用结构与模块化设计&#xff08;三&#xff09;Django vs Flask&#xff1a;哪种框架适…...

【Unity3D】基于UGUI——简易版 UI框架

https://github.com/AMikeW/BStandShaderResources/blob/master/milk_UIFramework.unitypackage UI框架支持如下功能&#xff1a; 1、层级控制 2、支持面板多次打开时&#xff0c;隐藏前一个打开的面板&#xff0c;当关闭面板时&#xff0c;能够恢复前一个打开面板状态 3、支…...

算法排序算法

文章目录 快速排序[leetcode 215数组中的第K个最大元素](https://leetcode.cn/problems/kth-largest-element-in-an-array/)分析题解快速排序 桶排序[leetcode 347 前K个高频元素](https://leetcode.cn/problems/top-k-frequent-elements/)分析题解 快速排序 leetcode 215数组…...

第3章 总线

总线的定义 为多个部件 分时共享 公共信息传送线路。 系统之间、模块之间、芯片内部用来传递信息信号线集合。 共享 总线上可连接多个部件 各部件间相互交换信息 都可通过总线来。 分时 同一时刻 总线上只能传 一个部件信息。 采用标准总线的优点 简化系统软硬件设计 从硬件角度…...

手机实时提取SIM卡打电话的信令声音-双卡手机来电如何获取哪一个卡的来电

手机实时提取SIM卡打电话的信令声音 --双卡手机来电如何获取哪一个卡的来电 一、前言 前面的篇章《手机实时提取SIM卡打电话的信令声音-智能拨号器的双SIM卡切换方案》中&#xff0c;我们论述了局域网SIP坐席通过手机外呼出去时&#xff0c;手机中主副卡的呼叫调度策略。 但…...

共阳极LED的控制与短路问题解析

共阳极LED的控制与短路问题解析 在电子电路中&#xff0c;LED&#xff08;发光二极管&#xff09;是最常见的元件之一。LED的连接方式分为共阳极和共阴极&#xff0c;不同的连接方式决定了LED的控制逻辑。本文将重点讲解共阳极LED的工作原理&#xff0c;并解答“为什么给1不会…...

华为消费级QLC SSD来了

近日&#xff0c;有关消息显示&#xff0c;华为的消费级SSD产品线&#xff0c;eKitStor Xtreme 200E系列&#xff0c;在韩国一家在线零售商处首次公开销售&#xff0c;引起了业界的广泛关注。 尽管华为已经涉足服务器级别的SSD制造多年&#xff0c;但直到今年6月才正式推出面向…...

liunx下载gitlab

1.地址&#xff1a; https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/ 安装 postfix 并启动 yum install postfix systemctl start postfix systemctl enable postfix ssh服务启动 systemctl enable sshd systemctl start sshd开放 ssh 以及 http 服务&#xff0c…...

深度学习模型预测值集中在某一个值

深度学习模型&#xff0c;训练过程中&#xff0c;经常遇到预测的结果集中在某个值&#xff0c;而且在学习的过程中会变&#xff0c;样例如下。 主要有如下解决方案 1、更换relu ->tanh 或者其他激活函数 2、更改随机种子&#xff0c;估计是没有初始化好&#xff0c;或者调…...

Sqoop的使用

每个人的生活都是一个世界&#xff0c;即使最平凡的人也要为他那个世界的存在而战斗。 ——《平凡的世界》 目录 一、sqoop简介 1.1 导入流程 1.2 导出流程 二、使用sqoop 2.1 sqoop的常用参数 2.2 连接参数列表 2.3 操作hive表参数 2.4 其它参数 三、sqoop应用 - 导入…...

OpenGL ES 04 图片数据是怎么写入到对应纹理单元的

从指定路径加载图像并转换为 CGImage。获取图像的宽度和高度。创建一个 RGB 颜色空间。为图像数据分配内存。创建一个位图上下文并将图像绘制到上下文中。创建一个新的纹理对象并绑定到指定的纹理单元。指定二维纹理图像。释放分配的内存。设置纹理参数&#xff0c;包括放大和缩…...

C# 设计模式的六大原则(SOLID)

C# 设计模式的六大原则&#xff08;SOLID&#xff09; 引言 在面向对象编程中&#xff0c;设计模式提供了高效、可复用和可维护的代码结构。SOLID原则是软件设计中的一组重要原则&#xff0c;用于确保代码具有良好的可维护性、可扩展性和灵活性。SOLID是五个设计原则的首字母…...

告别手动刷课!智慧树网课助手让你的学习效率提升50%

告别手动刷课&#xff01;智慧树网课助手让你的学习效率提升50% 【免费下载链接】zhihuishu 智慧树刷课插件&#xff0c;自动播放下一集、1.5倍速度、无声 项目地址: https://gitcode.com/gh_mirrors/zh/zhihuishu 你是否厌倦了在智慧树平台上频繁点击"下一集"…...

第21课:把 Qt 常用能力串成实战链路,打通文本、绘图、线程、网络与多媒体

本节路线图 为什么这节课看起来很散, → 先把程序的输入输出拿下: → 让界面真正活起来:`QP 兔兔建议 先顺着路线图跑一遍,再抄命令和代码,学习体验会轻松很多。 前两课我们已经把 Qt 的“界面底座”搭起来了,但真正做项目时,很多同学还是会卡在另一个问题上:界面会做了…...

C语言宏定义:嵌入式开发中的高效利器与避坑指南

1. C语言宏定义的基础与陷阱在嵌入式开发中&#xff0c;宏定义是C语言最强大的特性之一&#xff0c;但也是最容易踩坑的特性。让我们从一个简单的需求开始&#xff1a;如何用宏实现两个数的比较并返回较小值&#xff1f;初学者最常见的写法是这样的&#xff1a;#define MIN(a,b…...

Wan2.2-I2V-A14B企业应用:合规可控的AI视频生成私有云部署方案

Wan2.2-I2V-A14B企业应用&#xff1a;合规可控的AI视频生成私有云部署方案 1. 企业级视频生成解决方案概述 在当今内容创作需求爆炸式增长的环境下&#xff0c;企业面临着视频制作成本高、周期长的挑战。Wan2.2-I2V-A14B私有部署镜像提供了一套完整的解决方案&#xff0c;让企…...

三自由度机械手-工业机器人(说明书+CAD图纸)

三自由度机械手作为工业机器人领域的典型代表&#xff0c;其核心作用在于通过三个独立运动轴的协同控制&#xff0c;实现末端执行器在三维空间内的精准定位与灵活操作。这种结构通过旋转、俯仰与伸缩三个方向的复合运动&#xff0c;能够覆盖工作空间内的任意目标点&#xff0c;…...

Phi-4-mini-reasoning推理质量评估:GSM8K/MATH数据集本地测试方法

Phi-4-mini-reasoning推理质量评估&#xff1a;GSM8K/MATH数据集本地测试方法 1. 模型简介 Phi-4-mini-reasoning是一个轻量级开源模型&#xff0c;专注于高质量数学推理任务。作为Phi-4模型家族的一员&#xff0c;它通过合成数据训练和微调&#xff0c;特别擅长解决需要密集…...

`claude code --print` 核心含义与用法指南

claude code --print 核心含义与用法指南 --print(简写为-p)是Claude Code CLI的非交互模式参数,用于执行单个查询后直接输出结果并退出,不进入交互式会话。这是自动化脚本、管道操作和CI/CD集成的核心工具。 一、核心定义与作用 特性 说明 全称/简写 --print / -p 核心功…...

人工智能应用快速原型开发:基于PyTorch 2.8和Gradio构建交互式Demo

人工智能应用快速原型开发&#xff1a;基于PyTorch 2.8和Gradio构建交互式Demo 1. 为什么需要快速原型开发工具 在人工智能领域&#xff0c;一个好想法从诞生到落地往往需要经历漫长的验证过程。传统方式下&#xff0c;即使训练出了一个效果不错的模型&#xff0c;想要展示给…...

intv_ai_mk11惊艳效果:输入‘用小学生能懂的话解释Transformer’→输出比喻+图示描述+小练习

intv_ai_mk11惊艳效果&#xff1a;输入用小学生能懂的话解释Transformer→输出比喻图示描述小练习 1. 效果展示开场 当我第一次尝试让intv_ai_mk11解释Transformer这个复杂概念时&#xff0c;我完全没想到它会给出如此惊艳的答案。我输入了一个看似简单的请求&#xff1a;&qu…...

提升GitHub访问效率的实用方案

提升GitHub访问效率的实用方案 【免费下载链接】gh-proxy github release、archive以及项目文件的加速项目 项目地址: https://gitcode.com/gh_mirrors/gh/gh-proxy 诊断连接瓶颈 检测网络延迟指标 准备工作&#xff1a;确保系统已安装网络诊断工具&#xff08;Linux默…...