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

IDEA2023 SpringBoot整合MyBatis(三)

一、数据库表

CREATE TABLE students (id INT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(100) NOT NULL,age INT,gender ENUM('Male', 'Female', 'Other'),email VARCHAR(100) UNIQUE,phone_number VARCHAR(20),address VARCHAR(255),date_of_birth DATE,enrollment_date DATE,course VARCHAR(100)
);-- 插入10条学生数据
INSERT INTO students (name, age, gender, email, phone_number, address, date_of_birth, enrollment_date, course) VALUES
('John Doe', 20, 'Male', 'john.doe@example.com', '1234567890', '123 Main St, City', '2003-01-01', '2023-09-01', 'Computer Science'),
('Jane Smith', 21, 'Female', 'jane.smith@example.com', '0987654321', '456 Elm Rd, Town', '2002-02-02', '2023-09-01', 'Business Administration'),
('Michael Johnson', 19, 'Male', 'michael.johnson@example.com', '1122334455', '789 Oak Ave, Village', '2004-03-03', '2023-09-01', 'Electrical Engineering'),
('Emily Davis', 22, 'Female', 'emily.davis@example.com', '5544332211', '321 Pine Blvd, County', '2001-04-04', '2023-09-01', 'Mechanical Engineering'),
('William Brown', 20, 'Male', 'william.brown@example.com', '9988776655', '654 Cedar Ln, District', '2003-05-05', '2023-09-01', 'Civil Engineering'),
('Olivia Wilson', 21, 'Female', 'olivia.wilson@example.com', '4433221100', '987 Walnut St, Borough', '2002-06-06', '2023-09-01', 'Chemistry'),
('Benjamin Taylor', 19, 'Male', 'benjamin.taylor@example.com', '7766554433', '246 Maple Dr, Neighborhood', '2004-07-07', '2023-09-01', 'Physics'),
('Grace Anderson', 22, 'Female', 'grace.anderson@example.com', '3322110099', '852 Birch Rd, Hamlet', '2001-08-08', '2023-09-01', 'Mathematics'),
('Henry Thompson', 20, 'Male', 'henry.thompson@example.com', '6655443322', '587 Aspen St, Province', '2003-09-09', '2023-09-01', 'Biology'),
('Chloe Robinson', 21, 'Female', 'chloe.robinson@example.com', '9977665544', '101 Poplar Ave, Region', '2002-10-10', '2023-09-01', 'Psychology');

二、在pom文件中添加MyBatis相关依赖包,Mysql驱动依赖

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- mybatis依赖 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.3</version></dependency><!-- mysql依赖 --><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- mybatis测试依赖 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter-test</artifactId><version>3.0.3</version><scope>test</scope></dependency>

   当我们没有热部署的时候,我们必须在代码修改完后再重启程序,程序才会同步你修改的信息那么我们可以手动启动热部署-》》》

     <!-- 热部署工具 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency>

 

三、编写实体类

public class Student {// 定义属性private String name; // 姓名private int age; // 年龄private String gender; // 性别private String email; // 邮箱private String phoneNumber; // 电话号码private String address; // 地址private java.util.Date dateOfBirth; // 出生日期private java.util.Date enrollmentDate; // 入学日期private String course; // 课程...setXXX and getXXX and constructor and toString...

四、 编写映射类StudentMapper

 */
@Mapper
public interface StudentMapper  {// @Select("select * from student where id = #{id}")public List<Student> findById(int id);//  @Select("select * from student")public List<Student> findAll();
}

注意:如果不写@Select注解,则就需要写Mapper映射文件

五、编写Mapper映射文件

