|
|
|
|
@ -1,278 +0,0 @@
|
|
|
|
|
好的,从配置数据库开始,以下是详细的步骤,帮助你在 Spring Boot 项目中连接 MySQL 数据库,并结合 MyBatis 进行开发。
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
### **1. 配置 MySQL 数据库**
|
|
|
|
|
#### **1.1 创建数据库和表**
|
|
|
|
|
打开 Navicat 或 MySQL 命令行工具,创建一个数据库和表。例如,创建一个名为 `mydatabase` 的数据库和一个名为 `user` 的表:
|
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
-- 创建数据库
|
|
|
|
|
CREATE DATABASE mydatabase;
|
|
|
|
|
|
|
|
|
|
-- 使用数据库
|
|
|
|
|
USE mydatabase;
|
|
|
|
|
|
|
|
|
|
-- 创建表
|
|
|
|
|
CREATE TABLE user (
|
|
|
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
|
|
|
name VARCHAR(50) NOT NULL,
|
|
|
|
|
email VARCHAR(100) NOT NULL
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### **1.2 插入测试数据**
|
|
|
|
|
为了方便后续测试,可以插入一些初始数据:
|
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
INSERT INTO user (name, email) VALUES ('Alice', 'alice@example.com');
|
|
|
|
|
INSERT INTO user (name, email) VALUES ('Bob', 'bob@example.com');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
### **2. 配置 Spring Boot 项目以连接 MySQL**
|
|
|
|
|
#### **2.1 配置 `application.properties`**
|
|
|
|
|
在 Spring Boot 项目的 `src/main/resources/application.properties` 文件中,添加以下配置,以连接 MySQL 数据库:
|
|
|
|
|
|
|
|
|
|
```properties
|
|
|
|
|
# 数据源配置
|
|
|
|
|
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase?useSSL=false&serverTimezone=UTC
|
|
|
|
|
spring.datasource.username=root # 替换为你的 MySQL 用户名
|
|
|
|
|
spring.datasource.password=your_password # 替换为你的 MySQL 密码
|
|
|
|
|
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
|
|
|
|
|
|
|
|
|
# MyBatis 配置
|
|
|
|
|
mybatis.mapper-locations=classpath:mappers/*.xml
|
|
|
|
|
mybatis.type-aliases-package=com.example.demo.entity
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**注意**:
|
|
|
|
|
- `spring.datasource.url` 中的 `mydatabase` 是你创建的数据库名称。
|
|
|
|
|
- `useSSL=false` 是为了避免 SSL 连接问题,`serverTimezone=UTC` 是为了设置时区。
|
|
|
|
|
- 替换 `username` 和 `password` 为你的 MySQL 数据库用户名和密码。
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
### **3. 添加 MyBatis 相关配置**
|
|
|
|
|
#### **3.1 创建实体类**
|
|
|
|
|
在 `src/main/java/com/example/demo/entity` 下创建 `User.java` 实体类,对应数据库中的 `user` 表:
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
package com.example.demo.entity;
|
|
|
|
|
|
|
|
|
|
public class User {
|
|
|
|
|
private Integer id;
|
|
|
|
|
private String name;
|
|
|
|
|
private String email;
|
|
|
|
|
|
|
|
|
|
// Getters and Setters
|
|
|
|
|
public Integer getId() {
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setId(Integer id) {
|
|
|
|
|
this.id = id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setName(String name) {
|
|
|
|
|
this.name = name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getEmail() {
|
|
|
|
|
return email;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setEmail(String email) {
|
|
|
|
|
this.email = email;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### **3.2 创建 Mapper 接口**
|
|
|
|
|
在 `src/main/java/com/example/demo/mapper` 下创建 `UserMapper.java` 接口:
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
package com.example.demo.mapper;
|
|
|
|
|
|
|
|
|
|
import com.example.demo.entity.User;
|
|
|
|
|
import org.apache.ibatis.annotations.Mapper;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
@Mapper
|
|
|
|
|
public interface UserMapper {
|
|
|
|
|
List<User> getAllUsers();
|
|
|
|
|
User getUserById(Integer id);
|
|
|
|
|
void insertUser(User user);
|
|
|
|
|
void updateUser(User user);
|
|
|
|
|
void deleteUser(Integer id);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### **3.3 创建 Mapper XML 文件**
|
|
|
|
|
在 `src/main/resources/mappers` 文件夹下创建 `UserMapper.xml` 文件,定义 SQL 映射:
|
|
|
|
|
|
|
|
|
|
```xml
|
|
|
|
|
<?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.example.demo.mapper.UserMapper">
|
|
|
|
|
<select id="getAllUsers" resultType="com.example.demo.entity.User">
|
|
|
|
|
SELECT * FROM user
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<select id="getUserById" parameterType="int" resultType="com.example.demo.entity.User">
|
|
|
|
|
SELECT * FROM user WHERE id = #{id}
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
<insert id="insertUser" parameterType="com.example.demo.entity.User">
|
|
|
|
|
INSERT INTO user (name, email) VALUES (#{name}, #{email})
|
|
|
|
|
</insert>
|
|
|
|
|
|
|
|
|
|
<update id="updateUser" parameterType="com.example.demo.entity.User">
|
|
|
|
|
UPDATE user SET name = #{name}, email = #{email} WHERE id = #{id}
|
|
|
|
|
</update>
|
|
|
|
|
|
|
|
|
|
<delete id="deleteUser" parameterType="int">
|
|
|
|
|
DELETE FROM user WHERE id = #{id}
|
|
|
|
|
</delete>
|
|
|
|
|
</mapper>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
### **4. 创建 Service 和 Controller**
|
|
|
|
|
#### **4.1 创建 Service**
|
|
|
|
|
在 `src/main/java/com/example/demo/service` 下创建 `UserService.java`:
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
package com.example.demo.service;
|
|
|
|
|
|
|
|
|
|
import com.example.demo.entity.User;
|
|
|
|
|
import com.example.demo.mapper.UserMapper;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class UserService {
|
|
|
|
|
@Autowired
|
|
|
|
|
private UserMapper userMapper;
|
|
|
|
|
|
|
|
|
|
public List<User> getAllUsers() {
|
|
|
|
|
return userMapper.getAllUsers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public User getUserById(Integer id) {
|
|
|
|
|
return userMapper.getUserById(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void insertUser(User user) {
|
|
|
|
|
userMapper.insertUser(user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void updateUser(User user) {
|
|
|
|
|
userMapper.updateUser(user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void deleteUser(Integer id) {
|
|
|
|
|
userMapper.deleteUser(id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### **4.2 创建 Controller**
|
|
|
|
|
在 `src/main/java/com/example/demo/controller` 下创建 `UserController.java`:
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
package com.example.demo.controller;
|
|
|
|
|
|
|
|
|
|
import com.example.demo.entity.User;
|
|
|
|
|
import com.example.demo.service.UserService;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/users")
|
|
|
|
|
public class UserController {
|
|
|
|
|
@Autowired
|
|
|
|
|
private UserService userService;
|
|
|
|
|
|
|
|
|
|
@GetMapping
|
|
|
|
|
public List<User> getAllUsers() {
|
|
|
|
|
return userService.getAllUsers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/{id}")
|
|
|
|
|
public User getUserById(@PathVariable Integer id) {
|
|
|
|
|
return userService.getUserById(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping
|
|
|
|
|
public void insertUser(@RequestBody User user) {
|
|
|
|
|
userService.insertUser(user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PutMapping
|
|
|
|
|
public void updateUser(@RequestBody User user) {
|
|
|
|
|
userService.updateUser(user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@DeleteMapping("/{id}")
|
|
|
|
|
public void deleteUser(@PathVariable Integer id) {
|
|
|
|
|
userService.deleteUser(id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
### **5. 启动项目并测试**
|
|
|
|
|
#### **5.1 启动项目**
|
|
|
|
|
在 VSCode 的终端中运行以下命令启动项目:
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
mvn spring-boot:run
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### **5.2 测试接口**
|
|
|
|
|
使用 Postman 或浏览器测试接口:
|
|
|
|
|
- **获取所有用户**:`GET http://localhost:8080/users`
|
|
|
|
|
- **根据 ID 获取用户**:`GET http://localhost:8080/users/{id}`
|
|
|
|
|
- **添加用户**:`POST http://localhost:8080/users`,请求体为 JSON:
|
|
|
|
|
```json
|
|
|
|
|
{
|
|
|
|
|
"name": "Charlie",
|
|
|
|
|
"email": "charlie@example.com"
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
- **更新用户**:`PUT http://localhost:8080/users`,请求体为 JSON:
|
|
|
|
|
```json
|
|
|
|
|
{
|
|
|
|
|
"id": 1,
|
|
|
|
|
"name": "Alice Updated",
|
|
|
|
|
"email": "alice@example.com"
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
- **删除用户**:`DELETE http://localhost:8080/users/{id}`
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
### **6. 使用 Navicat 查看数据库操作结果**
|
|
|
|
|
在 Navicat 中连接到 `mydatabase` 数据库,查看 `user` 表的内容,确认数据是否正确插入、更新或删除。
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
### **7. 注意事项**
|
|
|
|
|
1. **数据库连接问题**:如果无法连接到 MySQL,请检查用户名、密码和数据库名称是否正确。
|
|
|
|
|
2. **MyBatis 配置问题**:确保 `application.properties` 中的 `mybatis.mapper-locations` 和 `mybatis.type-aliases-package` 配置正确。
|
|
|
|
|
3. **SQL 语法问题**:检查 `UserMapper.xml` 中的 SQL 是否正确,尤其是表名和字段名是否与数据库一致。
|
|
|
|
|
|
|
|
|
|
如果在开发过程中遇到任何问题,欢迎随时提问!
|