SpringBoot + MyBatis + H2 实验报告
一、实验目的掌握 Spring Boot 项目基本结构熟悉 MyBatis 的基本使用Mapper、SQL 映射实现后端接口并通过 HTTP 请求访问实现数据库数据查询并返回给前端二、实验环境JDK17开发工具IntelliJ IDEA构建工具Maven框架Spring Boot 3.4.3持久层MyBatis数据库H2内存数据库三、项目结构hello├── src│ ├── main│ │ ├── java/com/example/hello│ │ │ ├── controller│ │ │ ├── service│ │ │ ├── mapper│ │ │ └── entity│ │ └── resources│ │ ├── application.properties│ │ ├── schema.sql│ │ └── data.sql├── pom.xml四、核心代码实现1️⃣ 实体类 Userpackage com.example.hello.entity;public class User {private Integer id;private String username;private String email;public Integer getId() { return id; }public void setId(Integer id) { this.id id; }public String getUsername() { return username; }public void setUsername(String username) { this.username username; }public String getEmail() { return email; }public void setEmail(String email) { this.email email; }}2️⃣ Mapper 接口package com.example.hello.mapper;import com.example.hello.entity.User;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;import java.util.List;Mapperpublic interface UserMapper {Select(SELECT * FROM user)ListUser findAll();}3️⃣ Service 层package com.example.hello.service;import com.example.hello.entity.User;import com.example.hello.mapper.UserMapper;import org.springframework.stereotype.Service;import java.util.List;Servicepublic class UserService {private final UserMapper userMapper;public UserService(UserMapper userMapper) {this.userMapper userMapper;}public ListUser getAllUsers() {return userMapper.findAll();}}4️⃣ Controller 层package com.example.hello.controller;import com.example.hello.entity.User;import com.example.hello.service.UserService;import org.springframework.web.bind.annotation.*;import java.util.List;RestControllerRequestMapping(/api/users)public class UserController {private final UserService userService;public UserController(UserService userService) {this.userService userService;}GetMappingpublic ListUser getUsers() {return userService.getAllUsers();}}5️⃣ 数据库初始化schema.sqlCREATE TABLE user (id INT PRIMARY KEY AUTO_INCREMENT,username VARCHAR(50),email VARCHAR(50));data.sqlINSERT INTO user (username, email) VALUES (alice, aliceexample.com);INSERT INTO user (username, email) VALUES (bob, bobexample.com);6️⃣ 配置文件spring.datasource.urljdbc:h2:mem:testdbspring.datasource.driver-class-nameorg.h2.Driverspring.datasource.usernamesaspring.datasource.passwordspring.h2.console.enabledtruemybatis.configuration.map-underscore-to-camel-casetrue五、运行结果启动项目后访问http://localhost:8080/api/users返回结果[{id: 1,username: alice,email: aliceexample.com},{id: 2,username: bob,email: bobexample.com}]六、关键问题与解决❌ 问题1JDK版本不兼容错误类文件具有错误的版本 61.0应为 52.0原因Spring Boot 3 必须使用 JDK 17系统默认是 JDK 1.8✅ 解决$env:JAVA_HOMEC:\Users\33502\.jdks\ms-17.0.18$env:PATH$env:JAVA_HOME\bin;$env:PATH❌ 问题2Maven 无法下载依赖证书问题错误PKIX path building failed✅ 解决使用阿里云镜像已自动生效或重新执行mvn clean install❌ 问题3mvnw.cmd 无法执行原因不在项目目录✅ 解决cd C:\Users\33502\Desktop\hello.\mvnw.cmd spring-boot:run七、实验总结本实验成功实现了Spring Boot 项目搭建MyBatis 查询数据库RESTful 接口开发H2 内存数据库初始化前后端数据交互通过本实验掌握了从数据库 → 后端 → 接口 的完整开发流程。