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

【Spring Boot】Spring Boot结合MyBatis简单实现学生信息管理模块

实战:实现学生信息管理模块

  1. 环境准备
  • JDK
  • Spring Boot
  • MyBatis
  1. 创建Spring Boot项目
    使用Spring Initializr创建一个新的Spring Boot项目,并添加以下依赖:
  • Spring Web
  • MyBatis Framework
  • MySQL Driver
  1. 数据库设计
    在MySQL数据库中创建一个名为studentdb的数据库,并创建一个名为students的表,表结构如下:
CREATE TABLE `students` (`id` int NOT NULL AUTO_INCREMENT,`name` varchar(255) NOT NULL,`email` varchar(255) NOT NULL,`address` varchar(255) NOT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  1. 配置数据源和MyBatis
    application.properties文件中添加以下配置:
# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/studentdb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# MyBatis配置
mybatis.type-aliases-package=com.example.demo.entity
mybatis.mapper-locations=classpath:mapper/*.xml
  1. 定义实体类
    创建一个名为Student的实体类,代码如下:
public class Student {private int id;private String name;private String email;private String address;// getter和setter方法省略
}
  1. 定义Mapper接口
    创建一个名为StudentMapper的Mapper接口,代码如下:
@Mapper
public interface StudentMapper {List<Student> getAllStudents();Student getStudentById(int id);int addStudent(Student student);int updateStudent(Student student);int deleteStudent(int id);
}
  1. 定义Mapper XML配置文件
    resources/mapper下创建一个名为StudentMapper.xml的配置文件,代码如下:
<mapper namespace="com.example.demo.mapper.StudentMapper"><resultMap id="StudentMap" type="com.example.demo.entity.Student"><id property="id" column="id" /><result property="name" column="name" /><result property="email" column="email" /><result property="address" column="address" /></resultMap><select id="getAllStudents" resultMap="StudentMap">SELECT * FROM students</select><select id="getStudentById" resultMap="StudentMap">SELECT * FROM students WHERE id=#{id}</select><insert id="addStudent" parameterType="com.example.demo.entity.Student">INSERT INTO students(name, email, address) VALUES(#{name}, #{email}, #{address})</insert><update id="updateStudent" parameterType="com.example.demo.entity.Student">UPDATE students SET name=#{name}, email=#{email}, address=#{address} WHERE id=#{id}</update><delete id="deleteStudent">DELETE FROM students WHERE id=#{id}</delete>
</mapper>
  1. 实现Controller
    创建一个名为StudentController的Controller,代码如下:
@RestController
@RequestMapping("/api")
public class StudentController {@Autowiredprivate StudentMapper studentMapper;@GetMapping("/students")public List<Student> getAllStudents() {return studentMapper.getAllStudents();}@GetMapping("/students/{id}")public Student getStudentById(@PathVariable int id) {return studentMapper.getStudentById(id);}@PostMapping("/students")public int addStudent(@RequestBody Student student) {return studentMapper.addStudent(student);}@PutMapping("/students")public int updateStudent(@RequestBody Student student) {return studentMapper.updateStudent(student);}@DeleteMapping("/students/{id}")public int deleteStudent(@PathVariable int id) {return studentMapper.deleteStudent(id);}
}
  1. 测试API
    使用Postman或其他工具测试API,例如:
  • GET http://localhost:8080/api/students
  • GET http://localhost:8080/api/students/1
  • POST http://localhost:8080/api/students
    请求体:
    {"name": "张三","email": "zhangsan@example.com","address": "北京市海淀区"
    }
    
  • PUT http://localhost:8080/api/students
    请求体:
    {"id": 1,"name": "张三","email": "zhangsan@example.com","address": "北京市海淀区"
    }
    
  • DELETE http://localhost:8080/api/students/1

完成以上步骤后,就可以使用Spring Boot和MyBatis实现一个简单的学生信息管理模块了。

http://www.lryc.cn/news/146046.html

相关文章:

  • 【Java List与Map】List<T> Map与Map List<T>的区别(126)
  • 【FreeRTOS】常用函数总结
  • The Cherno——OpenGL
  • linux中学习控制进程的要点
  • C++Qt QSS要注意的坑
  • LeetCode每日一题:56. 合并区间(2023.8.27 C++)
  • 电视盒子什么牌子好?经销商整理线下热销电视盒子品牌排行榜
  • JavaScript关于函数的小挑战
  • 机器学习深度学习——针对序列级和词元级应用微调BERT
  • 重启Mysql时报错rm: cannot remove ‘/var/lock/subsys/mysql‘: Permission denied
  • [C/C++]指针详讲-让你不在害怕指针
  • 无涯教程-Android - Frame Layout函数
  • docker desktop安装es 并连接elasticsearch-head:5
  • 计网(第四章)(网络层)(六)
  • 科研无人机平台P600进阶版,突破科研难题!
  • Apache的简单介绍(LAMP架构+搭建Discuz论坛)
  • CDL基础原理
  • WPF基础入门-Class7-WPF-MVVN框架
  • C语言练习题第三弹!!!绝对典中典!!!
  • Jedis
  • Linux 使用TCP_INFO查询TCP连接的状态信息
  • 软件测试案例 | 气象探测库存管理系统的集成测试计划
  • vue点击按钮重新加载页面(vue第一次加载页面点击按钮出现页面刷新问题之后一切正常)
  • 软件工程(十一) 系统设计分类
  • 数字转中文大写金额
  • Java——HashMap和HashTable的区别
  • Docker去除sudo权限
  • 【ROS系统】Ubuntu22.04系统中安装ROS2系统_ubuntu 安装ros2_GoesM
  • MySQL8.0.22安装过程记录(个人笔记)
  • Python中pip和conda的爱恨情仇