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

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# 数据源类型: 默认的是 Hikaritype: 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.ssmtestconfiguration:# 开启驼峰命名转换map-underscore-to-camel-case: true# 开启日志#log-impl: org.apache.ibatis.logging.stdout.StdOutImpllog-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/settertoString 方法}

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 的步骤。

相关文章:

  • 【MySQL】分库分表
  • USB - 常用开发工具
  • C++ -- 红黑树的基本操作
  • 从零开始精通Onvif之图片抓拍
  • Mybatis --- 动态SQL 和数据库连接池
  • docker Pulling fs layer 含义
  • DeepSpeed Monitoring Comm. Logging
  • 速盾:什么是高防CDN?有什么优势?
  • 网络安全复习笔记
  • 入门Rabbitmq
  • QT中出现ASSERT failure in QList::at: “index out of range”的情况和解决办法
  • 全面解析AdaBoost:多分类、逻辑回归与混合分类器的实现
  • 基于DPU的云原生裸金属服务快速部署及存储解决方案
  • Jupyter Notebook 中 %run 魔法命令
  • 高级优化理论与方法(十五)
  • 【翻译】babel对TC39装饰器草案的实现
  • 【知识碎片】第三方登录弹窗效果
  • Angular 2 DI - IoC DI - 1
  • el-input获取焦点 input输入框为空时高亮 el-input值非法时
  • Idea+maven+scala构建包并在spark on yarn 运行
  • JS题目及答案整理
  • js学习笔记
  • k8s如何管理Pod
  • learning koa2.x
  • Node.js 新计划:使用 V8 snapshot 将启动速度提升 8 倍
  • overflow: hidden IE7无效
  • redis学习笔记(三):列表、集合、有序集合
  • spring security oauth2 password授权模式
  • SQLServer之索引简介
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • WordPress 获取当前文章下的所有附件/获取指定ID文章的附件(图片、文件、视频)...
  • 解析 Webpack中import、require、按需加载的执行过程
  • 马上搞懂 GeoJSON
  • 我是如何设计 Upload 上传组件的
  • Python 之网络式编程
  • 湖北分布式智能数据采集方法有哪些?
  • 曜石科技宣布获得千万级天使轮投资,全方面布局电竞产业链 ...
  • # centos7下FFmpeg环境部署记录
  • #LLM入门|Prompt#2.3_对查询任务进行分类|意图分析_Classification
  • #NOIP 2014#Day.2 T3 解方程
  • (21)起落架/可伸缩相机支架
  • (22)C#传智:复习,多态虚方法抽象类接口,静态类,String与StringBuilder,集合泛型List与Dictionary,文件类,结构与类的区别
  • (30)数组元素和与数字和的绝对差
  • (C)一些题4
  • (Repost) Getting Genode with TrustZone on the i.MX
  • (vue)el-cascader级联选择器按勾选的顺序传值,摆脱层级约束
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (二)JAVA使用POI操作excel
  • (附源码)ssm基于微信小程序的疫苗管理系统 毕业设计 092354
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (免费领源码)python+django+mysql线上兼职平台系统83320-计算机毕业设计项目选题推荐
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (五)c52学习之旅-静态数码管
  • (一)eclipse Dynamic web project 工程目录以及文件路径问题
  • (转)四层和七层负载均衡的区别