【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的表,表结构如下:
sql 复制代码
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文件中添加以下配置:
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的实体类,代码如下:
java 复制代码
public class Student {
    private int id;
    private String name;
    private String email;
    private String address;

    // getter和setter方法省略
}
  1. 定义Mapper接口
    创建一个名为StudentMapper的Mapper接口,代码如下:
java 复制代码
@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的配置文件,代码如下:
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,代码如下:
java 复制代码
@RestController
@RequestMapping("/api")
public class StudentController {
    @Autowired
    private 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,例如:

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

相关推荐
百***37486 分钟前
Spring Boot 中 RabbitMQ 的使用
spring boot·rabbitmq·java-rabbitmq
IT_陈寒6 分钟前
Python 3.12新特性实战:5个让你的代码效率翻倍的隐藏技巧!
前端·人工智能·后端
Victor35627 分钟前
Redis(125)Redis在社交网络中的应用有哪些?
后端
Victor35630 分钟前
Redis(124)Redis在电商系统中的应用有哪些?
后端
武子康31 分钟前
Java-173 Neo4j + Spring Boot 实战:从 Driver 到 Repository 的整合与踩坑
java·数据库·spring boot·后端·spring·nosql·neo4j
凌波粒33 分钟前
SpringMVC基础教程(2)--Controller/RestFul风格/JSON/数据转发和重定向
java·后端·spring·json·restful
李慕婉学姐1 小时前
Springboot智慧旅游管理系统6w63eon8(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·旅游
程序员爱钓鱼1 小时前
Python 编程实战 · 实用工具与库 — Flask 路由与模板
前端·后端·python
JIngJaneIL2 小时前
旅游|内蒙古景点旅游|基于Springboot+Vue的内蒙古景点旅游管理系统设计与实现(源码+数据库+文档)
java·vue.js·spring boot·论文·旅游·毕设·内蒙古景点旅游
程序员爱钓鱼2 小时前
Python 编程实战 · 实用工具与库 — Django 项目结构简介
后端·python·面试