SpringBoot-Mybatis整合+Restful风格 + (CRUD简单案例)
发布人:shili8
发布时间:2025-02-14 09:13
阅读次数:0
**Spring Boot + MyBatis 整合 + Restful 风格 + CRUD 简单案例**
在本文中,我们将介绍如何使用 Spring Boot 和 MyBatis 来实现一个简单的 CRUD(Create、Read、Update、Delete)操作。我们将使用 Restful 风格来设计 API。
###依赖配置首先,我们需要在 `pom.xml` 文件中添加必要的依赖:
xml<dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- Test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
### 配置文件接下来,我们需要配置 `application.properties` 文件:
properties# MySQL 配置spring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver# MyBatis 配置mybatis.type-aliases-package=com.example.demo.entity
### 实体类接下来,我们需要创建实体类 `User.java`:
java// User.javapackage com.example.demo.entity; import java.util.Date; import lombok.Data; @Datapublic class User { private Long id; private String name; private Date birthday; }
### Mapper 接口接着,我们需要创建 Mapper 接口 `UserMapper.java`:
java// UserMapper.javapackage com.example.demo.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.demo.entity.User; import org.apache.ibatis.annotations.Mapper; @Mapperpublic interface UserMapper extends BaseMapper{ }
### Service 类然后,我们需要创建 Service 类 `UserService.java`:
java// UserService.javapackage com.example.demo.service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.demo.entity.User; import com.example.demo.mapper.UserMapper; import org.springframework.stereotype.Service; @Servicepublic class UserService extends ServiceImpl{ }
### Controller 类最后,我们需要创建 Controller 类 `UserController.java`:
java// UserController.javapackage 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.*; @RestController@RequestMapping("/api") public class UserController { @Autowired private UserService userService; @GetMapping("/users") public ListgetUsers() { return userService.list(); } @PostMapping("/users") public User createUser(@RequestBody User user) { return userService.save(user); } @PutMapping("/users/{id}") public User updateUser(@PathVariable Long id, @RequestBody User user) { return userService.updateById(id, user); } @DeleteMapping("/users/{id}") public void deleteUser(@PathVariable Long id) { userService.removeById(id); } }
### 测试最后,我们可以使用 Postman 或其他工具来测试我们的 API:
* GET /api/users:获取所有用户列表* POST /api/users:创建新用户* PUT /api/users/{id}:更新指定 ID 的用户* DELETE /api/users/{id}:删除指定 ID 的用户通过以上步骤,我们成功地实现了 Spring Boot + MyBatis 整合 + Restful 风格 + CRUD 简单案例。