  mybatis – MyBatis 3 | 配置

<?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.hlx.springbootdemo1.mapper.StudentMapper"><select id="findById" parameterType="int" resultType="Student">SELECT * FROM student WHERE id = #{id}</select><select id="findAll" resultType="Student">select * from student</select></mapper>

六、编写配置文件

#mysql数据源配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/school?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=123456#mybatis
mybatis.mapper-locations=classpath*:mapper/*.xml#实体类的别名
mybatis.type-aliases-package=com.hlx.springbootdemo1.entity#日志
logging.pattern.console='%d{HH:mm:ss.SSS} %clr(%-5level) ---  [%-15thread] %cyan(%-50logger{50}):%msg%n'

 七、编写业务类

@Service
public class StudentService {@Autowiredprivate StudentMapper studentMapper;public List<Student> findAll(){return (studentMapper.findAll());}public Student findById(int id){return (studentMapper.findById(id));}
}

 八、编写控制器类

@RestController
public class StudentsController {@Autowiredprivate StudentService studentService;@GetMapping("/user/{id}")public Student findById(@PathVariable int id) {return studentService.findById(id);}@GetMapping("/user/all")public List<Student> findAll() {return studentService.findAll();}
}

九、启动类

@SpringBootApplication
@MapperScan("com.hlx.springbootdemo1.mapper")
public class SpringbootDemo1Application {public static void main(String[] args) {SpringApplication.run(SpringbootDemo1Application.class, args);}}

十、项目结构

十一、启动运行

相关文章:

IDEA2023 SpringBoot整合MyBatis(三)

一、数据库表 CREATE TABLE students (id INT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(100) NOT NULL,age INT,gender ENUM(Male, Female, Other),email VARCHAR(100) UNIQUE,phone_number VARCHAR(20),address VARCHAR(255),date_of_birth DATE,enrollment_date DATE,cours…...

【Apache Paimon】-- 6 -- 清理过期数据

目录 1、简要介绍 2、操作方式和步骤 2.1、调整快照文件过期时间 2.2、设置分区过期时间 2.2.1、举例1 2.2.2、举例2 2.3、清理废弃文件 3、参考 1、简要介绍 清理 paimon (表)过期数据可以释放存储空间,优化资源利用并提升系统运行效率等。本文将介绍如何清理 Paim…...

C语言数据结构——详细讲解 双链表

从单链表到双链表&#xff1a;数据结构的演进与优化 前言一、单链表回顾二、单链表的局限性三、什么是双链表四、双链表的优势1.双向遍历2.不带头双链表的用途3.带头双链表的用途 五、双链表的操作双链表的插入操作&#xff08;一&#xff09;双链表的尾插操作&#xff08;二&a…...

Shell脚本基础(4):条件判断

内容预览 ≧∀≦ゞ Shell脚本基础&#xff08;4&#xff09;&#xff1a;条件判断声明导语基本的if语句结构数值比较运算符文件测试运算符扩展&#xff1a;使用elif和else使用&&和||结合条件判断小结 Shell脚本基础&#xff08;4&#xff09;&#xff1a;条件判断 声明…...

在 Swift 中实现字符串分割问题:以字典中的单词构造句子

文章目录 前言摘要描述题解答案题解代码题解代码分析示例测试及结果时间复杂度空间复杂度总结 前言 本题由于没有合适答案为以往遗留问题&#xff0c;最近有时间将以往遗留问题一一完善。 LeetCode - #140 单词拆分 II 不积跬步&#xff0c;无以至千里&#xff1b;不积小流&…...

win10中使用ffmpeg和MediaMTX 推流rtsp视频

在win10上测试下ffmpeg推流rtsp视频&#xff0c;需要同时用到流媒体服务器MediaMTX 。ffmpeg推流到流媒体服务器MediaMTX &#xff0c;其他客户端从流媒体服务器拉流。 步骤如下&#xff1a; 1 下载MediaMTX github: Release v1.9.3 bluenviron/mediamtx GitHub​​​​​…...

16. 【.NET 8 实战--孢子记账--从单体到微服务】--汇率获取定时器

这篇文章我们将一起编写这个系列专栏中第一个和外部系统交互的功能&#xff1a;获取每日汇率。下面我们一起来编写代码吧。 一、需求 根据文章标题可知&#xff0c;在这片文章中我们只进行汇率的获取和写入数据库。 编号需求说明1获取每日汇率1. 从第三方汇率API中获取汇率信…...

C#元组详解:创建、访问与解构

在C#中&#xff0c;元组&#xff08;Tuple&#xff09;是一种数据结构&#xff0c;用于将多个元素组合成一个单一的对象。元组可以包含不同类型的元素&#xff0c;并且每个元素都有一个指定的位置&#xff08;索引&#xff09;。元组在需要临时组合多个值而不想创建自定义类时非…...

wsl2安装

Windows Subsystem for Linux 2 (WSL2) 是 Windows 10 和 Windows 11 中用于运行 Linux 二进制可执行文件的兼容层。WSL2 是 WSL 的最新版本&#xff0c;提供了更快的文件系统性能和完整的系统调用兼容性。本教程将指导你如何在 Windows 系统上安装 WSL2。 前提条件 操作系统要…...

android studio无法下载,Could not GET xxx, Received status code 400

-- 1. 使用下面的地址代替 原地址: distributionUrlhttps\://services.gradle.org/distributions/gradle-6.5-all.zip 镜像地址: distributionUrlhttps\://downloads.gradle-dn.com/distributions/gradle-6.5-all.zips 上面的已经不好用了 https\://mirrors.cloud.tencent.c…...

RUST学习教程-安装教程

文章目录 参考文档安装教程更新卸载 参考文档 https://course.rs/first-try/installation.html 安装教程 Linux或者mac安装教程 curl --proto https --tlsv1.2 https://sh.rustup.rs -sSf | sh安装完成&#xff0c;当出现command not found的时候&#xff0c;需要source一下…...

redis6.0之后的多线程版本的问题

一、redis早期版本和新版本的讨论 这个问题其实有些废话&#xff0c;哪个版本肯定都有不同啊。其实这里主要是提到的网上的大家对Redis6.0中的多线程版本的不同即以前宣传的Redis是单线程程版的&#xff0c;之后变成了多线程版本的。网上对这个讨论非常激烈&#xff0c;反正各…...

python的 pandas.Dataframe 和 pandas.Series基础内容

目录 0 有一个比较麻烦琐碎的地方 1 python pandas.Dataframe 2 pd.concat() 可以合并 pd.Dataframe 2.1 pd.concat() 合并规则 3 pd.Dataframe.drop() 删除行列的操作 4 pd.Dataframe 列操作 5 pd.Dataframe 行操作 5.1 sample_dataframe2.head(n2) 取前面的n行&…...

golang学习5

为结构体添加方法 异常处理过程...

【C语言】11月第二次测试 ing

文章目录 1.输入n名同学的成绩和学号&#xff0c;对成绩排序&#xff0c;输出对应学号 要求重复的学号重新输入 计算n名同学的平均值&#xff0c;对小于60分的同学删除分数 大于60分的同学输出&#xff1a;优秀&#xff1a;几人&#xff0c;良好&#xff1a;几人&#xff0c;中…...

行列式的理解与计算:线性代数中的核心概念

开发领域&#xff1a;前端开发 | AI 应用 | Web3D | 元宇宙 技术栈&#xff1a;JavaScript、React、ThreeJs、WebGL、Go 经验经验&#xff1a;6 年 前端开发经验&#xff0c;专注于图形渲染和 AI 技术 开源项目&#xff1a;github 简智未来、数字孪生引擎、前端面试题 大家好&a…...

按出生日期排序(结构体专题)

题目描述 送人玫瑰手有余香&#xff0c;小明希望自己能带给他人快乐&#xff0c;于是小明在每个好友生日的时候发去一份生日祝福。小明希望将自己的通讯录按好友的生日排序排序&#xff0c;这样就查看起来方便多了&#xff0c;也避免错过好友的生日。为了小明的美好愿望&#x…...

【C++】拆分详解 - 多态

文章目录 一、概念二、定义和实现1. 多态的构成条件2. 虚函数2.1 虚函数的重写/覆盖2.2 虚函数重写的两个例外 3. override 和 final关键字4. 重载/重写/隐藏的对比5. 例题 三、纯虚函数和抽象类四、多态的原理1. 虚函数表2. 实现原理3. 动态绑定和静态绑定 总结 一、概念 多态…...

Python世界:力扣题解875,珂珂爱吃香蕉,中等

Python世界&#xff1a;力扣题解875&#xff0c;珂珂爱吃香蕉&#xff0c;中等 任务背景思路分析代码实现坑点排查测试套件本文小结 任务背景 问题来自力扣题目875 Koko Eating Bananas&#xff0c;大意如下&#xff1a; Koko loves to eat bananas. There are n piles of bana…...

Java设计模式 —— Java七大设计原则详解

文章目录 前言一、单一职责原则1、概述2、案例演示 二、接口隔离原则1、概述2、案例演示 三、依赖倒转原则1、概述2、案例演示 四、里氏替换原则1、概述2、案例演示 五、开闭原则1、概述2、案例演示 六、迪米特法则1、概述2、案例演示 七、合成/聚合复用原则1、概述2、组合3、聚…...

SpringBoot学习记录(六)配置文件参数化

SpringBoot学习记录&#xff08;六&#xff09;配置文件参数化 一、参数提取到配置文件中二、yml配置文件三、ConfigurationProperties注解实现批量属性注入 一、参数提取到配置文件中 定义在代码中的参数的值分散在各个不同的文件中&#xff0c;不便于后期维护管理&#xff0…...

android 使用MediaPlayer实现音乐播放--获取音乐数据

前面已经添加了权限&#xff0c;有权限后可以去数据库读取音乐文件&#xff0c;一般可以获取全部音乐、专辑、歌手、流派等。 1. 获取全部音乐数据 class MusicHelper {companion object {SuppressLint("Range")fun getMusic(context: Context): MutableList<Mu…...

.net 8使用hangfire实现库存同步任务

C# 使用HangFire 第一章:.net Framework 4.6 WebAPI 使用Hangfire 第二章:net 8使用hangfire实现库存同步任务 文章目录 C# 使用HangFire前言项目源码一、项目架构二、项目服务介绍HangFire服务结构解析HangfireCollectionExtensions 类ModelHangfireSettingsHttpAuthInfoUs…...

第 22 章 - Go语言 测试与基准测试

在Go语言中&#xff0c;测试是一个非常重要的部分&#xff0c;它帮助开发者确保代码的正确性、性能以及可维护性。Go语言提供了一套标准的测试工具&#xff0c;这些工具可以帮助开发者编写单元测试、表达式测试&#xff08;通常也是指单元测试中的断言&#xff09;、基准测试等…...

VB.Net笔记-更新ing

目录 1.1 设置默认VS的开发环境为VB.NET&#xff08;2024/11/18&#xff09; 1.2 新建一个“Hello&#xff0c;world”的窗体&#xff08;2024/11/18&#xff09; 1.3 计算圆面积的小程序&#xff08;2024/11/18&#xff09; 显示/隐式 声明 &#xff08;2024/11/18&…...

centos 服务器 docker 使用代理

宿主机使用代理 在宿主机的全局配置文件中添加代理信息 vim /etc/profile export http_proxyhttp://127.0.0.1:7897 export https_proxyhttp://127.0.0.1:7897 export no_proxy"localhost,127.0.0.1,::1,172.171.0.0" docker 命令使用代理 例如我想在使用使用 do…...

python语言基础

1. 基础语法 Q: Python 中的变量与数据类型有哪些&#xff1f; A: Python 支持多种数据类型&#xff0c;包括数字&#xff08;整数 int、浮点数 float、复数 complex&#xff09;、字符串 str、列表 list、元组 tuple、字典 dict 和集合 set。每种数据类型都有其特定的用途和…...

Python中的Apriori库详解

文章目录 Python中的Apriori库详解一、引言二、Apriori算法原理与Python实现1、Apriori算法原理2、Python实现1.1、数据准备1.2、转换数据1.3、计算频繁项集1.4、提取关联规则 三、案例分析1、导入必要的库2、准备数据集3、数据预处理4、应用Apriori算法5、生成关联规则6、打印…...

MongoDB比较查询操作符中英对照表及实例详解

mongodb比较查询操作符中英表格一览表 NameDescription功能$eqMatches values that are equal to a specified value.匹配值等于指定值。$gtMatches values that are greater than a specified value.匹配值大于指定值。$gteMatches values that are greater than or equal to…...

掌上单片机实验室 – RT-Thread + ROS2 初探(25)

在初步尝试RT-Thread之后&#xff0c;一直在琢磨如何进一步感受它的优点&#xff0c;因为前面只是用了它的内核&#xff0c;感觉和FreeRTOS、uCOS等RTOS差别不大&#xff0c;至于它们性能、可靠性上的差异&#xff0c;在这种学习性的程序中&#xff0c;很难有所察觉。 RT-Threa…...