一、概述

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

二、步骤

2.1 新建实体类

  1. import com.baomidou.mybatisplus.annotation.TableField;
  2. import com.baomidou.mybatisplus.annotation.TableId;
  3. import com.baomidou.mybatisplus.annotation.TableName;
  4. import lombok.Data;
  5. /**
  6. * @author: yunhu
  7. * @date: 2022/6/14
  8. */
  9. @Data
  10. @TableName("sys_user")
  11. public class UserTableEntity {
  12. @TableId("id")
  13. private Integer id;
  14. @TableField("username")
  15. private String username;
  16. @TableField("password")
  17. private String password;
  18. }

sys_user 中的数据

id username password
1 admin yunhu0
2 test 123456

2.2 新建 UserService 接口

新建 service目录,在 service目录下新建 UserService 接口继承 IService<T>
T 泛型在这边就是 UserTableEntity实体类。

  1. import com.baomidou.mybatisplus.extension.service.IService;
  2. import org.springframework.stereotype.Component;
  3. import org.springframework.stereotype.Service;
  4. import java.util.List;
  5. /**
  6. * @author: yunhu
  7. * @date: 2022/6/14
  8. */
  9. @Service
  10. @Component
  11. public interface UserService extends IService<UserTableEntity> {
  12. List<UserTableEntity> listAllByUsername(String username);
  13. }

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 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 implements UserService { }

  1. <a name="gxqrN"></a>
  2. ### 2.4 新建`UserTableMapper`接口
  3. 新建 `mapper` 目录,然后在 `mapper` 目录下新建 `UserTableMapper`接口。
  4. ```java
  5. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  6. import org.apache.ibatis.annotations.Mapper;
  7. /**
  8. * @author: yunhu
  9. * @date: 2022/6/14
  10. */
  11. @Mapper
  12. public interface UserTableMapper extends BaseMapper<UserTableEntity> {
  13. }

三、测试

3.1 查询

  1. package com.example.library;
  2. import com.example.library.entity.UserTableEntity;
  3. import com.example.library.service.UserService;
  4. import org.junit.jupiter.api.Test;
  5. import org.junit.runner.RunWith;
  6. import org.mybatis.spring.annotation.MapperScan;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  10. import java.util.List;
  11. /**
  12. * @author: yunhu
  13. * @date: 2022/6/14
  14. */
  15. @MapperScan("com.example.library.mapper")
  16. @RunWith(SpringJUnit4ClassRunner.class)
  17. @SpringBootTest
  18. class LibraryApplicationTests {
  19. @Autowired
  20. private UserService userService;
  21. @Test
  22. void contextLoads() {
  23. // 查找所有值
  24. List<UserTableEntity> list = userService.list();
  25. list.forEach(System.out::println);
  26. }
  27. }

output:

  1. UserTableEntity(id=1, username=admin, password=yunhu0)
  2. UserTableEntity(id=2, username=test, password=123456)

3.2 插入

  1. void contextLoads() {
  2. // 插入操作
  3. UserTableEntity userTableEntity = new UserTableEntity();
  4. userTableEntity.setId(3);
  5. userTableEntity.setUsername("cheng");
  6. userTableEntity.setPassword("666666");
  7. Boolean res = userService.save(userTableEntity);
  8. if (res) {
  9. System.out.println("add success");
  10. // 查看插入后的表中数据
  11. List<UserTableEntity> list = userService.list();
  12. list.forEach(System.out::println);
  13. }
  14. }

output:

  1. add success
  2. UserTableEntity(id=1, username=admin, password=yunhu0)
  3. UserTableEntity(id=2, username=test, password=123456)
  4. UserTableEntity(id=3, username=cheng, password=666666)

3.3 删除

删除用户名为 admin 的记录。

  1. void contextLoads() {
  2. QueryWrapper<UserTableEntity> queryWrapper = new QueryWrapper<>();
  3. queryWrapper.eq("username", "admin");
  4. boolean res = userService.remove(queryWrapper);
  5. if(res) {
  6. System.out.println("delete success");
  7. // 查看删除后的表中数据
  8. List<UserTableEntity> list = userService.list();
  9. list.forEach(System.out::println);
  10. }
  11. }

output:

  1. delete success
  2. UserTableEntity(id=2, username=test, password=123456)
  3. UserTableEntity(id=3, username=cheng, password=666666)

3.4 更新

  1. void contextLoads() {
  2. UserTableEntity userTableEntity = new UserTableEntity();
  3. userTableEntity.setId(3);
  4. userTableEntity.setUsername("Tom Sawyer");
  5. userTableEntity.setPassword("777777");
  6. // 通过 id 更新
  7. boolean res = userService.updateById(userTableEntity);
  8. if(res) {
  9. System.out.println("update success");
  10. // 查看更新后的表中数据
  11. List<UserTableEntity> list = userService.list();
  12. list.forEach(System.out::println);
  13. }
  14. }

output:

  1. update success
  2. UserTableEntity(id=2, username=test, password=123456)
  3. UserTableEntity(id=3, username=Tom Sawyer, password=777777)