package com.taotao.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.taotao.domain.User;
import java.util.List;
/**
* create by 刘鸿涛
* 2022/5/4 19:32
*/
@SuppressWarnings({"all"})
public interface UserService {
Boolean save(User user);
Boolean updata(User user);
Boolean delete(Integer id);
User getById(Integer id);
List<User> getAll();
IPage<User> getPage(int currentPage,int pageSize);
}
package com.taotao.service.impl;
import com.taotao.dao.UserMapper;
import com.taotao.domain.User;
import com.taotao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.util.List;
/**
* create by 刘鸿涛
* 2022/5/4 19:36
*/
@SuppressWarnings({"all"})
//业务层对应的bean
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public Boolean save(User user) {
return userMapper.insert(user) > 0; //影响行大于0时进行操作
}
@Override
public Boolean updata(User user) {
return userMapper.updateById(user) > 0;
}
@Override
public Boolean delete(Integer id) {
return userMapper.deleteById(id) > 0;
}
@Override
public User getById(Integer id) {
return userMapper.selectById(id);
}
@Override
public List<User> getAll() {
return userMapper.selectList(null);
}
}
package com.taotao.service;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* create by 刘鸿涛
* 2022/5/4 19:47
*/
@SuppressWarnings({"all"})
@SpringBootTest
public class UserServiceTestCase {
@Autowired
private UserService userService;
@Test
void testGetById(){
System.out.println(userService.getById(4));
}
}
package com.taotao.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.taotao.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* create by 刘鸿涛
* 2022/5/4 19:47
*/
@SuppressWarnings({"all"})
@SpringBootTest
public class UserServiceTestCase {
@Autowired
private UserService userService;
@Test
void testGetById(){
System.out.println(userService.getById(4));
}
//添加
@Test
void testSave(){
User user = new User();
user.setUsername("taotao");
user.setAddr("上海");
user.setPassword(456);
user.setGender("女");
userService.save(user);
}
@Test
void testUpdata(){
User user = new User();
user.setId(3);
user.setUsername("刘鸿涛");
user.setAddr("上海");
user.setPassword(456);
user.setGender("男");
userService.updata(user);
}
@Test
void testDelete(){
userService.delete(4);
}
//查询所有
@Test
void testGetAll(){
userService.getAll();
}
@Test
void testGetPage(){
//两个参数分别代表“第几页”,每页显示多少条数据
IPage<User> page = userService.getPage(2, 2);
// System.out.println(page.getCurrent());
// System.out.println(page.getRecords());
// System.out.println(page.getSize());
// System.out.println(page.getTotal());
}
}
IUserService
package com.taotao.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.taotao.domain.User;
/**
* create by 刘鸿涛
* 2022/5/4 21:18
*/
@SuppressWarnings({"all"})
public interface IBookService extends IService<User> {
}
ctrl + F12查看方法
package com.taotao.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taotao.dao.UserMapper;
import com.taotao.domain.User;
import com.taotao.service.IUserService;
import org.springframework.stereotype.Service;
/**
* create by 刘鸿涛
* 2022/5/4 21:31
*/
@SuppressWarnings({"all"})
@Service
public class IUserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
}
package com.taotao.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taotao.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* create by 刘鸿涛
* 2022/5/4 19:47
*/
@SuppressWarnings({"all"})
@SpringBootTest
public class IUserServiceTestcase {
@Autowired
private IUserService userService;
@Test
void testGetById(){
System.out.println(userService.getById(4));
}
//添加
@Test
void testSave(){
User user = new User();
user.setUsername("taotao");
user.setAddr("上海");
user.setPassword(456);
user.setGender("女");
userService.save(user);
}
@Test
void testUpdata(){
User user = new User();
user.setId(3);
user.setUsername("刘鸿涛");
user.setAddr("上海");
user.setPassword(456);
user.setGender("男");
userService.updateById(user);
}
@Test
void testDelete(){
userService.removeById(4);
}
//查询所有
@Test
void testGetAll(){
userService.list();
}
@Test
void testGetPage(){
//两个参数分别代表“第几页”,每页显示多少条数据
Page<User> page = new Page<User>(2,2);
userService.page(page);
}
}
package com.taotao.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.taotao.domain.User;
/**
* create by 刘鸿涛
* 2022/5/4 21:18
*/
@SuppressWarnings({"all"})
public interface IUserService extends IService<User> {
boolean saveUser(User user);
boolean modify(User user);
boolean delete(Integer id);
}
package com.taotao.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taotao.dao.UserMapper;
import com.taotao.domain.User;
import com.taotao.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* create by 刘鸿涛
* 2022/5/4 21:31
*/
@SuppressWarnings({"all"})
@Service
public class IUserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public boolean saveUser(User user) {
return userMapper.insert(user) > 0;
}
@Override
public boolean modify(User user) {
return userMapper.updateById(user) > 0;
}
@Override
public boolean delete(Integer id) {
return userMapper.deleteById(id) > 0;
}
}