MyBatis Plus 使用 Service 接口进行增删改查

宅哥聊构架 后端 2025-03-24

MyBatis Plus 使用 Service 接口进行增删改查

一、概述

一般我们不在 controller 层直接使用 mapper 方法去操控数据库,而是通过 service 写业务逻辑,然后去操控数据库。

src/main/resources/application.yml 中加上以下配置,可以在控制台打印 sql 语句。yml

体验AI代码助手
代码解读
复制代码
mybatis-plus: configuration: # 显示查询语句 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

二、步骤

2.1 新建实体类java

体验AI代码助手
代码解读
复制代码
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; /** * @author: yunhu * @date: 2022/6/14 */ @Data @TableName("sys_user") public class UserTableEntity { @TableId("id") private Integer id; @TableField("username") private String username; @TableField("password") private String password; }

sys_user 中的数据

idusernamepassword
1adminyunhu0
2test123456

2.2 新建 UserService 接口

新建 service目录,在 service目录下新建 UserService 接口继承 IService<T>

T 泛型在这边就是 UserTableEntity实体类。java

体验AI代码助手
代码解读
复制代码
import com.baomidou.mybatisplus.extension.service.IService; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.util.List; /** * @author: yunhu * @date: 2022/6/14 */ @Service @Component public interface UserService extends IService<UserTableEntity> { }

2.3 新建 UserServiceImpl 类

service目录下新建 impl目录,在 impl目录中新建 UserServiceImpl类 。

UserServiceImpl去继承 ServiceImpl<M extends BaseMapper<T> , T> implements IService<T>

ServiceImpl 有两个参数:

  1. M extends BaseMapper<T>是一个继承 BaseMapper<T>的自定义 mapper,接下来会新建。
  2. T实体类。java
体验AI代码助手
代码解读
复制代码
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; import java.util.List; /** * @author: yunhu * @date: 2022/6/14 */ @Service public class UserServiceImpl extends ServiceImpl<UserTableMapper, UserTableEntity> implements UserService { }

2.4 新建UserTableMapper接口

新建 mapper 目录,然后在 mapper 目录下新建 UserTableMapper接口。java

体验AI代码助手
代码解读
复制代码
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; /** * @author: yunhu * @date: 2022/6/14 */ @Mapper public interface UserTableMapper extends BaseMapper<UserTableEntity> { }

三、测试

3.1 查询java

体验AI代码助手
代码解读
复制代码
package com.example.library; import com.example.library.entity.UserTableEntity; import com.example.library.service.UserService; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; /** * @author: yunhu * @date: 2022/6/14 */ @MapperScan("com.example.library.mapper") @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest class LibraryApplicationTests { @Autowired private UserService userService; @Test void contextLoads() { // 查找所有值 List<UserTableEntity> list = userService.list(); list.forEach(System.out::println); } }

output:sh

体验AI代码助手
代码解读
复制代码
UserTableEntity(id=1, username=admin, password=yunhu0) UserTableEntity(id=2, username=test, password=123456)

3.2 插入java

体验AI代码助手
代码解读
复制代码
void contextLoads() { // 插入操作 UserTableEntity userTableEntity = new UserTableEntity(); userTableEntity.setId(3); userTableEntity.setUsername("cheng"); userTableEntity.setPassword("666666"); Boolean res = userService.save(userTableEntity); if (res) { System.out.println("add success"); // 查看插入后的表中数据 List<UserTableEntity> list = userService.list(); list.forEach(System.out::println); } }

output:sh

体验AI代码助手
代码解读
复制代码
add success UserTableEntity(id=1, username=admin, password=yunhu0) UserTableEntity(id=2, username=test, password=123456) UserTableEntity(id=3, username=cheng, password=666666)

3.3 删除

删除用户名为 admin 的记录。java

体验AI代码助手
代码解读
复制代码
void contextLoads() { QueryWrapper<UserTableEntity> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("username", "admin"); boolean res = userService.remove(queryWrapper); if(res) { System.out.println("delete success"); // 查看删除后的表中数据 List<UserTableEntity> list = userService.list(); list.forEach(System.out::println); } }

output:sh

体验AI代码助手
代码解读
复制代码
delete success UserTableEntity(id=2, username=test, password=123456) UserTableEntity(id=3, username=cheng, password=666666)

3.4 更新java

体验AI代码助手
代码解读
复制代码
void contextLoads() { UserTableEntity userTableEntity = new UserTableEntity(); userTableEntity.setId(3); userTableEntity.setUsername("Tom Sawyer"); userTableEntity.setPassword("777777"); // 通过 id 更新 boolean res = userService.updateById(userTableEntity); if(res) { System.out.println("update success"); // 查看更新后的表中数据 List<UserTableEntity> list = userService.list(); list.forEach(System.out::println); } }

output:sh

体验AI代码助手
代码解读
复制代码
update success UserTableEntity(id=2, username=test, password=123456) UserTableEntity(id=3, username=Tom Sawyer, password=777777)

转载来源:https://juejin.cn/post/7109017150328668174

Apipost 私有化火热进行中

评论