springBoot中myBatisPlus的使用步骤及示例代码
作者:无足鸟丶
MyBatis-Plus 是一个 MyBatis 的增强工具,在 Spring Boot 项目里使用它能极大提升开发效率,下面为你详细介绍在 Spring Boot 中使用 MyBatis-Plus 的步骤以及示例代码,感兴趣的朋友一起看看吧
MyBatis-Plus 是一个 MyBatis 的增强工具,在 Spring Boot 项目里使用它能极大提升开发效率。下面为你详细介绍在 Spring Boot 中使用 MyBatis-Plus 的步骤以及示例代码。
1. 创建 Spring Boot 项目
你可以借助 Spring Initializr(https://start.spring.io/)来创建一个新的 Spring Boot 项目,添加以下依赖:
- Spring Web
- Spring Data JPA(虽然用 MyBatis-Plus,但这个依赖可按需添加)
- MyBatis Framework
- MySQL Driver(假设使用 MySQL 数据库)
2. 添加 MyBatis-Plus 依赖
在 pom.xml 里添加 MyBatis-Plus 的依赖:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>3. 配置数据库连接
在 application.properties 或 application.yml 中配置数据库连接信息:
spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
4. 创建实体类
创建一个简单的实体类,例如 User:
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
@TableName("user")
public class User {
@TableId
private Long id;
private String name;
private Integer age;
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}5. 创建 Mapper 接口
创建一个继承自 BaseMapper 的 Mapper 接口:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}6. 创建 Service 层
创建一个 Service 接口及其实现类:
import java.util.List;
public interface UserService {
List<User> getAllUsers();
User getUserById(Long id);
boolean saveUser(User user);
boolean updateUser(User user);
boolean deleteUser(Long id);
}import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> getAllUsers() {
return userMapper.selectList(null);
}
@Override
public User getUserById(Long id) {
return userMapper.selectById(id);
}
@Override
public boolean saveUser(User user) {
return userMapper.insert(user) > 0;
}
@Override
public boolean updateUser(User user) {
return userMapper.updateById(user) > 0;
}
@Override
public boolean deleteUser(Long id) {
return userMapper.deleteById(id) > 0;
}
}7. 创建 Controller 层
创建一个 Controller 来处理 HTTP 请求:
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 Long id) {
return userService.getUserById(id);
}
@PostMapping
public boolean saveUser(@RequestBody User user) {
return userService.saveUser(user);
}
@PutMapping
public boolean updateUser(@RequestBody User user) {
return userService.updateUser(user);
}
@DeleteMapping("/{id}")
public boolean deleteUser(@PathVariable Long id) {
return userService.deleteUser(id);
}
}8. 启动应用
启动 Spring Boot 应用后,你就能通过以下 API 来操作 User 数据:
GET /users:获取所有用户信息。GET /users/{id}:根据 ID 获取用户信息。POST /users:新增用户。PUT /users:更新用户信息。DELETE /users/{id}:根据 ID 删除用户。
按照以上步骤,你就能在 Spring Boot 项目中使用 MyBatis-Plus 进行数据库操作了。
到此这篇关于springBoot中myBatisPlus的使用步骤及示例代码的文章就介绍到这了,更多相关springBoot myBatisPlus使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
- springboot如何使用MybatisPlus
- SpringBoot3.0集成MybatisPlus的实现方法
- Springboot mybatisplus如何解决分页组件IPage失效问题
- SpringBoot连接PostgreSQL+MybatisPlus入门案例(代码详解)
- SpringBoot项目整合MybatisPlus并使用SQLite作为数据库的过程
- SpringBoot整合MybatisPlus的基本应用详解
- 解决SpringBoot搭建MyBatisPlus中selectList遇到LambdaQueryWrapper报错问题
- SpringBoot集成MyBatisPlus+MySQL的实现
