news 2026/5/15 9:33:26

SpringBoot3 整合 Mybatis 完整版

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
SpringBoot3 整合 Mybatis 完整版
本文记录一下完整的 SpringBoot3 整合 Mybatis 的步骤。 只要按照本步骤来操作,整合完成后就可以正常使用。

1. 添加数据库驱动依赖

以 MySQL 为例。
当不指定 依赖版本的时候,会 由 springboot 自动管理。

<dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <!-- <version>8.0.32</version> --> </dependency>

2. 添加 MyBatis 依赖

第三方的依赖库,需要明确的指定版本号。推荐使用最新的即可。

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency>

3. 配置数据源信息

在 application.yaml 文件中添加数据源的信息

spring: datasource: # 数据库连接驱动 driver-class-name: com.mysql.cj.jdbc.Driver # 数据源类型: 默认的是 Hikari type: com.zaxxer.hikari.HikariDataSource # 数据库连接地址 url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai # 数据库连接用户名 username: root # 数据库连接密码 password: 12345678

4. 配置 mybatis

在 application.yaml 文件中添加mybatis的相关配置。

# mybatis 的配置 mybatis: # 配置 mybatis 的xml文件的扫描路径 mapper-locations: classpath:mybatis/**/*.xml # 配置实体类的扫描路径 type-aliases-package: com.testabc.demo.ssmtest configuration: # 开启驼峰命名转换 map-underscore-to-camel-case: true # 开启日志 #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl # 指定日志级别 : 对mybatis的日志输出 logging: level: com.testabc.demo.ssmtest: debug

5. 功能开发

5.1 建表

简单创建一张表。包含了普通属性,标准的下划线属性。

CREATE TABLE `test`.`student` ( `id` int NOT NULL, `name` varchar(20) NOT NULL, `age` int NOT NULL, `other_message` varchar(100) NULL, PRIMARY KEY (`id`) );
5.2 创建普通的bean类

结合表结构,创建普通的一个bean类。此时属性用标准的驼峰命名

package com.testabc.demo.ssmtest; public class Student { private int id; private String name; private int age; private String otherMessage; 。。。。。。 构造方法 getter/setter toString 方法 }
5.3 创建mapper接口

注意 : 此处的接口用到了@Mapper注解。先写上吧,没有副作用。

package com.testabc.demo.ssmtest; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @Mapper public interface StudentMapper { // 根据id查询student的方法 Student getStudentById(@Param("id") int id); }
5.4 创建xml文件

classpath:/resources/mybatis/目录下新增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" > <mapper namespace="com.testabc.demo.ssmtest.StudentMapper"> <select id="getStudentById" resultType="com.testabc.demo.ssmtest.Student"> select * from student where id = #{id} </select> </mapper>
5.5 创建controller类
package com.testabc.demo.ssmtest; @RestController public class StudentController { /** * 通过构造方法的方式注入 StudentMapper */ private final StudentMapper studentMapper; public StudentController(StudentMapper studentMapper) { this.studentMapper = studentMapper; } @GetMapping("/getStudentById/{id}") public Student getStudentById(@PathVariable("id") int id){ Student student = null; student = studentMapper.getStudentById(id); return student; } }
5.6 配置扫描的包

在 项目的 启动类上添加注解MapperScan(xxxx), 指定要扫描的 mapper 接口的包路径。

package com.testabc.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.testabc.demo.ssmtest") public class DemoApplication { public static void main(String[] args) { // 这个工具会返回一个 ApplicationContext 的对象 var ioc = SpringApplication.run(DemoApplication.class, args); } }

6. 功能测试

浏览器中访问测试。



成功,至此,已经完成了 SpringBoot3 整合 Mybatis 的步骤。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/11 17:27:55

springboot-自定义注解

1.注解的概念 注解是一种能被添加到java代码中的【元数据&#xff0c;类、方法、变量、参数和包】都可以用注解来修饰。用来定义一个类、属性或一些方法&#xff0c;以便程序能被捕译处理。 相当于一个说明文件&#xff0c;告诉应用程序某个被注解的类或属性是什么&#xff0c…

作者头像 李华
网站建设 2026/5/1 8:17:27

Springboot3学习(5、Druid使用及配置)

Springboot3学习——Druid使用及配置&#xff08;五&#xff09; 0、Druid 启动项目&#xff0c;我们可以看到Springboot3自带的数据库连接池是HikariPool&#xff0c;HikariPool的主要优点是高性能&#xff0c;而我们即将集成的Druid数据库连接池&#xff0c;主要有点则是丰富…

作者头像 李华
网站建设 2026/5/11 3:37:48

PDF-Extract-Kit部署指南:安全加固与权限管理

PDF-Extract-Kit部署指南&#xff1a;安全加固与权限管理 1. 引言 1.1 技术背景与业务需求 随着企业数字化转型的加速&#xff0c;PDF文档作为知识载体在科研、金融、教育等领域广泛应用。然而&#xff0c;传统PDF处理工具普遍存在结构化提取能力弱、自动化程度低等问题。PD…

作者头像 李华
网站建设 2026/5/2 18:18:59

PDF-Extract-Kit主题建模:自动分类文档内容

PDF-Extract-Kit主题建模&#xff1a;自动分类文档内容 1. 引言&#xff1a;智能文档提取的工程挑战与PDF-Extract-Kit的诞生 在科研、教育和企业办公场景中&#xff0c;PDF文档承载着大量结构化与非结构化信息。传统手动提取方式效率低下&#xff0c;尤其面对公式、表格、图…

作者头像 李华
网站建设 2026/5/11 0:41:05

espidf实现远程空调控制系统:完整示例

用ESP-IDF打造远程空调控制器&#xff1a;从零构建智能温控系统你有没有过这样的经历&#xff1f;夏天出差在外&#xff0c;心里却惦记着家里的老人怕热&#xff1b;冬天回家前&#xff0c;只希望能提前打开空调&#xff0c;进门就是暖意融融。传统空调只能靠遥控器操作&#x…

作者头像 李华
网站建设 2026/5/14 19:35:39

PDF-Extract-Kit需求管理:功能优先级排序方法

PDF-Extract-Kit需求管理&#xff1a;功能优先级排序方法 1. 引言&#xff1a;PDF智能提取工具箱的工程背景与挑战 1.1 工具定位与发展动因 在科研、教育和出版领域&#xff0c;PDF文档承载了大量结构化信息&#xff0c;包括文本、公式、表格和图像。然而&#xff0c;传统PD…

作者头像 李华