使用 MyBatisPlus
的 Service
接口,实现基础的增删改查功能。
创建Service,继承自MyBatisPlus提供的Service接口。
package com.example.web.service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.web.entity.User;
import com.example.web.mapper.UserMapper;
import org.springframework.stereotype.Service;
@Service
public class UserService extends ServiceImpl<UserMapper, User> {
}
@Test
void save() {
User user = new User();
user.setId(7L);
user.setName("李四");
user.setAge(26);
user.setEmail("lisi@example.com");
userService.save(user);
}
@Test
public void removeById() {
userService.removeById(7);
}
@Test
void updateById() {
User user = new User();
user.setId(7L);
user.setName("李四");
user.setAge(36);
user.setEmail("lisi@example.com");
userService.updateById(user);
}
@Test
public void getById() {
User user = userService.getById(7);
System.out.println(user);
}
@Test
void list() {
List<User> list = userService.list();
System.out.println(list);
}
package com.example;
import com.example.web.entity.User;
import com.example.web.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class ServiceTest {
@Autowired
private UserService userService;
@Test
void save() {
User user = new User();
user.setId(7L);
user.setName("李四");
user.setAge(26);
user.setEmail("lisi@example.com");
userService.save(user);
}
@Test
public void removeById() {
userService.removeById(7);
}
@Test
void updateById() {
User user = new User();
user.setId(7L);
user.setName("李四");
user.setAge(36);
user.setEmail("lisi@example.com");
userService.updateById(user);
}
@Test
public void getById() {
User user = userService.getById(7);
System.out.println(user);
}
@Test
void list() {
List<User> list = userService.list();
System.out.println(list);
}
}