一、实验目的
- 掌握 Spring Boot 项目基本结构
- 熟悉 MyBatis 的基本使用(Mapper、SQL 映射)
- 实现后端接口并通过 HTTP 请求访问
- 实现数据库数据查询并返回给前端
二、实验环境
- JDK:17
- 开发工具: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️⃣ 实体类 User
package 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;
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user")
List<User> 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;
@Service
public class UserService {
private final UserMapper userMapper;
public UserService(UserMapper userMapper) {
this.userMapper = userMapper;
}
public List<User> 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;
@RestController
@RequestMapping("/api/users")
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping
public List<User> getUsers() {
return userService.getAllUsers();
}
}
5️⃣ 数据库初始化
schema.sql
CREATE TABLE user (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50),
email VARCHAR(50)
);
data.sql
INSERT INTO user (username, email) VALUES ('alice', 'alice@example.com');
INSERT INTO user (username, email) VALUES ('bob', 'bob@example.com');
6️⃣ 配置文件
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true
mybatis.configuration.map-underscore-to-camel-case=true
五、运行结果
启动项目后访问:
http://localhost:8080/api/users
返回结果:
[
{
"id": 1,
"username": "alice",
"email": "alice@example.com"
},
{
"id": 2,
"username": "bob",
"email": "bob@example.com"
}
]
六、关键问题与解决
❌ 问题1:JDK版本不兼容
错误:
类文件具有错误的版本 61.0,应为 52.0
原因:
- Spring Boot 3 必须使用 JDK 17+
- 系统默认是 JDK 1.8
✅ 解决:
$env:JAVA_HOME="C:\Users\33502\.jdks\ms-17.0.18"
$env:PATH="$env:JAVA_HOME\bin;$env:PATH"
❌ 问题2:Maven 无法下载依赖(证书问题)
错误:
PKIX path building failed
✅ 解决:
使用阿里云镜像(已自动生效)或重新执行:
mvn clean install
❌ 问题3:mvnw.cmd 无法执行
原因:
- 不在项目目录
✅ 解决:
cd C:\Users\33502\Desktop\hello
.\mvnw.cmd spring-boot:run
七、实验总结
本实验成功实现了:
- Spring Boot 项目搭建
- MyBatis 查询数据库
- RESTful 接口开发
- H2 内存数据库初始化
- 前后端数据交互
通过本实验,掌握了从数据库 → 后端 → 接口 的完整开发流程。